-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
53 lines (40 loc) · 1.22 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
package server
import (
"net/http"
"github.com/anfimovoleh/ms-content-manager/server/handlers"
"github.com/anfimovoleh/go-chi-middlewares"
"github.com/anfimovoleh/ms-content-manager/config"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/chi"
)
func Router(cfg config.Config) chi.Router {
router := chi.NewRouter()
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"*", "GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
AllowedHeaders: []string{"*", "Accept", "Authorization", "Content-Type", "X-CSRF-Asset", "x-auth"},
ExposedHeaders: []string{"*", "Link"},
AllowCredentials: true,
MaxAge: 300,
})
router.Use(
chiwares.Logger(cfg.Log(), cfg.HTTP().ReqDurThreshold),
c.Handler,
middleware.Recoverer,
)
router.Mount("/admin", adminRouter(cfg))
return router
}
// A completely separate router for administrator routes
func adminRouter(cfg config.Config) http.Handler {
r := chi.NewRouter()
r.Use(
chiwares.BasicAuth("admin", "admin"),
chiwares.VerifyRemoteAddressIsPrivate(nil),
)
r.Route("/upload", func(r chi.Router) {
r.Post("/file", handlers.NewUploadDocumentHandler(cfg.Log()).Handle)
})
return r
}