diff options
author | Alan Pearce | 2024-05-07 17:33:06 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-07 17:36:06 +0200 |
commit | c15b142b18dcdc7f5ab6d5f1afca8ae1696692cc (patch) | |
tree | 6bda157426a7eb79e3299839eeb43230db99f206 /internal/server/server.go | |
parent | 6c8c36b2c34bc375859230ddc13c5071274f60b4 (diff) | |
download | searchix-c15b142b18dcdc7f5ab6d5f1afca8ae1696692cc.tar.lz searchix-c15b142b18dcdc7f5ab6d5f1afca8ae1696692cc.tar.zst searchix-c15b142b18dcdc7f5ab6d5f1afca8ae1696692cc.zip |
feat: search one set of options
Diffstat (limited to 'internal/server/server.go')
-rw-r--r-- | internal/server/server.go | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/internal/server/server.go b/internal/server/server.go index a6f22e2..3f87b9d 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -2,7 +2,6 @@ package server import ( "context" - "encoding/json" "fmt" "html/template" "io" @@ -17,6 +16,7 @@ import ( cfg "searchix/internal/config" "searchix/internal/options" + "searchix/internal/search" "github.com/getsentry/sentry-go" sentryhttp "github.com/getsentry/sentry-go/http" @@ -59,10 +59,10 @@ type TemplateData struct { Query string } -type OptionResultData struct { +type ResultData[T options.NixOption] struct { TemplateData Query string - Results options.NixOptions + Results *search.Result[T] } func applyDevModeOverrides(config *cfg.Config) { @@ -70,6 +70,13 @@ func applyDevModeOverrides(config *cfg.Config) { config.CSP.ConnectSrc = slices.Insert(config.CSP.ConnectSrc, 0, "'self'") } +func init() { + err := search.Index() + 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() @@ -113,23 +120,25 @@ func New(runtimeConfig *Config) (*Server, error) { } }) - var nixosOptions = options.NixOptions{} - jsonFile, err := os.ReadFile(path.Join(config.DataPath, "test.json")) - if err != nil { - slog.Error(fmt.Sprintf("error reading json file: %v", err)) - } - err = json.Unmarshal(jsonFile, &nixosOptions) - if err != nil { - slog.Error(fmt.Sprintf("error parsing json file: %v", err)) - } - + timeout := 1 * time.Second mux.HandleFunc("/options/results", func(w http.ResponseWriter, r *http.Request) { - tdata := OptionResultData{ + ctx, cancel := context.WithTimeoutCause(r.Context(), timeout, errors.New("timeout")) + defer cancel() + results, err := search.Search(ctx, r.URL.Query().Get("query")) + if err != nil { + if err == context.DeadlineExceeded { + http.Error(w, "Search timed out", http.StatusInternalServerError) + + return + } + slog.Error("search error", "error", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + } + tdata := ResultData[options.NixOption]{ TemplateData: indexData, Query: r.URL.Query().Get("query"), - Results: nixosOptions, + Results: results, } - var err error if r.Header.Get("Fetch") == "true" { w.Header().Add("Content-Type", "text/html; charset=utf-8") err = templates["options"].Execute(w, tdata) @@ -137,7 +146,7 @@ func New(runtimeConfig *Config) (*Server, error) { err = templates["options"].ExecuteTemplate(w, "index.gotmpl", tdata) } if err != nil { - slog.Error(fmt.Sprintf("template error: %v", err)) + slog.Error("template error", "template", "options", "error", err) http.Error(w, err.Error(), http.StatusInternalServerError) } }) |