-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
62 lines (48 loc) · 1.43 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
package handler
import (
"github.com/go-chi/chi"
"net/http"
"strings"
)
func (h *Handler) NewRouter() *chi.Mux {
router := chi.NewRouter()
router.Use(CORS)
router.Get("/api/health", h.HealthHandler)
router.Get("/api/readiness", h.ReadinessHandler)
router.Post("/api/create", h.CreateIcal)
router.Post("/api/show", h.CreateSchedule)
return router
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func (h *Handler) FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}
func CORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set headers
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
// Next
next.ServeHTTP(w, r)
return
})
}