-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
39 lines (29 loc) · 814 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
package rest
import (
"net/http"
"github.com/devisions/go-mux-jwt-gorm-starter/api/rest/routes"
"github.com/devisions/go-mux-jwt-gorm-starter/users"
"github.com/gorilla/mux"
)
func NewRestApiRouter(usersRestApi users.RestApi) *mux.Router {
router := mux.NewRouter()
// setup the routes
var ApiRestRouteSets []routes.ApiRestRouteSet
ApiRestRouteSets = append(ApiRestRouteSets, usersRestApi.Routes)
for _, routeSet := range ApiRestRouteSets {
subRouter := router.PathPrefix(routeSet.Prefix).Subrouter()
for _, sr := range routeSet.SubRoutes {
var handler http.Handler
handler = sr.HandlerFunc
if sr.Protected {
handler = JWTMiddleware(sr.HandlerFunc)
}
subRouter.
Path(sr.Pattern).
Handler(handler).
Methods(sr.Method).
Name(sr.Name)
}
}
return router
}