-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
59 lines (48 loc) 路 1.43 KB
/
helper.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
package server
import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/render"
"github.com/uwblueprint/shoe-project/config"
)
type Router struct {
chi.Router
}
// CreatCreateRouter creates router with custom configurations
func CreateRouter() Router {
r := chi.NewRouter()
return Router{
Router: r,
}
}
// WithBasicMiddlewares adds basic needed middlewares to the router
func (r Router) WithBasicMiddlewares() Router {
r.Use(
middleware.RequestID,
middleware.Recoverer,
render.SetContentType(render.ContentTypeJSON),
)
return r
}
// WithLoggingMiddleware adds HTTP request logging
func (r Router) WithLoggingMiddleware() Router {
// Log Requests - use appropriate format depending on the encoding
if config.IsLoggerLoggingServerRequests() {
logRequestsBody := config.IsLoggerLoggingServerRequestsBody()
disabledEndpoints := config.GetLoggerDisabledServerEndpoints()
r.Use(loggerHTTPMiddlewareDefault(logRequestsBody, disabledEndpoints))
}
return r
}
// WithCORS adds CORS config to the router
func (r Router) WithCORS() Router {
r.Use(cors.New(cors.Options{
AllowedOrigins: config.GetServerCorsAllowedOrigins(),
AllowedMethods: config.GetServerCorsAllowedMethods(),
AllowedHeaders: config.GetServerCorsAllowedHeaders(),
AllowCredentials: config.IsServerCorsCredentialsAllowed(),
MaxAge: config.GetServerCorsMaxAge(),
}).Handler)
return r
}