-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
107 lines (86 loc) · 3.01 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
98
99
100
101
102
103
104
105
106
107
package router
import (
"net/http"
"time"
"github.com/pagient/pagient-server/internal/config"
"github.com/pagient/pagient-server/internal/service"
"github.com/pagient/pagient-server/internal/ui/handler"
"github.com/pagient/pagient-server/internal/ui/router/context"
"github.com/pagient/pagient-server/internal/ui/router/middleware"
"github.com/pagient/pagient-server/internal/ui/static"
"github.com/pagient/pagient-server/internal/ui/websocket"
"github.com/go-chi/chi"
chiMiddleware "github.com/go-chi/chi/middleware"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
// Load initializes the routing of the application.
func Load(s service.Service, wsHub *websocket.Hub) http.Handler {
mux := chi.NewRouter()
mux.Use(hlog.NewHandler(log.Logger))
mux.Use(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Debug().
Str("method", r.Method).
Str("url", r.URL.String()).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("")
}))
mux.Use(hlog.RemoteAddrHandler("ip"))
mux.Use(hlog.RequestIDHandler("request_id", "Request-Id"))
mux.Use(chiMiddleware.RequestID)
mux.Use(chiMiddleware.RealIP)
mux.Use(chiMiddleware.Recoverer)
mux.Use(chiMiddleware.Timeout(60 * time.Second))
mux.Use(middleware.Version)
mux.Use(middleware.Cache)
mux.Use(middleware.Secure())
mux.Use(middleware.Options())
tokenAuth := jwtauth.New("HS256", []byte(config.General.Secret), nil)
mux.Route("/", func(root chi.Router) {
root.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(tokenAuth))
r.Use(middleware.Authenticator(s, s))
r.Route("/api", func(r chi.Router) {
r.Use(render.SetContentType(render.ContentTypeJSON))
r.Use(context.AuthCtx(s))
// Manage patients
r.Route("/patients", func(r chi.Router) {
r.Get("/", handler.GetPatients(s))
r.With(context.ClientCtx(s)).Post("/", handler.AddPatient(s))
r.Route("/{patientID}", func(r chi.Router) {
r.Use(context.PatientCtx(s))
r.Get("/", handler.GetPatient())
r.With(context.ClientCtx(s)).Post("/", handler.UpdatePatient(s))
r.Delete("/", handler.DeletePatient(s))
})
})
// List pagers
r.Get("/pagers", handler.GetPagers(s))
// List clients
r.Get("/clients", handler.GetClients(s))
})
// Serve Websocket
r.Get("/ws", handler.ServeWebsocket(s, wsHub))
})
root.Route("/oauth", func(r chi.Router) {
r.Post("/token", handler.CreateToken(s, s))
r.Route("/", func(r chi.Router) {
r.Use(jwtauth.Verifier(tokenAuth))
r.Use(middleware.Authenticator(s, s))
r.Delete("/token", handler.DeleteToken(s, wsHub))
r.Get("/sessions", handler.GetSessions(s, s))
})
})
// Pagient UI static files
root.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// static files contain all files from "public/dist/"
fs := http.StripPrefix("/", http.FileServer(static.HTTP))
fs.ServeHTTP(w, req)
}))
})
return mux
}