all repos — searchix @ 32c4f1ddd704984dad79ad059619b127dcc7de2f

Search engine for NixOS, nix-darwin, home-manager and NUR users

style: show pages for different sources

Alan Pearce
commit

32c4f1ddd704984dad79ad059619b127dcc7de2f

parent

48b22d9ad78e4f3bdb6ca5bbd5ab642b1f991715

M frontend/templates/blocks/search.gotmplfrontend/templates/blocks/search.gotmpl
@@ -2,10 +2,12 @@ {{- template "main" . }}
{{- template "js" . }} {{- define "main" }} - <label for="query">Search</label> - <form id="search" action="/options/results"> - <input id="query" name="query" type="search" value="{{ .Query }}" /> - <button>Search</button> + <form id="search" action="/options/{{ .Source }}/results"> + <label for="query">{{ sourceName .Source }} option search</label> + <fieldset> + <input id="query" name="query" type="search" value="{{ .Query }}" /> + <button>Search</button> + </fieldset> </form> {{- with .Results }} {{ block "results" . }}{{ end }}
M frontend/templates/index.gotmplfrontend/templates/index.gotmpl
@@ -12,6 +12,8 @@ <header>
<nav> <h1><a href="/">Searchix</a></h1> <a href="/search/nixos">NixOS</a> + <a href="/search/darwin">Darwin</a> + <a href="/search/home-manager">Home Manager</a> </nav> </header> <main>
M internal/server/server.gointernal/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 @@ }
}) 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 @@ slog.Error("search error", "error", err)
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")
M internal/server/templates.gointernal/server/templates.go
@@ -32,6 +32,18 @@ }
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) {