From ef7c33bb81d908d0f61921806e6383200a543d00 Mon Sep 17 00:00:00 2001 From: Wessie Date: Sat, 21 Dec 2024 21:29:59 +0100 Subject: [PATCH] search/bleve: make the package buildable on windows --- search/bleve/{main.go => index.go} | 54 ----------------- search/bleve/main_linux.go | 96 ++++++++++++++++++++++++++++++ search/bleve/server.go | 33 ---------- 3 files changed, 96 insertions(+), 87 deletions(-) rename search/bleve/{main.go => index.go} (87%) create mode 100644 search/bleve/main_linux.go diff --git a/search/bleve/main.go b/search/bleve/index.go similarity index 87% rename from search/bleve/main.go rename to search/bleve/index.go index 95323235..cda33822 100644 --- a/search/bleve/main.go +++ b/search/bleve/index.go @@ -5,15 +5,11 @@ import ( "fmt" "net/http" "strings" - "syscall" "time" radio "github.com/R-a-dio/valkyrie" "github.com/R-a-dio/valkyrie/config" "github.com/R-a-dio/valkyrie/errors" - "github.com/R-a-dio/valkyrie/search" - "github.com/R-a-dio/valkyrie/util" - "github.com/Wessie/fdstore" "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/mapping" "github.com/blevesearch/bleve/v2/search/query" @@ -23,52 +19,6 @@ import ( "go.opentelemetry.io/otel/attribute" ) -func Execute(ctx context.Context, cfg config.Config) error { - const op errors.Op = "search/bleve.Execute" - - idx, err := NewIndex(cfg.Conf().Search.IndexPath) - if err != nil { - return errors.E(op, err) - } - defer idx.index.Close() - - srv, err := NewServer(ctx, idx) - if err != nil { - return errors.E(op, err) - } - defer srv.Close() - - fdstorage := fdstore.NewStoreListenFDs() - - endpoint := cfg.Conf().Search.Endpoint.URL() - ln, _, err := util.RestoreOrListen(fdstorage, "bleve", "tcp", endpoint.Host) - if err != nil { - return errors.E(op, err) - } - - errCh := make(chan error, 1) - go func() { - errCh <- srv.Serve(ln) - }() - - select { - case <-ctx.Done(): - return srv.Close() - case <-util.Signal(syscall.SIGUSR2): - zerolog.Ctx(ctx).Info().Msg("SIGUSR2 received") - err := fdstorage.AddListener(ln, "bleve", nil) - if err != nil { - zerolog.Ctx(ctx).Error().Err(err).Msg("failed to store listener") - } - if err = fdstorage.Send(); err != nil { - zerolog.Ctx(ctx).Error().Err(err).Msg("failed to send store") - } - return srv.Close() - case err := <-errCh: - return err - } -} - type indexSong struct { // main fields we're searching through Title string `bleve:"title"` @@ -192,10 +142,6 @@ func (b *index) Delete(ctx context.Context, tids []radio.TrackID) error { return nil } -func init() { - search.Register("bleve", true, Open) -} - func mixedTextMapping() *mapping.FieldMapping { m := bleve.NewTextFieldMapping() m.Analyzer = "radio" diff --git a/search/bleve/main_linux.go b/search/bleve/main_linux.go new file mode 100644 index 00000000..aab96ca8 --- /dev/null +++ b/search/bleve/main_linux.go @@ -0,0 +1,96 @@ +package bleve + +import ( + "context" + "net/http" + "syscall" + + "github.com/R-a-dio/valkyrie/config" + "github.com/R-a-dio/valkyrie/errors" + "github.com/R-a-dio/valkyrie/search" + "github.com/R-a-dio/valkyrie/util" + "github.com/R-a-dio/valkyrie/website" + "github.com/Wessie/fdstore" + "github.com/go-chi/chi/v5/middleware" + "github.com/rs/zerolog" + "github.com/rs/zerolog/hlog" +) + +func Execute(ctx context.Context, cfg config.Config) error { + const op errors.Op = "search/bleve.Execute" + + idx, err := NewIndex(cfg.Conf().Search.IndexPath) + if err != nil { + return errors.E(op, err) + } + defer idx.index.Close() + + srv, err := NewServer(ctx, idx) + if err != nil { + return errors.E(op, err) + } + defer srv.Close() + + fdstorage := fdstore.NewStoreListenFDs() + + endpoint := cfg.Conf().Search.Endpoint.URL() + ln, _, err := util.RestoreOrListen(fdstorage, "bleve", "tcp", endpoint.Host) + if err != nil { + return errors.E(op, err) + } + + errCh := make(chan error, 1) + go func() { + errCh <- srv.Serve(ln) + }() + + select { + case <-ctx.Done(): + return srv.Close() + case <-util.Signal(syscall.SIGUSR2): + zerolog.Ctx(ctx).Info().Msg("SIGUSR2 received") + err := fdstorage.AddListener(ln, "bleve", nil) + if err != nil { + zerolog.Ctx(ctx).Error().Err(err).Msg("failed to store listener") + } + if err = fdstorage.Send(); err != nil { + zerolog.Ctx(ctx).Error().Err(err).Msg("failed to send store") + } + return srv.Close() + case err := <-errCh: + return err + } +} + +func init() { + search.Register("bleve", true, Open) +} + +func NewServer(ctx context.Context, idx *index) (*http.Server, error) { + logger := zerolog.Ctx(ctx) + r := website.NewRouter() + r.Use(middleware.Recoverer) + r.Use( + hlog.NewHandler(*logger), + hlog.RemoteAddrHandler("ip"), + hlog.UserAgentHandler("user_agent"), + hlog.RequestIDHandler("req_id", "Request-Id"), + hlog.URLHandler("url"), + hlog.MethodHandler("method"), + hlog.ProtoHandler("protocol"), + hlog.CustomHeaderHandler("is_htmx", "Hx-Request"), + hlog.AccessHandler(zerologLoggerFunc), + ) + + r.Get(searchPath, SearchHandler(idx)) + r.Get(searchJSONPath, SearchJSONHandler(idx)) + r.Get(extendedPath, ExtendedSearchHandler(idx)) + r.Get(indexStatsPath, IndexStatsHandler(idx)) + r.Post(deletePath, DeleteHandler(idx)) + r.Post(updatePath, UpdateHandler(idx)) + + srv := &http.Server{ + Handler: r, + } + return srv, nil +} diff --git a/search/bleve/server.go b/search/bleve/server.go index 10a2afdc..3e7873ec 100644 --- a/search/bleve/server.go +++ b/search/bleve/server.go @@ -1,7 +1,6 @@ package bleve import ( - "context" "encoding/json" "io" "net/http" @@ -12,10 +11,7 @@ import ( radio "github.com/R-a-dio/valkyrie" "github.com/R-a-dio/valkyrie/errors" "github.com/R-a-dio/valkyrie/util/pool" - "github.com/R-a-dio/valkyrie/website" "github.com/blevesearch/bleve/v2" - "github.com/go-chi/chi/v5/middleware" - "github.com/rs/zerolog" "github.com/rs/zerolog/hlog" "github.com/vmihailenco/msgpack/v4" ) @@ -56,35 +52,6 @@ func zerologLoggerFunc(r *http.Request, status, size int, duration time.Duration Msg("http request") } -func NewServer(ctx context.Context, idx *index) (*http.Server, error) { - logger := zerolog.Ctx(ctx) - r := website.NewRouter() - r.Use(middleware.Recoverer) - r.Use( - hlog.NewHandler(*logger), - hlog.RemoteAddrHandler("ip"), - hlog.UserAgentHandler("user_agent"), - hlog.RequestIDHandler("req_id", "Request-Id"), - hlog.URLHandler("url"), - hlog.MethodHandler("method"), - hlog.ProtoHandler("protocol"), - hlog.CustomHeaderHandler("is_htmx", "Hx-Request"), - hlog.AccessHandler(zerologLoggerFunc), - ) - - r.Get(searchPath, SearchHandler(idx)) - r.Get(searchJSONPath, SearchJSONHandler(idx)) - r.Get(extendedPath, ExtendedSearchHandler(idx)) - r.Get(indexStatsPath, IndexStatsHandler(idx)) - r.Post(deletePath, DeleteHandler(idx)) - r.Post(updatePath, UpdateHandler(idx)) - - srv := &http.Server{ - Handler: r, - } - return srv, nil -} - func DeleteHandler(idx *index) http.HandlerFunc { const op errors.Op = "search/bleve.DeleteHandler"