-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
32 lines (26 loc) · 906 Bytes
/
http.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
package httpapi
import (
"net/http"
"github.com/castaneai/rainbox/pkg/rainbox"
"github.com/go-chi/chi"
)
func NewHandler(v rainbox.Verifier, services *rainbox.Services) http.Handler {
r := chi.NewRouter()
r.Route("/users", routeGroup(v, services, usersApi))
return r
}
func routeGroup(v rainbox.Verifier, services *rainbox.Services, f func(chi.Router, rainbox.Verifier, *rainbox.Services)) func(chi.Router) {
return func(r chi.Router) {
f(r, v, services)
}
}
func apiHandler(services *rainbox.Services, f func(rainbox.UserID, *rainbox.Services, http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, _ := r.Context().Value(userIDCtxKey).(rainbox.UserID)
if userID == rainbox.InvalidUserID {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
f(userID, services, w, r)
}
}