From 830e14f372fcff094448ac8528386ab381f6f35c Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 9 May 2024 19:27:15 +0200 Subject: refactor: pass Source object to templates --- internal/server/server.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'internal/server') 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, -- cgit 1.4.1