-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
155 lines (133 loc) · 4.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/blevesearch/bleve/v2"
"github.com/pirmd/epub"
"gorm.io/gorm"
"github.com/alecthomas/kong"
"github.com/spf13/afero"
"github.com/svera/coreander/v4/internal/index"
"github.com/svera/coreander/v4/internal/metadata"
"github.com/svera/coreander/v4/internal/webserver"
"github.com/svera/coreander/v4/internal/webserver/infrastructure"
)
var version string = "unknown"
const indexPath = "/.coreander/index"
const databasePath = "/.coreander/database.db"
var (
input CLIInput
appFs afero.Fs
idx *index.BleveIndexer
db *gorm.DB
homeDir string
err error
metadataReaders map[string]metadata.Reader
sender webserver.Sender
)
func init() {
ctx := kong.Parse(&input, kong.Description(`
Coreander is a document management system which indexes metadata from documents in a library and allows users to search and read them through a web interface.
`),
kong.Vars{
"version": version,
},
)
if ctx.Error != nil {
log.Fatalf("Error parsing configuration: %s", ctx.Error)
}
log.Printf("Coreander version %s starting\n", version)
homeDir, err = os.UserHomeDir()
if err != nil {
log.Fatal("Error retrieving user home dir")
}
if _, err := os.Stat(input.LibPath); os.IsNotExist(err) {
log.Fatalf("Directory '%s' does not exist, exiting", input.LibPath)
}
metadataReaders = map[string]metadata.Reader{
".epub": metadata.EpubReader{
GetMetadataFromFile: epub.GetMetadataFromFile,
GetPackageFromFile: epub.GetPackageFromFile,
},
".pdf": metadata.PdfReader{},
}
appFs = afero.NewOsFs()
indexFile := getIndexFile(appFs)
idx = index.NewBleve(indexFile, appFs, input.LibPath, metadataReaders)
db = infrastructure.Connect(homeDir+databasePath, input.WordsPerMinute)
}
func main() {
defer idx.Close()
go startIndex(idx, input.BatchSize, input.LibPath)
sender = &infrastructure.NoEmail{}
if input.SmtpServer != "" && input.SmtpUser != "" && input.SmtpPassword != "" {
sender = &infrastructure.SMTP{
Server: input.SmtpServer,
Port: input.SmtpPort,
User: input.SmtpUser,
Password: input.SmtpPassword,
}
}
webserverConfig := webserver.Config{
Version: version,
MinPasswordLength: input.MinPasswordLength,
WordsPerMinute: input.WordsPerMinute,
JwtSecret: []byte(input.JwtSecret),
FQDN: input.FQDN,
Port: input.Port,
HomeDir: homeDir,
LibraryPath: input.LibPath,
CoverMaxWidth: input.CoverMaxWidth,
RequireAuth: input.RequireAuth,
UploadDocumentMaxSize: input.UploadDocumentMaxSize,
}
webserverConfig.SessionTimeout, err = time.ParseDuration(fmt.Sprintf("%fh", input.SessionTimeout))
if err != nil {
log.Fatal(fmt.Errorf("wrong value for session timeout"))
}
webserverConfig.RecoveryTimeout, err = time.ParseDuration(fmt.Sprintf("%fh", input.RecoveryTimeout))
if err != nil {
log.Fatal(fmt.Errorf("wrong value for recovery timeout"))
}
controllers := webserver.SetupControllers(webserverConfig, db, metadataReaders, idx, sender, appFs)
app := webserver.New(webserverConfig, controllers, sender, idx)
if strings.ToLower(input.FQDN) == "localhost" {
fmt.Printf("Warning: using \"localhost\" as FQDN. Links using this FQDN won't be accessible outside this system.\n")
}
log.Printf("Started listening on port %d\n", input.Port)
log.Fatal(app.Listen(fmt.Sprintf(":%d", input.Port)))
}
func startIndex(idx *index.BleveIndexer, batchSize int, libPath string) {
start := time.Now().Unix()
log.Printf("Indexing documents at %s, this can take a while depending on the size of your library.", libPath)
err := idx.AddLibrary(batchSize, input.ForceIndexing)
if err != nil {
log.Fatal(err)
}
end := time.Now().Unix()
dur, _ := time.ParseDuration(fmt.Sprintf("%ds", end-start))
log.Printf("Indexing finished, took %d seconds", int(dur.Seconds()))
fileWatcher(idx, libPath)
}
func getIndexFile(fs afero.Fs) bleve.Index {
indexFile, err := bleve.Open(homeDir + indexPath)
if err == bleve.ErrorIndexPathDoesNotExist {
log.Println("No index found, creating a new one.")
indexFile = index.Create(homeDir + indexPath)
}
version, err := indexFile.GetInternal([]byte("version"))
if err != nil {
log.Fatal(err)
}
if string(version) == "" || string(version) < index.Version {
log.Println("Old version index found, recreating it.")
if err = fs.RemoveAll(homeDir + indexPath); err != nil {
log.Fatal(err)
}
indexFile = index.Create(homeDir + indexPath)
}
return indexFile
}