Skip to content
Open
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
36 changes: 36 additions & 0 deletions cmd/myapi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"encoding/json"
"io"
"log"
"net/http"
)

func main() {
mux := http.NewServeMux()

mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"status":"ok"}`)

Check failure on line 15 in cmd/myapi/main.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `io.WriteString` is not checked (errcheck)
})

mux.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
defer r.Body.Close()

Check failure on line 23 in cmd/myapi/main.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `r.Body.Close` is not checked (errcheck)
var v any
body, _ := io.ReadAll(r.Body)

Check failure on line 25 in cmd/myapi/main.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `io.ReadAll` is not checked (errcheck)
if err := json.Unmarshal(body, &v); err != nil {
v = map[string]string{"echo": string(body)}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(v)

Check failure on line 30 in cmd/myapi/main.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `(*encoding/json.Encoder).Encode` is not checked (errcheck)
})

addr := ":8080"
log.Println("myapi listening on", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}
Loading