about summary refs log tree commit diff stats
path: root/internal/server/mux.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-12 21:44:57 +0200
committerAlan Pearce2024-05-12 21:44:57 +0200
commit7077a3748fa545e8dee83d4f3464c55b4b459183 (patch)
tree2e6fda93ba9a1a81ea23bd2b4902f6abbbe2e478 /internal/server/mux.go
parentfe79b4509dd580542fa28186673826df603677bb (diff)
downloadsearchix-7077a3748fa545e8dee83d4f3464c55b4b459183.tar.lz
searchix-7077a3748fa545e8dee83d4f3464c55b4b459183.tar.zst
searchix-7077a3748fa545e8dee83d4f3464c55b4b459183.zip
feat: allow adding extra HTML to the body via configuration
Diffstat (limited to 'internal/server/mux.go')
-rw-r--r--internal/server/mux.go35
1 files changed, 18 insertions, 17 deletions
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)