This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
97 lines (84 loc) · 2.6 KB
/
router.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package starter
import (
"net/http"
"time"
"github.com/fossapps/starter/middleware"
"github.com/fossapps/starter/rate"
"github.com/gorilla/mux"
"gopkg.in/matryer/respond.v1"
)
type router struct {
router *mux.Router
perm Permissions
server Server
}
func (r router) build() {
r.perm = Const().Permissions
r.userResource()
r.deviceResource()
r.permissionsResource()
r.authResource()
}
func (r router) deviceResource() {
r.router.HandleFunc("/device/register", middleware.Adapt(r.server.RegisterHandler(), middleware.ResponseTime(r.server.Logger))).
Methods("POST")
}
func (r router) userResource() {
r.router.HandleFunc(
"/users",
middleware.Adapt(r.server.CreateUser(), middleware.MustHavePermission(r.perm.User.Create, r.server.Jwt))).
Methods("POST")
r.router.HandleFunc(
"/users", middleware.Adapt(r.server.ListUsers(),
middleware.MustHavePermission(r.perm.User.List, r.server.Jwt))).
Methods("GET")
r.router.Handle("/users/available", r.server.UserAvailability()).
Methods("POST")
r.router.Handle(
"/users/{user}",
middleware.Adapt(r.server.UpdateUser(), middleware.MustHavePermission(r.perm.User.Edit, r.server.Jwt))).
Methods("PUT")
r.router.Handle("/users/{user}", r.server.GetUser()).Methods("GET")
}
func (r router) permissionsResource() {
r.router.HandleFunc(
"/permissions", middleware.Adapt(r.server.ListPermissions(),
middleware.AuthMw(r.server.Jwt))).
Methods("GET")
}
func (r router) authResource() {
r.router.HandleFunc("/session/", r.server.LoginHandler()).
Methods("POST")
r.router.HandleFunc("/session/refresh", r.server.RefreshTokenHandler()).
Methods("POST")
r.router.HandleFunc(
"/session",
middleware.Adapt(r.server.RefreshTokensList(), middleware.AuthMw(r.server.Jwt))).
Methods("GET")
r.router.HandleFunc("/session/{token}", r.server.DeleteSession()).Methods("DELETE")
}
// NewRouter returns a configured router with path and handlers
func NewRouter(s Server) *mux.Router {
muxRouter := mux.NewRouter()
routerInstance := router{
server: s,
router: muxRouter,
}
routerInstance.build()
limiterOptions := middleware.LimiterOptions{
RequestHelper: routerInstance.server.ReqHelper,
Limit: 3,
Namespace: "test",
AddHeaders: true,
Logger: s.Logger,
Limiter: rate.Limiter{
Decay: 10 * time.Second,
Limit: 3,
RedisClient: routerInstance.server.Redis,
},
}
routerInstance.router.HandleFunc("/test", middleware.Adapt(func(writer http.ResponseWriter, request *http.Request) {
respond.With(writer, request, http.StatusOK, "success")
}, middleware.Limit(limiterOptions)))
return routerInstance.router
}