-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
61 lines (50 loc) · 1.47 KB
/
middlewares.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
49
50
51
52
53
54
55
56
57
58
59
60
61
package golaze
import (
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog/log"
)
type AuthToken struct {
Token string
}
func NewAuthMiddleware(getAuthTokens func() ([]*AuthToken, error)) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authTokens, err := getAuthTokens()
if err != nil {
JSONError(w, "error getting auth tokens", http.StatusInternalServerError)
return
}
authHeader := r.Header.Get("Authorization")
if !strings.HasPrefix(authHeader, "Bearer ") {
JSONError(w, "unauthorized", http.StatusUnauthorized)
return
}
token := strings.TrimPrefix(authHeader, "Bearer ")
authorized := false
for _, t := range authTokens {
if t.Token == token {
authorized = true
break
}
}
if !authorized {
JSONError(w, "unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
}
func LogMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(ww, r)
rctx := chi.RouteContext(r.Context())
host := r.Header.Get("Host")
url := r.URL.String()
log.Info().Msgf("request: %v | method: %v | host: %v | status: %v | route pattern: %v", url, r.Method, host, ww.Status(), rctx.RoutePatterns)
})
}