diff options
author | Alan Pearce | 2024-05-12 21:44:57 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-12 21:44:57 +0200 |
commit | 7077a3748fa545e8dee83d4f3464c55b4b459183 (patch) | |
tree | 2e6fda93ba9a1a81ea23bd2b4902f6abbbe2e478 /internal | |
parent | fe79b4509dd580542fa28186673826df603677bb (diff) | |
download | searchix-7077a3748fa545e8dee83d4f3464c55b4b459183.tar.lz searchix-7077a3748fa545e8dee83d4f3464c55b4b459183.tar.zst searchix-7077a3748fa545e8dee83d4f3464c55b4b459183.zip |
feat: allow adding extra HTML to the body via configuration
Diffstat (limited to 'internal')
-rw-r--r-- | internal/config/config.go | 10 | ||||
-rw-r--r-- | internal/server/mux.go | 35 |
2 files changed, 24 insertions, 21 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 9880b3a..71b56be 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,6 +1,7 @@ package config import ( + "html/template" "log/slog" "maps" "net/url" @@ -27,10 +28,11 @@ func (u *URL) UnmarshalText(text []byte) (err error) { } type Config struct { - DataPath string `toml:"data-path"` - CSP CSP `toml:"content-security-policy"` - Headers map[string]string - Sources map[string]*importer.Source + DataPath string `toml:"data-path"` + CSP CSP `toml:"content-security-policy"` + ExtraBodyHTML template.HTML `toml:"extra-body-html"` + Headers map[string]string + Sources map[string]*importer.Source } var defaultConfig = Config{ diff --git a/internal/server/mux.go b/internal/server/mux.go index d98d415..ac128ce 100644 --- a/internal/server/mux.go +++ b/internal/server/mux.go @@ -50,12 +50,12 @@ type HTTPError struct { const jsSnippet = template.HTML(livereload.JsSnippet) // #nosec G203 type TemplateData struct { - LiveReload template.HTML - Sources map[string]*importer.Source - Source importer.Source - Query string - Results bool - SourceResult *bleve.SearchResult + Sources map[string]*importer.Source + Source importer.Source + Query string + Results bool + SourceResult *bleve.SearchResult + ExtraBodyHTML template.HTML } type ResultData[T options.NixOption] struct { @@ -109,11 +109,11 @@ func NewMux(runtimeConfig *Config) (*http.ServeMux, error) { top := http.NewServeMux() mux := http.NewServeMux() - indexData := TemplateData{ - LiveReload: jsSnippet, - Sources: config.Sources, - } mux.HandleFunc("/{$}", func(w http.ResponseWriter, _ *http.Request) { + indexData := TemplateData{ + ExtraBodyHTML: config.ExtraBodyHTML, + Sources: config.Sources, + } err := templates["index"].ExecuteTemplate(w, "index.gotmpl", indexData) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -157,9 +157,9 @@ func NewMux(runtimeConfig *Config) (*http.ServeMux, error) { tdata := ResultData[options.NixOption]{ TemplateData: TemplateData{ - LiveReload: jsSnippet, - Source: *source, - Sources: config.Sources, + ExtraBodyHTML: config.ExtraBodyHTML, + Source: *source, + Sources: config.Sources, }, ResultsPerPage: search.ResultsPerPage, Query: qs, @@ -216,10 +216,10 @@ func NewMux(runtimeConfig *Config) (*http.ServeMux, error) { } err = templates["search"].Execute(w, TemplateData{ - LiveReload: jsSnippet, - Sources: config.Sources, - Source: *source, - SourceResult: sourceResult, + ExtraBodyHTML: config.ExtraBodyHTML, + Sources: config.Sources, + Source: *source, + SourceResult: sourceResult, }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -233,6 +233,7 @@ func NewMux(runtimeConfig *Config) (*http.ServeMux, error) { if runtimeConfig.LiveReload { applyDevModeOverrides(config) + config.ExtraBodyHTML = jsSnippet liveReload := livereload.New() liveReload.Start() top.Handle("/livereload", liveReload) |