Refactor base-url & add documentation

This commit is contained in:
Svilen Markov
2024-08-05 14:08:16 +01:00
parent c2cdd0fa08
commit e1161b9227
4 changed files with 20 additions and 7 deletions

View File

@@ -24,7 +24,7 @@ function throttledDebounce(callback, maxDebounceTimes, debounceDelay) {
async function fetchPageContent(pageData) {
// TODO: handle non 200 status codes/time outs
// TODO: add retries
const response = await fetch(`${pageData.baseUrl}/api/pages/${pageData.slug}/content/`);
const response = await fetch(`${pageData.baseURL}/api/pages/${pageData.slug}/content/`);
const content = await response.text();
return content;

View File

@@ -6,22 +6,23 @@
<script>
const pageData = {
slug: "{{ .Page.Slug }}",
baseUrl: "{{ .App.Config.Server.BaseUrl }}",
baseURL: "{{ .App.Config.Server.BaseURL }}",
};
</script>
{{ end }}
{{ define "document-root-attrs" }}class="{{ if .App.Config.Theme.Light }}light-scheme {{ end }}{{ if ne "" .Page.Width }}page-width-{{ .Page.Width }}{{ end }}"{{ end }}
{{ define "document-head-after" }}
{{ template "page-style-overrides.gotmpl" . }}
{{ if ne "" .App.Config.Theme.CustomCSSFile }}
<link rel="stylesheet" href="{{ .App.Config.Server.BaseUrl }}{{ .App.Config.Theme.CustomCSSFile }}?v={{ .App.Config.Server.StartedAt.Unix }}">
<link rel="stylesheet" href="{{ .App.Config.Theme.CustomCSSFile }}?v={{ .App.Config.Server.StartedAt.Unix }}">
{{ end }}
{{ end }}
{{ define "navigation-links" }}
{{ range .App.Config.Pages }}
<a href="{{ $.App.Config.Server.BaseUrl }}/{{ .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

@@ -43,7 +43,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"`
AssetsHash string `yaml:"-"`
StartedAt time.Time `yaml:"-"` // used in custom css file
}
@@ -131,6 +131,14 @@ func NewApplication(config *Config) (*Application, error) {
}
}
config = &app.Config
if config.Server.BaseURL != "" &&
config.Theme.CustomCSSFile != "" &&
strings.HasPrefix(config.Theme.CustomCSSFile, "/assets/") {
config.Theme.CustomCSSFile = config.Server.BaseURL + config.Theme.CustomCSSFile
}
return app, nil
}
@@ -224,7 +232,7 @@ func (a *Application) HandleWidgetRequest(w http.ResponseWriter, r *http.Request
}
func (a *Application) AssetPath(asset string) string {
return "/static/" + a.Config.Server.AssetsHash + "/" + asset
return a.Config.Server.BaseURL + "/static/" + a.Config.Server.AssetsHash + "/" + asset
}
func (a *Application) Serve() error {
@@ -263,7 +271,7 @@ 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, "base url", a.Config.Server.BaseUrl)
slog.Info("Starting server", "host", a.Config.Server.Host, "port", a.Config.Server.Port, "base-url", a.Config.Server.BaseURL)
return server.ListenAndServe()
}