-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
43 lines (35 loc) · 1.08 KB
/
handler.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/dgrijalva/jwt-go/request"
"github.com/nicksnyder/go-i18n/i18n"
)
type ContextHandler func(c *Context, rw http.ResponseWriter, req *http.Request) error
func newContextHandler(context *Context, endpoint *Endpoint) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
// check if the http method is mapped
var handler ContextHandler
var found bool
if handler, found = endpoint.Handlers[req.Method]; !found {
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// extract the token, if the endpoint is private
if !endpoint.Public {
context.Token, _ = request.ParseFromRequest(
req,
request.AuthorizationHeaderExtractor,
context.middleware.Options.ValidationKeyGetter,
)
}
// update T
acceptLang := req.Header.Get("Accept-Language")
defaultLang := "en-US"
context.T = i18n.MustTfunc(acceptLang, defaultLang)
// call the apropriate handler
err := handler(context, rw, req)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
}
}
}