about summary refs log tree commit diff stats
path: root/internal/server
diff options
context:
space:
mode:
authorAlan Pearce2024-05-14 21:16:23 +0200
committerAlan Pearce2024-05-14 21:45:02 +0200
commit1a5c82e2d08accb6330c4164ab987b87157b10ed (patch)
tree48d6a913809e60b1a0cc28a8b67413ffaf979f97 /internal/server
parentf08094f23aa4dc7d63a7c98b4429d88005fe3a2d (diff)
downloadsearchix-1a5c82e2d08accb6330c4164ab987b87157b10ed.tar.lz
searchix-1a5c82e2d08accb6330c4164ab987b87157b10ed.tar.zst
searchix-1a5c82e2d08accb6330c4164ab987b87157b10ed.zip
build: bake git hash into build for headers and --version
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/headers.go1
-rw-r--r--internal/server/mux.go14
2 files changed, 15 insertions, 0 deletions
diff --git a/internal/server/headers.go b/internal/server/headers.go
index 4fb9efd..562fccc 100644
--- a/internal/server/headers.go
+++ b/internal/server/headers.go
@@ -11,6 +11,7 @@ func AddHeadersMiddleware(next http.Handler, config *cfg.Config) http.Handler {
 			w.Header().Add(h, v)
 		}
 		w.Header().Add("Content-Security-Policy", config.Web.ContentSecurityPolicy.String())
+		w.Header().Add("Server", "searchix/"+cfg.ShortSHA)
 
 		next.ServeHTTP(w, r)
 	})
diff --git a/internal/server/mux.go b/internal/server/mux.go
index 582d154..0f32507 100644
--- a/internal/server/mux.go
+++ b/internal/server/mux.go
@@ -35,6 +35,11 @@ type HTTPError struct {
 
 const jsSnippet = template.HTML(livereload.JsSnippet) // #nosec G203
 
+type VersionInfo struct {
+	ShortSHA  string
+	CommitSHA string
+}
+
 type TemplateData struct {
 	Sources       map[string]*config.Source
 	Source        config.Source
@@ -42,6 +47,7 @@ type TemplateData struct {
 	Results       bool
 	SourceResult  *bleve.SearchResult
 	ExtraBodyHTML template.HTML
+	Version       VersionInfo
 }
 
 type ResultData[T options.NixOption] struct {
@@ -53,6 +59,11 @@ type ResultData[T options.NixOption] struct {
 	Next           string
 }
 
+var versionInfo = &VersionInfo{
+	ShortSHA:  config.ShortSHA,
+	CommitSHA: config.CommitSHA,
+}
+
 func applyDevModeOverrides(config *config.Config) {
 	if len(config.Web.ContentSecurityPolicy.ScriptSrc) == 0 {
 		config.Web.ContentSecurityPolicy.ScriptSrc = config.Web.ContentSecurityPolicy.DefaultSrc
@@ -93,6 +104,7 @@ func NewMux(
 		indexData := TemplateData{
 			ExtraBodyHTML: config.Web.ExtraBodyHTML,
 			Sources:       config.Importer.Sources,
+			Version:       *versionInfo,
 		}
 		err := templates["index"].ExecuteTemplate(w, "index.gotmpl", indexData)
 		if err != nil {
@@ -140,6 +152,7 @@ func NewMux(
 					ExtraBodyHTML: config.Web.ExtraBodyHTML,
 					Source:        *source,
 					Sources:       config.Importer.Sources,
+					Version:       *versionInfo,
 				},
 				ResultsPerPage: search.ResultsPerPage,
 				Query:          qs,
@@ -200,6 +213,7 @@ func NewMux(
 				Sources:       config.Importer.Sources,
 				Source:        *source,
 				SourceResult:  sourceResult,
+				Version:       *versionInfo,
 			})
 			if err != nil {
 				http.Error(w, err.Error(), http.StatusInternalServerError)