feat: translation middleware
This commit is contained in:
@@ -5,19 +5,36 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handler"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/page"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/handlehome"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/templer"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
|
||||
)
|
||||
|
||||
func (s *Server) RegisterRoutes() http.Handler {
|
||||
func (s *Server) RegisterRoutes() (h http.Handler) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
fileServer := http.FileServer(http.FS(web.GetStaticFS()))
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", fileServer))
|
||||
registerStatic(mux)
|
||||
mux.Handle("/", registerOther())
|
||||
|
||||
mux.Handle("/", handler.Home())
|
||||
h = mux
|
||||
h = templer.Middleware(h)
|
||||
h = s.loggingMiddleware(h)
|
||||
return h
|
||||
}
|
||||
|
||||
return s.loggingMiddleware(mux)
|
||||
func registerOther() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
pager := page.New()
|
||||
|
||||
_ = handlehome.New(mux, pager)
|
||||
|
||||
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
||||
|
||||
translator := translate.New()
|
||||
return translator.Middleware(mux)
|
||||
}
|
||||
|
||||
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
|
||||
)
|
||||
|
||||
// registerStatic registers all the static files.
|
||||
func registerStatic(mux *http.ServeMux) {
|
||||
fileSystem := web.GetStaticFS()
|
||||
|
||||
// we still use go's fileServer to avoid unnecessary implementation
|
||||
fileServer := http.FileServer(http.FS(fileSystem))
|
||||
|
||||
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
path = "/" + path
|
||||
|
||||
slog.Info("registering file", "path", path)
|
||||
|
||||
mux.Handle(path, fileServer)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("could not register static files", "err", err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user