-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
47 lines (37 loc) · 1.67 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"net/http"
"github.com/gorilla/mux"
"github.com/nathanmalishev/go_api_example/common"
"github.com/nathanmalishev/go_api_example/controllers"
"github.com/nathanmalishev/go_api_example/models"
"github.com/urfave/negroni"
)
func InitRoutes(store models.DataStorer, authModule common.Authorizer) http.Handler {
router := mux.NewRouter().StrictSlash(false)
/* healthcheck / motd */
router.Handle("/", http.HandlerFunc(controllers.Healthcheck)).Methods("GET")
/* User routes */
router.Handle("/users", controllers.WithDbAndAuth(authModule, store, controllers.Register)).Methods("POST")
router.Handle("/users/login", controllers.WithDbAndAuth(authModule, store, controllers.Login)).Methods("POST")
//router.Handle("/users", withDb(store, controllers.GetUser)).Methods("GET")
/* Task routes */
taskRouter := mux.NewRouter().StrictSlash(false)
taskRouter.Handle("/tasks", controllers.WithDb(store, controllers.GetAllTasks)).Methods("GET")
taskRouter.Handle("/tasks/{id}", controllers.WithDb(store, controllers.GetTask)).Methods("GET")
taskRouter.Handle("/tasks/{id}", controllers.WithDb(store, controllers.DeleteTask)).Methods("DELETE")
taskRouter.Handle("/tasks", controllers.WithDb(store, controllers.CreateTask)).Methods("POST")
taskRouter.Handle("/tasks/{id}", controllers.WithDb(store, controllers.UpdateTask)).Methods("PUT")
/* middleware */
commonMidleware := negroni.New(
negroni.NewLogger(),
)
router.PathPrefix("/tasks").Handler(negroni.New(
common.WithAuth(authModule),
negroni.Wrap(taskRouter),
))
// common wraps all routes in default middleware
// this includes all API hits
commonMidleware.UseHandler(router)
return commonMidleware
}