Skip to content

Commit

Permalink
Use go-chi for handling routes (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
daltonfury42 authored Dec 26, 2021
1 parent 63b49d8 commit af4f3c7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
10 changes: 4 additions & 6 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
15 changes: 2 additions & 13 deletions internal/handler/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions internal/mux/mux.go
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit af4f3c7

Please sign in to comment.