skeleton/internal/web/routes.go

51 lines
1 KiB
Go
Raw Normal View History

2019-05-14 14:11:03 +02:00
package web
import (
"net/http"
"strings"
2019-08-22 00:48:27 +02:00
"bitmask.me/skeleton/internal/app"
2019-05-14 14:11:03 +02:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func registerRoutes(ac *app.App, r chi.Router) {
h := NewHandlers(ac)
r.Use(middleware.Recoverer)
r.Route("/", func(r chi.Router) {
r.Use(
middleware.RedirectSlashes,
h.Session().LoadAndSave,
)
2019-08-22 00:48:27 +02:00
r.Get("/login", h.LoginPageHandler)
r.Post("/login", h.LoginPageHandler)
2020-07-19 11:52:26 +02:00
r.Get("/signup", h.RegisterPageHandler)
r.Post("/signup", h.RegisterPageHandler)
2019-05-14 14:11:03 +02:00
r.Get("/", h.LandingPageHandler)
r.Route("/app", func(r chi.Router) {
// authenticated routes
r.Use(requireLogin(h.Session()))
r.Get("/", h.LandingPageHandler)
})
})
r.Handle("/static/*", staticHandler(ac.Files, "/"))
}
// staticHandler handles the static assets path.
func staticHandler(fs http.FileSystem, prefix string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if prefix != "/" {
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix)
}
http.FileServer(fs).ServeHTTP(w, r)
}
}