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

@ -21,10 +21,10 @@ function throttledDebounce(callback, maxDebounceTimes, debounceDelay) {
};
async function fetchPageContent(pageSlug) {
async function fetchPageContent(pageData) {
// TODO: handle non 200 status codes/time outs
// TODO: add retries
const response = await fetch(`/api/pages/${pageSlug}/content/`);
const response = await fetch(`${pageData.baseUrl}/api/pages/${pageData.slug}/content/`);
const content = await response.text();
return content;
@ -336,7 +336,7 @@ function setupCollapsibleGrids() {
async function setupPage() {
const pageElement = document.getElementById("page");
const pageContentElement = document.getElementById("page-content");
const pageContent = await fetchPageContent(pageData.slug);
const pageContent = await fetchPageContent(pageData);
pageContentElement.innerHTML = pageContent;

View File

@ -6,6 +6,7 @@
<script>
const pageData = {
slug: "{{ .Page.Slug }}",
baseUrl: "{{ .App.Config.Server.BaseUrl }}",
};
</script>
{{ end }}
@ -14,13 +15,13 @@
{{ define "document-head-after" }}
{{ template "page-style-overrides.gotmpl" . }}
{{ if ne "" .App.Config.Theme.CustomCSSFile }}
<link rel="stylesheet" href="{{ .App.Config.Theme.CustomCSSFile }}?v={{ .App.Config.Server.StartedAt.Unix }}">
<link rel="stylesheet" href="{{ .App.Config.Server.BaseUrl }}{{ .App.Config.Theme.CustomCSSFile }}?v={{ .App.Config.Server.StartedAt.Unix }}">
{{ end }}
{{ end }}
{{ define "navigation-links" }}
{{ range .App.Config.Pages }}
<a href="/{{ .Slug }}" class="nav-item{{ if eq .Slug $.Page.Slug }} nav-item-current{{ end }}">{{ .Title }}</a>
<a href="{{ $.App.Config.Server.BaseUrl }}/{{ .Slug }}" class="nav-item{{ if eq .Slug $.Page.Slug }} nav-item-current{{ end }}">{{ .Title }}</a>
{{ end }}
{{ end }}

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()
}