From 32c4f1ddd704984dad79ad059619b127dcc7de2f Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 7 May 2024 21:04:13 +0200 Subject: style: show pages for different sources --- frontend/templates/blocks/search.gotmpl | 10 ++++++---- frontend/templates/index.gotmpl | 2 ++ internal/server/server.go | 29 +++++++++++++++++++++-------- internal/server/templates.go | 12 ++++++++++++ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/frontend/templates/blocks/search.gotmpl b/frontend/templates/blocks/search.gotmpl index 5482e6b..b03410c 100644 --- a/frontend/templates/blocks/search.gotmpl +++ b/frontend/templates/blocks/search.gotmpl @@ -2,10 +2,12 @@ {{- template "js" . }} {{- define "main" }} - - {{- with .Results }} {{ block "results" . }}{{ end }} diff --git a/frontend/templates/index.gotmpl b/frontend/templates/index.gotmpl index 77e1265..d4a46da 100644 --- a/frontend/templates/index.gotmpl +++ b/frontend/templates/index.gotmpl @@ -12,6 +12,8 @@
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") diff --git a/internal/server/templates.go b/internal/server/templates.go index 8755e36..17b14ba 100644 --- a/internal/server/templates.go +++ b/internal/server/templates.go @@ -32,6 +32,18 @@ var templateFuncs = template.FuncMap{ return template.HTML(out.String()) // #nosec G203 }, + "sourceName": func(input string) string { + switch input { + case "nixos": + return "NixOS" + case "darwin": + return "Darwin" + case "home-manager": + return "Home Manager" + } + + return input + }, } func loadTemplate(layoutFile string, filename string) (*template.Template, error) { -- cgit 1.4.1