From c4e10ad113c7bf066faa55ac32bb41117d23d3d4 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Sat, 13 Mar 2021 19:00:19 +0100 Subject: [PATCH] Added skip reindex option --- README.md | 5 +- config.go | 1 + internal/webserver/embedded/views/layout.html | 2 +- main.go | 49 +++++++++++-------- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2c3aa00..030ae5e 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ A personal Ebooks server, Coreander indexes the ebooks (only EPUBs with no DRM a * New books added or removed to/from the library folder are automatically indexed (Linux only). ## Building from source -Only source code is provided at the moment, so you'll have to manually build it. The only requirement is Go 1.16. +Coreander's only requirement is Go 1.16. There are two possibilites for building Coreander from source: * If you have [Mage](https://magefile.org) installed in your system, just type `mage install` from the source code folder. @@ -47,4 +47,7 @@ Coreander requires a `LIBPATH` environment variable to be set, which tells the a On first run, Coreander will index the books in your library, creating a database with those entries located at `$home/coreander/db`. Depending on your system's performance and the size of your library this may take a while. Also, the database can grow fairly big, so make sure you have enough free space on disk. +Every time is run, the application check for new entries, reindexing the whole library. You can +avoid this behaviour by setting the environment variable `SKIPREINDEX` to `false`. + Even if the application is still indexing entries, you can access its web interface right away. Just open a web browser and go to `localhost:3000` (replace `localhost` for the IP address of the machine where the server is running if you want to access it from another machine). It is possible to change the listening port just executing the application with the `PORT` environment variable (e. g. `PORT=4000 coreander`) \ No newline at end of file diff --git a/config.go b/config.go index 454b8b5..d421576 100644 --- a/config.go +++ b/config.go @@ -6,4 +6,5 @@ type Config struct { Port string `env:"PORT" env-default:"3000"` BatchSize int `env:"BATCHSIZE" env-default:"100"` CoverMaxWidth int `env:"COVERMAXWIDTH" env-default:"300"` + SkipReindex bool `env:"SKIPREINDEX" env-default:"false"` } diff --git a/internal/webserver/embedded/views/layout.html b/internal/webserver/embedded/views/layout.html index d65793c..c2611e6 100644 --- a/internal/webserver/embedded/views/layout.html +++ b/internal/webserver/embedded/views/layout.html @@ -33,7 +33,7 @@ diff --git a/main.go b/main.go index ee332a1..24c5924 100644 --- a/main.go +++ b/main.go @@ -50,29 +50,14 @@ func run(cfg Config, homeDir string, metadataReaders map[string]metadata.Reader, idx = index.NewBleve(indexFile, cfg.LibPath, metadataReaders) } if err == bleve.ErrorIndexPathDoesNotExist { - log.Println("No index found, creating a new one") - indexMapping := bleve.NewIndexMapping() - index.AddMappings(indexMapping) - indexFile, err := bleve.New(homeDir+"/coreander/db", indexMapping) - if err != nil { - log.Fatal(err) - } - idx = index.NewBleve(indexFile, cfg.LibPath, metadataReaders) + cfg.SkipReindex = false + idx = createIndex(homeDir, cfg.LibPath, metadataReaders) } defer idx.Close() - go func() { - start := time.Now().Unix() - log.Println(fmt.Sprintf("Indexing books at %s, this can take a while depending on the size of your library.", cfg.LibPath)) - err := idx.AddLibrary(appFs, cfg.BatchSize) - if err != nil { - log.Fatal(err) - } - end := time.Now().Unix() - dur, _ := time.ParseDuration(fmt.Sprintf("%ds", end-start)) - log.Println(fmt.Sprintf("Indexing finished, took %d seconds", int(dur.Seconds()))) - fileWatcher(idx, cfg.LibPath) - }() + if !cfg.SkipReindex { + go reindex(idx, appFs, cfg.BatchSize, cfg.LibPath) + } app := webserver.New(idx, cfg.LibPath, homeDir, version, metadataReaders, cfg.CoverMaxWidth) fmt.Printf("Coreander version %s started listening on port %s\n\n", version, cfg.Port) err = app.Listen(fmt.Sprintf(":%s", cfg.Port)) @@ -80,3 +65,27 @@ func run(cfg Config, homeDir string, metadataReaders map[string]metadata.Reader, log.Fatal(err) } } + +func reindex(idx *index.BleveIndexer, appFs afero.Fs, batchSize int, libPath string) { + start := time.Now().Unix() + log.Println(fmt.Sprintf("Indexing books at %s, this can take a while depending on the size of your library.", libPath)) + err := idx.AddLibrary(appFs, batchSize) + if err != nil { + log.Fatal(err) + } + end := time.Now().Unix() + dur, _ := time.ParseDuration(fmt.Sprintf("%ds", end-start)) + log.Println(fmt.Sprintf("Indexing finished, took %d seconds", int(dur.Seconds()))) + fileWatcher(idx, libPath) +} + +func createIndex(homeDir, libPath string, metadataReaders map[string]metadata.Reader) *index.BleveIndexer { + log.Println("No index found, creating a new one") + indexMapping := bleve.NewIndexMapping() + index.AddMappings(indexMapping) + indexFile, err := bleve.New(homeDir+"/coreander/db", indexMapping) + if err != nil { + log.Fatal(err) + } + return index.NewBleve(indexFile, libPath, metadataReaders) +}