Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NAME = casecmp
BINARY = bin/${NAME}
VERSION ?= $(shell cat VERSION)
VERSION ?= $(shell git describe --tags)
SOURCES = $(shell find . -name '*.go' -o -name 'Makefile')

$(BINARY): $(SOURCES)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/jimeh/casecmp

go 1.20
go 1.24
76 changes: 39 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ curl -X POST -H "Content-Type: application/json" -d '{"a":"Foo Bar","b":"FOO BAR
`))

func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" || r.URL.RawQuery != "" {
casecmpHandler(w, r)
return
}

scheme := "http"
if r.TLS != nil || *forceHTTPSFlag {
scheme = "https"
Expand Down Expand Up @@ -89,20 +94,44 @@ type JSONData struct {
B string `json:"b"`
}

func casecmpHandler(w http.ResponseWriter, r *http.Request) error {
func casecmpHandler(w http.ResponseWriter, r *http.Request) {
equal, err := casecmp(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, err.Error())
return
}

resp := "0"
if equal {
resp = "1"
}

accept := r.Header.Get("Accept")
if strings.Contains(accept, "application/json") {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, _ = fmt.Fprintf(w, `{"result":%s}`, resp)

return
}

_, _ = fmt.Fprint(w, resp)
}

func casecmp(r *http.Request) (bool, error) {
var a, b string

contentType := r.Header.Get("Content-Type")
if strings.Contains(contentType, "application/json") {
body, err := io.ReadAll(r.Body)
if err != nil {
return err
return false, err
}

d := JSONData{}
err = json.Unmarshal(body, &d)
if err != nil {
return err
return false, fmt.Errorf("invalid JSON request: %w", err)
}

a = d.A
Expand All @@ -112,39 +141,7 @@ func casecmpHandler(w http.ResponseWriter, r *http.Request) error {
b = r.FormValue("b")
}

resp := "0"
if strings.EqualFold(string(a), string(b)) {
resp = "1"
}

accept := r.Header.Get("Accept")
if strings.Contains(accept, "application/json") {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, err := fmt.Fprintf(w, `{"result":%s}`, resp)
return err
}

_, err := fmt.Fprint(w, resp)
return err
}

func handler(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
if r.Method != "GET" || r.URL.RawQuery != "" {
err := casecmpHandler(w, r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, err.Error())
}
return
}
indexHandler(w, r)
case "/about":
aboutHandler(w, r)
default:
http.NotFound(w, r)
}
return strings.EqualFold(string(a), string(b)), nil
}

func printVersion() {
Expand Down Expand Up @@ -184,14 +181,19 @@ func startServer() error {
*forceHTTPSFlag = true
}

mux := http.NewServeMux()
mux.HandleFunc("/{$}", indexHandler)
mux.HandleFunc("/about", aboutHandler)
mux.HandleFunc("/about/{$}", aboutHandler)

address := fmt.Sprintf("%s:%d", *bindFlag, *portFlag)
fmt.Printf("Listening on %s\n", address)

srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 30 * time.Second,
Handler: http.HandlerFunc(handler),
Handler: mux,
Addr: address,
}

Expand Down
Loading