generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
48 lines (40 loc) · 988 Bytes
/
routes.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
48
package routes
import (
"net/http"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
)
// Route ...
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
Middlewares []negroni.Handler
}
// AddRoutes adds routes to a router instance. If there are middlewares defined
// for a route, a new negroni app is created and wrapped as a http.Handler
func AddRoutes(routes []Route, router *mux.Router) {
var (
handler http.Handler
n *negroni.Negroni
)
for _, route := range routes {
// Add any specified middlewares
if len(route.Middlewares) > 0 {
n = negroni.New()
for _, middleware := range route.Middlewares {
n.Use(middleware)
}
// Wrap the handler in the negroni app with middlewares
n.Use(negroni.Wrap(route.HandlerFunc))
handler = n
} else {
handler = route.HandlerFunc
}
router.Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
}