diff options
author | Alan Pearce | 2024-05-09 16:47:41 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-09 19:27:55 +0200 |
commit | e062ca72b222b890e345548bd8422d5df98e9fef (patch) | |
tree | 89f52ebfdb1fb8069e6323d9dde42f5491dad5d1 /internal/server | |
parent | 967f6fdf5c1693d3aa27079b3ae28768fb7356c6 (diff) | |
download | searchix-e062ca72b222b890e345548bd8422d5df98e9fef.tar.lz searchix-e062ca72b222b890e345548bd8422d5df98e9fef.tar.zst searchix-e062ca72b222b890e345548bd8422d5df98e9fef.zip |
feat: import sources from configuration in go code and index options
Diffstat (limited to 'internal/server')
-rw-r--r-- | internal/server/server.go | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/internal/server/server.go b/internal/server/server.go index b794f05..5def347 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -20,6 +20,7 @@ import ( "searchix/internal/options" "searchix/internal/search" + "github.com/blevesearch/bleve/v2" "github.com/getsentry/sentry-go" sentryhttp "github.com/getsentry/sentry-go/http" "github.com/osdevisnot/sorvor/pkg/livereload" @@ -57,17 +58,18 @@ type Server struct { const jsSnippet = template.HTML(livereload.JsSnippet) // #nosec G203 type TemplateData struct { - LiveReload template.HTML - Source string - Query string - Results bool + LiveReload template.HTML + Source string + Query string + Results bool + SourceResult *bleve.SearchResult } type ResultData[T options.NixOption] struct { TemplateData Query string ResultsPerPage int - Results *search.Result[T] + Results *search.Result Prev string Next string } @@ -77,24 +79,6 @@ 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 makeIndex(source string, filename string) { - var err error - slog.Info("loading index", "index", source) - index[source], err = search.New(filename) - slog.Info("loaded index", "index", source) - if err != nil { - log.Fatalf("could not build search index, error: %#v", err) - } -} - func New(runtimeConfig *Config) (*Server, error) { var err error config, err = cfg.GetConfig() @@ -102,6 +86,13 @@ func New(runtimeConfig *Config) (*Server, error) { return nil, errors.WithMessage(err, "error parsing configuration file") } + slog.Debug("loading index") + index, err := search.Open(config.DataPath) + slog.Debug("loaded index") + if err != nil { + log.Fatalf("could not open search index, error: %#v", err) + } + env := "development" if runtimeConfig.Production { env = "production" @@ -138,19 +129,34 @@ 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") - if index[source] == nil { + ctx, cancel := context.WithTimeout(context.Background(), getSourceTimeout) + defer cancel() + + sourceResult, err := index.GetSource(ctx, source) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + + return + } + + if sourceResult.Total == 0 { http.Error(w, "Unknown source", http.StatusNotFound) return } - err := templates["search"].Execute(w, TemplateData{ - LiveReload: jsSnippet, - Source: source, + + err = templates["search"].Execute(w, TemplateData{ + LiveReload: jsSnippet, + Source: source, + SourceResult: sourceResult, }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) + + return } }) @@ -159,7 +165,7 @@ func New(runtimeConfig *Config) (*Server, error) { source := r.PathValue("source") ctx, cancel := context.WithTimeoutCause(r.Context(), timeout, errors.New("timeout")) defer cancel() - if index[source] == nil { + if index == nil { http.Error(w, "Unknown source", http.StatusNotFound) return @@ -173,7 +179,7 @@ func New(runtimeConfig *Config) (*Server, error) { http.Error(w, "Bad query string", http.StatusBadRequest) } } - results, err := index[source].Search(ctx, qs, (page-1)*search.ResultsPerPage) + results, err := index.Search(ctx, source, qs, (page-1)*search.ResultsPerPage) if err != nil { if err == context.DeadlineExceeded { http.Error(w, "Search timed out", http.StatusInternalServerError) @@ -238,12 +244,6 @@ func New(runtimeConfig *Config) (*Server, error) { mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("frontend/static")))) - go func() { - for source, filename := range sourceFileName { - makeIndex(source, filename) - } - }() - if runtimeConfig.LiveReload { applyDevModeOverrides(config) liveReload := livereload.New() |