-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrouter.go
46 lines (34 loc) · 922 Bytes
/
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
package main
import (
"github.com/gorilla/mux"
"net/http"
"github.com/hellojebus/go-envoz-api/user"
customRouter "github.com/hellojebus/go-envoz-api/router"
"github.com/hellojebus/go-envoz-api/middleware"
)
func NewRouter() *mux.Router {
//init router
router := mux.NewRouter()
//append user routes
customRouter.AppRoutes = append(customRouter.AppRoutes, user.Routes)
for _, route := range customRouter.AppRoutes {
//create subroute
routePrefix := router.PathPrefix(route.Prefix).Subrouter()
//loop through each sub route
for _, r := range route.SubRoutes {
var handler http.Handler
handler = r.HandlerFunc
//check to see if route should be protected with jwt
if r.Protected {
handler = middleware.JWTMiddleware(r.HandlerFunc)
}
//attach sub route
routePrefix.
Path(r.Pattern).
Handler(handler).
Methods(r.Method).
Name(r.Name)
}
}
return router
}