about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-09 19:27:15 +0200
committerAlan Pearce2024-05-09 19:31:36 +0200
commit830e14f372fcff094448ac8528386ab381f6f35c (patch)
treedbc0082cfc7d0791f83edc5b8ba3c82773ec67d5
parentc72f8dc787379f6544739c5c57a8241a14f6d2a0 (diff)
downloadsearchix-830e14f372fcff094448ac8528386ab381f6f35c.tar.lz
searchix-830e14f372fcff094448ac8528386ab381f6f35c.tar.zst
searchix-830e14f372fcff094448ac8528386ab381f6f35c.zip
refactor: pass Source object to templates
-rw-r--r--frontend/templates/blocks/search.gotmpl2
-rw-r--r--internal/config/config.go6
-rw-r--r--internal/server/server.go34
3 files changed, 24 insertions, 18 deletions
diff --git a/frontend/templates/blocks/search.gotmpl b/frontend/templates/blocks/search.gotmpl
index 0be63c1..e9d69b6 100644
--- a/frontend/templates/blocks/search.gotmpl
+++ b/frontend/templates/blocks/search.gotmpl
@@ -3,7 +3,7 @@
 
 {{- define "main" }}
   <form id="search" action="results">
-    <label for="query">{{ sourceName .Source }} option search</label>
+    <label for="query">{{ .Source.Name }} option search</label>
     <fieldset>
       <input id="query" name="query" type="search" value="{{ .Query }}" />
       <button>Search</button>
diff --git a/internal/config/config.go b/internal/config/config.go
index 5801847..e381e61 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -30,7 +30,7 @@ type Config struct {
 	DataPath string `toml:"data-path"`
 	CSP      CSP    `toml:"content-security-policy"`
 	Headers  map[string]string
-	Sources  map[string]importer.Source
+	Sources  map[string]*importer.Source
 }
 
 var defaultConfig = Config{
@@ -41,7 +41,7 @@ var defaultConfig = Config{
 	Headers: map[string]string{
 		"x-content-type-options": "nosniff",
 	},
-	Sources: map[string]importer.Source{
+	Sources: map[string]*importer.Source{
 		"nixos": {
 			Name:          "NixOS",
 			Enable:        true,
@@ -114,7 +114,7 @@ func GetConfig(filename string) (*Config, error) {
 		}
 	}
 
-	maps.DeleteFunc(config.Sources, func(_ string, v importer.Source) bool {
+	maps.DeleteFunc(config.Sources, func(_ string, v *importer.Source) bool {
 		return !v.Enable
 	})
 
diff --git a/internal/server/server.go b/internal/server/server.go
index 7ff174a..6b7f31c 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -17,6 +17,7 @@ import (
 	"time"
 
 	cfg "searchix/internal/config"
+	"searchix/internal/importer"
 	"searchix/internal/options"
 	"searchix/internal/search"
 
@@ -62,7 +63,7 @@ const jsSnippet = template.HTML(livereload.JsSnippet) // #nosec G203
 
 type TemplateData struct {
 	LiveReload   template.HTML
-	Source       string
+	Source       importer.Source
 	Query        string
 	Results      bool
 	SourceResult *bleve.SearchResult
@@ -134,26 +135,28 @@ func New(runtimeConfig *Config) (*Server, error) {
 
 	const getSourceTimeout = 1 * time.Second
 	mux.HandleFunc("/options/{source}/search", func(w http.ResponseWriter, r *http.Request) {
-		source := r.PathValue("source")
-		ctx, cancel := context.WithTimeout(context.Background(), getSourceTimeout)
-		defer cancel()
+		sourceKey := r.PathValue("source")
 
-		sourceResult, err := index.GetSource(ctx, source)
-		if err != nil {
-			http.Error(w, err.Error(), http.StatusInternalServerError)
+		source := config.Sources[sourceKey]
+		if source == nil {
+			http.Error(w, "Source not found", http.StatusNotFound)
 
 			return
 		}
 
-		if sourceResult.Total == 0 {
-			http.Error(w, "Unknown source", http.StatusNotFound)
+		ctx, cancel := context.WithTimeout(r.Context(), getSourceTimeout)
+		defer cancel()
+
+		sourceResult, err := index.GetSource(ctx, sourceKey)
+		if err != nil {
+			http.Error(w, err.Error(), http.StatusInternalServerError)
 
 			return
 		}
 
 		err = templates["search"].Execute(w, TemplateData{
 			LiveReload:   jsSnippet,
-			Source:       source,
+			Source:       *source,
 			SourceResult: sourceResult,
 		})
 		if err != nil {
@@ -165,14 +168,16 @@ func New(runtimeConfig *Config) (*Server, error) {
 
 	timeout := 1 * time.Second
 	mux.HandleFunc("/options/{source}/results", func(w http.ResponseWriter, r *http.Request) {
-		source := r.PathValue("source")
+		sourceKey := r.PathValue("source")
 		ctx, cancel := context.WithTimeoutCause(r.Context(), timeout, errors.New("timeout"))
 		defer cancel()
-		if index == nil {
+		source := config.Sources[sourceKey]
+		if source == nil {
 			http.Error(w, "Unknown source", http.StatusNotFound)
 
 			return
 		}
+
 		qs := r.URL.Query().Get("query")
 		pg := r.URL.Query().Get("page")
 		var page uint64 = 1
@@ -182,7 +187,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 				http.Error(w, "Bad query string", http.StatusBadRequest)
 			}
 		}
-		results, err := index.Search(ctx, source, qs, (page-1)*search.ResultsPerPage)
+		results, err := index.Search(ctx, sourceKey, qs, (page-1)*search.ResultsPerPage)
 		if err != nil {
 			if err == context.DeadlineExceeded {
 				http.Error(w, "Search timed out", http.StatusInternalServerError)
@@ -192,10 +197,11 @@ func New(runtimeConfig *Config) (*Server, error) {
 			slog.Error("search error", "error", err)
 			http.Error(w, err.Error(), http.StatusInternalServerError)
 		}
+
 		tdata := ResultData[options.NixOption]{
 			TemplateData: TemplateData{
 				LiveReload: jsSnippet,
-				Source:     source,
+				Source:     *source,
 			},
 			ResultsPerPage: search.ResultsPerPage,
 			Query:          qs,