diff options
author | Alan Pearce | 2024-05-07 21:32:36 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-07 21:32:36 +0200 |
commit | 212e5cf6621c99e46dbb37c860dab8938968bb19 (patch) | |
tree | abd06307e406290c17f453360e92dc6462f5533e /internal/server | |
parent | 32c4f1ddd704984dad79ad059619b127dcc7de2f (diff) | |
download | searchix-212e5cf6621c99e46dbb37c860dab8938968bb19.tar.lz searchix-212e5cf6621c99e46dbb37c860dab8938968bb19.tar.zst searchix-212e5cf6621c99e46dbb37c860dab8938968bb19.zip |
feat: search multiple sources
Diffstat (limited to 'internal/server')
-rw-r--r-- | internal/server/server.go | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/internal/server/server.go b/internal/server/server.go index e3cc82d..3b85061 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -72,10 +72,23 @@ func applyDevModeOverrides(config *cfg.Config) { config.CSP.ConnectSrc = slices.Insert(config.CSP.ConnectSrc, 0, "'self'") } +var index = map[string]*search.Index[options.NixOption]{} + +var sourceFileName = map[string]string{ + "darwin": "darwin-options", + "home-manager": "home-manager-options", + "nixos": "nixos-options-nixos-unstable", +} + func init() { - err := search.Index() - if err != nil { - log.Fatalf("could not build search index, error: %#v", err) + var err error + + for source, filename := range sourceFileName { + index[source], err = search.New(filename) + if err != nil { + log.Fatalf("could not build search index, error: %#v", err) + } + } } @@ -122,19 +135,19 @@ func New(runtimeConfig *Config) (*Server, error) { } }) - mux.HandleFunc("/search/{source}", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/options/{source}/search", func(w http.ResponseWriter, r *http.Request) { 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: + if index[source] == nil { http.Error(w, "Unknown source", http.StatusNotFound) + + return + } + err := templates["search"].Execute(w, TemplateData{ + LiveReload: jsSnippet, + Source: source, + }) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) } }) @@ -143,7 +156,12 @@ func New(runtimeConfig *Config) (*Server, error) { 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")) + if index[source] == nil { + http.Error(w, "Unknown source", http.StatusNotFound) + + return + } + results, err := index[source].Search(ctx, r.URL.Query().Get("query")) if err != nil { if err == context.DeadlineExceeded { http.Error(w, "Search timed out", http.StatusInternalServerError) |