about summary refs log tree commit diff stats
path: root/internal/server/server.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-07 21:04:13 +0200
committerAlan Pearce2024-05-07 21:04:13 +0200
commit32c4f1ddd704984dad79ad059619b127dcc7de2f (patch)
treef4d3c1bc0805ca715d54d80c74364d17d02793a2 /internal/server/server.go
parent48b22d9ad78e4f3bdb6ca5bbd5ab642b1f991715 (diff)
downloadsearchix-32c4f1ddd704984dad79ad059619b127dcc7de2f.tar.lz
searchix-32c4f1ddd704984dad79ad059619b127dcc7de2f.tar.zst
searchix-32c4f1ddd704984dad79ad059619b127dcc7de2f.zip
style: show pages for different sources
Diffstat (limited to 'internal/server/server.go')
-rw-r--r--internal/server/server.go29
1 files changed, 21 insertions, 8 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index c0fbfd0..e3cc82d 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -56,6 +56,7 @@ const jsSnippet = template.HTML(livereload.JsSnippet) // #nosec G203
 
 type TemplateData struct {
 	LiveReload template.HTML
+	Source     string
 	Query      string
 	Results    bool
 }
@@ -122,15 +123,24 @@ func New(runtimeConfig *Config) (*Server, error) {
 	})
 
 	mux.HandleFunc("/search/{source}", func(w http.ResponseWriter, r *http.Request) {
-		log.Println(r.PathValue("source"))
-		err := templates["search"].Execute(w, indexData)
-		if err != nil {
-			http.Error(w, err.Error(), http.StatusInternalServerError)
+		source := r.PathValue("source")
+		switch source {
+		case "nixos", "darwin", "home-manager":
+			err := templates["search"].Execute(w, TemplateData{
+				LiveReload: jsSnippet,
+				Source:     source,
+			})
+			if err != nil {
+				http.Error(w, err.Error(), http.StatusInternalServerError)
+			}
+		default:
+			http.Error(w, "Unknown source", http.StatusNotFound)
 		}
 	})
 
 	timeout := 1 * time.Second
-	mux.HandleFunc("/options/results", func(w http.ResponseWriter, r *http.Request) {
+	mux.HandleFunc("/options/{source}/results", func(w http.ResponseWriter, r *http.Request) {
+		source := r.PathValue("source")
 		ctx, cancel := context.WithTimeoutCause(r.Context(), timeout, errors.New("timeout"))
 		defer cancel()
 		results, err := search.Search(ctx, r.URL.Query().Get("query"))
@@ -144,9 +154,12 @@ func New(runtimeConfig *Config) (*Server, error) {
 			http.Error(w, err.Error(), http.StatusInternalServerError)
 		}
 		tdata := ResultData[options.NixOption]{
-			TemplateData: indexData,
-			Query:        r.URL.Query().Get("query"),
-			Results:      results,
+			TemplateData: TemplateData{
+				LiveReload: jsSnippet,
+				Source:     source,
+			},
+			Query:   r.URL.Query().Get("query"),
+			Results: results,
 		}
 		if r.Header.Get("Fetch") == "true" {
 			w.Header().Add("Content-Type", "text/html; charset=utf-8")