Skip to content

Commit c53244b

Browse files
👋chi-router setup done
1 parent 4dd185f commit c53244b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

handler_err.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "net/http"
4+
5+
func handlerErr(w http.ResponseWriter, r *http.Request) {
6+
respondWithError(w, 400, "something went wrong")
7+
}

json.go

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import (
66
"net/http"
77
)
88

9+
func respondWithError(w http.ResponseWriter, code int, msg string) {
10+
if code > 499 {
11+
log.Println("Error with the response 5XX :", msg)
12+
}
13+
type errResponse struct {
14+
Error string `json:"error"`
15+
}
16+
respondWithJson(w, code, errResponse{
17+
Error: msg,
18+
})
19+
}
20+
921
func respondWithJson(w http.ResponseWriter, code int, payload interface{}) {
1022
dat, err := json.Marshal(payload)
1123
if err != nil {

main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func main() {
2727
}))
2828

2929
v1Router := chi.NewRouter()
30-
v1Router.HandleFunc("/healtz", handlerReadiness)
30+
v1Router.Get("/healtz", handlerReadiness)
31+
v1Router.Get("/err", handlerErr)
3132
router.Mount("/v1", v1Router)
3233
srv := &http.Server{
3334
Handler: router,

0 commit comments

Comments
 (0)