-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
43 lines (36 loc) · 978 Bytes
/
init.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
package ctx
import (
"net/http"
"github.com/auth0/go-jwt-middleware"
"github.com/codegangsta/negroni"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
)
func Init(router *mux.Router, privKey, pubKey string, vars map[string]interface{}) error {
LoadSecureKeys(privKey, pubKey)
// create and configure a new context
context := &Context{
Router: router,
Vars: vars,
middleware: jwtmiddleware.New(
jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) { return verifyKey, nil },
},
),
}
// add the registered endpoints
for _, endpoint := range endpoints {
httpHandler := newContextHandler(context, endpoint)
if endpoint.Public {
context.Router.HandleFunc(endpoint.Path, httpHandler)
} else {
context.Router.Handle(
endpoint.Path, negroni.New(
negroni.HandlerFunc(context.middleware.HandlerWithNext),
negroni.Wrap(http.HandlerFunc(httpHandler)),
),
)
}
}
return nil
}