diff options
-rw-r--r-- | frontend/templates/blocks/search.gotmpl | 10 | ||||
-rw-r--r-- | frontend/templates/index.gotmpl | 2 | ||||
-rw-r--r-- | internal/server/server.go | 29 | ||||
-rw-r--r-- | 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" }} - <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 }} 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 @@ <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> 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) { |