diff options
-rw-r--r-- | frontend/templates/blocks/search.gotmpl | 2 | ||||
-rw-r--r-- | internal/config/config.go | 6 | ||||
-rw-r--r-- | internal/server/server.go | 34 |
3 files changed, 24 insertions, 18 deletions
diff --git a/frontend/templates/blocks/search.gotmpl b/frontend/templates/blocks/search.gotmpl index 0be63c1..e9d69b6 100644 --- a/frontend/templates/blocks/search.gotmpl +++ b/frontend/templates/blocks/search.gotmpl @@ -3,7 +3,7 @@ {{- define "main" }} <form id="search" action="results"> - <label for="query">{{ sourceName .Source }} option search</label> + <label for="query">{{ .Source.Name }} option search</label> <fieldset> <input id="query" name="query" type="search" value="{{ .Query }}" /> <button>Search</button> diff --git a/internal/config/config.go b/internal/config/config.go index 5801847..e381e61 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -30,7 +30,7 @@ type Config struct { DataPath string `toml:"data-path"` CSP CSP `toml:"content-security-policy"` Headers map[string]string - Sources map[string]importer.Source + Sources map[string]*importer.Source } var defaultConfig = Config{ @@ -41,7 +41,7 @@ var defaultConfig = Config{ Headers: map[string]string{ "x-content-type-options": "nosniff", }, - Sources: map[string]importer.Source{ + Sources: map[string]*importer.Source{ "nixos": { Name: "NixOS", Enable: true, @@ -114,7 +114,7 @@ func GetConfig(filename string) (*Config, error) { } } - maps.DeleteFunc(config.Sources, func(_ string, v importer.Source) bool { + maps.DeleteFunc(config.Sources, func(_ string, v *importer.Source) bool { return !v.Enable }) 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, |