From af4f3c796be0cb79d6eed980447125da7306c1af Mon Sep 17 00:00:00 2001 From: daltonfury42 Date: Sun, 26 Dec 2021 10:40:25 +0530 Subject: [PATCH] Use go-chi for handling routes (#11) --- cmd/api/main.go | 10 ++++------ go.mod | 1 + go.sum | 2 ++ internal/handler/queue.go | 15 ++------------- internal/mux/mux.go | 26 +++++++++++++++++++++----- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/cmd/api/main.go b/cmd/api/main.go index 7562c4d..95f60f4 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -9,14 +9,12 @@ import ( ) func main() { - listenAddr := ":8080" + routes := mux.InitalizeRoutes() + listenAddr := ":8080" if val, ok := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT"); ok { listenAddr = ":" + val } - - mux.InitalizeRoutes() - - log.Printf("About to listen on %s. Go to https://127.0.0.1%s/", listenAddr, listenAddr) - log.Fatal(http.ListenAndServe(listenAddr, nil)) + log.Printf("About to listen on %s. Go to http://127.0.0.1%s/", listenAddr, listenAddr) + log.Fatal(http.ListenAndServe(listenAddr, routes)) } diff --git a/go.mod b/go.mod index 3dc1901..72c568a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require go.mongodb.org/mongo-driver v1.8.1 require ( + github.com/go-chi/chi/v5 v5.0.7 // indirect github.com/go-stack/stack v1.8.0 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/klauspost/compress v1.13.6 // indirect diff --git a/go.sum b/go.sum index 4c42161..95a4298 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= +github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= diff --git a/internal/handler/queue.go b/internal/handler/queue.go index dcb460e..fb8d05c 100644 --- a/internal/handler/queue.go +++ b/internal/handler/queue.go @@ -11,22 +11,11 @@ import ( "github.com/SimplQ/simplQ-golang/internal/models" ) -func Queue(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case "GET": - getQueue(w, r) - case "POST": - createQueue(w, r) - default: - fmt.Fprintf(w, "Invalid method") - } -} - -func getQueue(w http.ResponseWriter, r *http.Request) { +func GetQueue(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "GET Queue not implemented") } -func createQueue(w http.ResponseWriter, r *http.Request) { +func CreateQueue(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) var q models.Queue diff --git a/internal/mux/mux.go b/internal/mux/mux.go index d899256..c79bde7 100644 --- a/internal/mux/mux.go +++ b/internal/mux/mux.go @@ -1,13 +1,29 @@ package mux import ( - "net/http" + "time" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" "github.com/SimplQ/simplQ-golang/internal/handler" ) -func InitalizeRoutes() { - // Both paths are needed since /api/queue/ doesn't cover /api/queue - http.HandleFunc("/api/queue/", handler.Queue) - http.HandleFunc("/api/queue", handler.Queue) +func InitalizeRoutes() chi.Router { + r := chi.NewRouter() + + r.Use(middleware.RequestID) + r.Use(middleware.RealIP) + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) + + r.Use(middleware.Timeout(10 * time.Second)) + + // Routes for "queue" resource + r.Route("/queue", func(r chi.Router) { + // POST /articles + r.Post("/", handler.CreateQueue) + }) + + return r; }