use chi as router
This commit is contained in:
@@ -8,29 +8,45 @@ import (
|
|||||||
|
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/web/handler"
|
"github.com/LazyBachelor/LazyPM/pkg/web/handler"
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
|
func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
|
||||||
mux := http.NewServeMux()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
s.handleAssets(mux, assets)
|
r.Use(middleware.Logger)
|
||||||
|
r.Use(middleware.Recoverer)
|
||||||
|
r.Use(cors.AllowAll().Handler)
|
||||||
|
r.Use(middleware.CleanPath)
|
||||||
|
|
||||||
for _, route := range handler.GetRoutes(s.Services) {
|
r.Use(handler.HTMXMiddleware)
|
||||||
mux.Handle(route.Pattern, route.Handler)
|
r.Use(handler.ServicesMiddleware(s.Services))
|
||||||
}
|
|
||||||
|
|
||||||
handler := cors.Default().Handler(mux)
|
s.handleAssets(r, assets)
|
||||||
|
|
||||||
return gziphandler.GzipHandler(handler)
|
r.Get("/", handler.IndexHandler)
|
||||||
|
|
||||||
|
r.Route("/issues", func(r chi.Router) {
|
||||||
|
r.Get("/", handler.ListIssues)
|
||||||
|
r.Post("/", handler.CreateIssue)
|
||||||
|
|
||||||
|
r.Route("/{id}", func(r chi.Router) {
|
||||||
|
r.Use(handler.IssueCtx)
|
||||||
|
r.Get("/", handler.GetIssue)
|
||||||
|
r.Put("/", handler.UpdateIssue)
|
||||||
|
r.Delete("/", handler.DeleteIssue)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return gziphandler.GzipHandler(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleAssets(mux *http.ServeMux, assets embed.FS) {
|
func (s *Server) handleAssets(r chi.Router, assets embed.FS) {
|
||||||
mux.Handle("/assets/",
|
r.Handle("/assets/*", http.StripPrefix("/assets/",
|
||||||
http.StripPrefix("/assets/",
|
http.FileServer(http.Dir("pkg/web/assets"))))
|
||||||
http.FileServer(http.Dir("pkg/web/assets"))))
|
|
||||||
|
|
||||||
mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("GET /robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
http.ServeContent(w, r, "robots.txt", time.Now(), strings.NewReader("User-agent: *\nAllow: /"))
|
http.ServeContent(w, r, "robots.txt", time.Now(), strings.NewReader("User-agent: *\nAllow: /"))
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user