Skip to content

Commit 1509d09

Browse files
committed
Use chi (https://github.com/go-chi/chi) instead of mux
1 parent f7d1248 commit 1509d09

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"log"
64
"net/http"
75

8-
"github.com/gorilla/mux"
6+
"github.com/go-chi/chi/v5"
7+
"github.com/go-chi/render"
98

109
internal "github.com/miguno/golang-docker-build-tutorial/internal/pkg"
1110
)
@@ -15,18 +14,23 @@ type Response struct {
1514
Status string `json:"status,omitempty"`
1615
}
1716

18-
func GetStatus(w http.ResponseWriter, _ *http.Request) {
19-
var response Response
20-
if internal.IsIdleToyFunction() {
21-
response = Response{Status: "idle"}
22-
} else {
23-
response = Response{Status: "busy"}
24-
}
25-
json.NewEncoder(w).Encode(response)
26-
}
27-
2817
func main() {
29-
router := mux.NewRouter()
30-
router.HandleFunc("/status", GetStatus).Methods("GET")
31-
log.Fatal(http.ListenAndServe(":8123", router))
18+
r := chi.NewRouter()
19+
20+
// Uncomment to enable logging of incoming HTTP requests to STDOUT.
21+
// Requires `import "github.com/go-chi/chi/v5/middleware"`.
22+
//r.Use(middleware.Logger)
23+
24+
r.Get("/status", func(w http.ResponseWriter, r *http.Request) {
25+
// Create a Response object
26+
var response Response
27+
if internal.IsIdleToyFunction() {
28+
response = Response{Status: "idle"}
29+
} else {
30+
response = Response{Status: "busy"}
31+
}
32+
33+
render.JSON(w, r, response)
34+
})
35+
_ = http.ListenAndServe(":8123", r)
3236
}

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ module github.com/miguno/golang-docker-build-tutorial
33
go 1.22
44

55
require (
6-
github.com/gorilla/mux v1.8.1
6+
github.com/go-chi/chi/v5 v5.0.12
7+
github.com/go-chi/render v1.0.3
78
github.com/stretchr/testify v1.9.0
89
)
910

1011
require (
12+
github.com/ajg/form v1.5.1 // indirect
1113
github.com/davecgh/go-spew v1.1.1 // indirect
1214
github.com/pmezard/go-difflib v1.0.0 // indirect
1315
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
2+
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
13
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
4-
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
5+
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
6+
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
7+
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
8+
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
59
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
610
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
711
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

0 commit comments

Comments
 (0)