fix(serve): using paths without base url

This commit is contained in:
CremaLuca
2024-07-10 15:22:58 +02:00
parent a07fbfd7b7
commit 23d6d41148
3 changed files with 13 additions and 12 deletions

View File

@@ -41,7 +41,7 @@ type Server struct {
Host string `yaml:"host"`
Port uint16 `yaml:"port"`
AssetsPath string `yaml:"assets-path"`
BaseUrl string `yaml:"base-url"`
BaseUrl string `yaml:"base-url"`
StartedAt time.Time `yaml:"-"`
}
@@ -195,10 +195,10 @@ func (a *Application) Serve() error {
// TODO: add HTTPS support
mux := http.NewServeMux()
mux.HandleFunc(fmt.Sprintf("GET %s/{$}", a.Config.Server.BaseUrl), a.HandlePageRequest)
mux.HandleFunc(fmt.Sprintf("GET %s/{page}", a.Config.Server.BaseUrl), a.HandlePageRequest)
mux.HandleFunc(fmt.Sprintf("GET %s/api/pages/{page}/content/{$}", a.Config.Server.BaseUrl), a.HandlePageContentRequest)
mux.Handle(fmt.Sprintf("GET %s/static/{path...}", a.Config.Server.BaseUrl), http.StripPrefix(fmt.Sprintf("%s/static/", a.Config.Server.BaseUrl), FileServerWithCache(http.FS(assets.PublicFS), 2*time.Hour)))
mux.HandleFunc("GET /{$}", a.HandlePageRequest)
mux.HandleFunc("GET /{page}", a.HandlePageRequest)
mux.HandleFunc("GET /api/pages/{page}/content/{$}", a.HandlePageContentRequest)
mux.Handle("GET /static/{path...}", http.StripPrefix("/static/", FileServerWithCache(http.FS(assets.PublicFS), 2*time.Hour)))
if a.Config.Server.AssetsPath != "" {
absAssetsPath, err := filepath.Abs(a.Config.Server.AssetsPath)
@@ -209,7 +209,7 @@ func (a *Application) Serve() error {
slog.Info("Serving assets", "path", absAssetsPath)
assetsFS := FileServerWithCache(http.Dir(a.Config.Server.AssetsPath), 2*time.Hour)
mux.Handle(fmt.Sprintf("%s/assets/{path...}", a.Config.Server.BaseUrl), http.StripPrefix("/assets/", assetsFS))
mux.Handle("/assets/{path...}", http.StripPrefix("/assets/", assetsFS))
}
server := http.Server{
@@ -219,6 +219,6 @@ func (a *Application) Serve() error {
a.Config.Server.StartedAt = time.Now()
slog.Info("Starting server", "host", a.Config.Server.Host, "port", a.Config.Server.Port)
slog.Info("Starting server", "host", a.Config.Server.Host, "port", a.Config.Server.Port, "base url", a.Config.Server.BaseUrl)
return server.ListenAndServe()
}