From 0986e8699e87addccbef43c0ce30a466988fed15 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 9 May 2024 18:47:37 +0200 Subject: feat: enable index path to be configured --- import/main.go | 3 ++- internal/search/indexer.go | 29 +++++++++++++++++++++++------ internal/search/search.go | 6 +----- internal/server/server.go | 3 ++- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/import/main.go b/import/main.go index 90f2b74..db899e2 100644 --- a/import/main.go +++ b/import/main.go @@ -22,6 +22,7 @@ type Config struct { LogLevel slog.Level `conf:"default:INFO"` Timeout time.Duration `conf:"default:30m,help:maximum time to wait for all fetchers and importers combined"` Replace bool `conf:"default:false,help:whether to remove existing database, if exists"` + IndexPath string `conf:"default:data/index.bleve"` } func main() { @@ -50,7 +51,7 @@ func main() { return } - indexer, err := search.NewIndexer(cfg.DataPath, runtimeConfig.Replace) + indexer, err := search.NewIndexer(runtimeConfig.IndexPath, runtimeConfig.Replace) if err != nil { log.Fatalf("Failed to create indexer: %v", err) } diff --git a/internal/search/indexer.go b/internal/search/indexer.go index fc0214b..ad891ff 100644 --- a/internal/search/indexer.go +++ b/internal/search/indexer.go @@ -29,19 +29,33 @@ type WriteIndex struct { indexMapping *mapping.IndexMappingImpl } -func NewIndexer(dir string, force bool) (*WriteIndex, error) { +const ExpectedIndexExtension = ".bleve" + +func NewIndexer(indexPath string, force bool) (*WriteIndex, error) { var err error bleve.SetLog(log.Default()) - indexPath := path.Join(dir, indexFilename) - exists, err := file.Exists(indexPath) if err != nil { - return nil, errors.WithMessagef(err, "could not check if index exists at path %s", indexPath) + return nil, errors.WithMessagef( + err, + "could not check if index exists at path %s", + indexPath, + ) } if exists { if !force { - return nil, errors.Errorf("index file %s already exists (use --force to replace)", indexPath) + return nil, errors.Errorf( + "index file %s already exists (use --force to replace)", + indexPath, + ) + } + if path.Ext(indexPath) != ExpectedIndexExtension { + return nil, errors.Errorf( + "cowardly refusing to delete path %s (it doesn't end in '%s')", + indexPath, + ExpectedIndexExtension, + ) } err := os.RemoveAll(indexPath) if err != nil { @@ -130,7 +144,10 @@ func NewIndexer(dir string, force bool) (*WriteIndex, error) { }, nil } -func (i *WriteIndex) ImportOptions(ctx context.Context, objects <-chan *options.NixOption) <-chan error { +func (i *WriteIndex) ImportOptions( + ctx context.Context, + objects <-chan *options.NixOption, +) <-chan error { var err error errs := make(chan error) diff --git a/internal/search/search.go b/internal/search/search.go index 92afdfb..9766294 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/gob" - "path" "searchix/internal/options" "github.com/blevesearch/bleve/v2" @@ -13,7 +12,6 @@ import ( ) const ResultsPerPage = 20 -const indexFilename = "index.bleve" type DocumentMatch struct { search.DocumentMatch @@ -29,9 +27,7 @@ type ReadIndex struct { index bleve.Index } -func Open(dir string) (*ReadIndex, error) { - indexPath := path.Join(dir, indexFilename) - +func Open(indexPath string) (*ReadIndex, error) { idx, err := bleve.Open(indexPath) if err != nil { return nil, errors.WithMessagef(err, "unable to open index at path %s", indexPath) diff --git a/internal/server/server.go b/internal/server/server.go index 1611a7a..8252b8e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -45,6 +45,7 @@ type Config struct { BaseURL cfg.URL `conf:"default:http://localhost:3000,short:b"` ConfigFile string `conf:"short:c"` LogLevel slog.Level `conf:"default:INFO"` + IndexPath string `conf:"default:data/index.bleve"` } type HTTPError struct { @@ -89,7 +90,7 @@ func New(runtimeConfig *Config) (*Server, error) { } slog.Debug("loading index") - index, err := search.Open(config.DataPath) + index, err := search.Open(runtimeConfig.IndexPath) slog.Debug("loaded index") if err != nil { log.Fatalf("could not open search index, error: %#v", err) -- cgit 1.4.1