Skip to content

Commit

Permalink
Merge pull request #16 from svera/added-skip
Browse files Browse the repository at this point in the history
Added skip reindex option
  • Loading branch information
svera authored Mar 13, 2021
2 parents 4e20795 + c4e10ad commit df329b0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`)
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion internal/webserver/embedded/views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<footer class="footer mt-auto pb-3">
<div class="container">
<span class="text-muted"><small>{{t .Lang "footer"}} - {{.Version}}</small></span>
<span class="text-muted"><small>{{t .Lang "footer"}} - <a href="https://github.com/svera/coreander/releases" target="_blank" rel="noopener noreferrer">{{.Version}}</a></small></span>
</div>
</footer>
<script src="/js/bootstrap.min.js"></script>
Expand Down
49 changes: 29 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,42 @@ 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))
if err != nil {
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)
}

0 comments on commit df329b0

Please sign in to comment.