62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"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"
|
|
"github.com/studio-b12/gowebdav"
|
|
)
|
|
|
|
func (s *Server) RegisterRoutes() (h http.Handler) {
|
|
mux := http.NewServeMux()
|
|
|
|
registerStatic(mux)
|
|
mux.Handle("/", registerOther())
|
|
|
|
h = mux
|
|
h = templer.Middleware(h)
|
|
h = s.loggingMiddleware(h)
|
|
return h
|
|
}
|
|
|
|
func registerOther() (h http.Handler) {
|
|
mux := http.NewServeMux()
|
|
|
|
webdavClient := gowebdav.NewClient("https://cloud.schreifuchs.ch/remote.php/dav/files/test/website", "test", os.Getenv("TEST_PW"))
|
|
if err := webdavClient.Connect(); err != nil {
|
|
slog.Error("could not connect webdavClient", "err", err, "pw", os.Getenv("TEST_PW"))
|
|
}
|
|
|
|
pager := page.New(webdavClient)
|
|
|
|
_ = handlehome.New(mux, pager)
|
|
|
|
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
|
|
|
translator := translate.New()
|
|
|
|
h = mux
|
|
h = translator.Middleware(h)
|
|
h = cacheMiddleware(time.Second * 45)(h)
|
|
return h
|
|
}
|
|
|
|
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
next.ServeHTTP(w, r)
|
|
slog.Info("request",
|
|
"method", r.Method,
|
|
"path", r.URL.Path,
|
|
"duration", time.Since(start),
|
|
)
|
|
})
|
|
}
|