-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
302 lines (262 loc) · 7.68 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Copyright 2020 Center for Digital Matter HGK FHNW, Basel.
Copyright 2020 info-age GmbH, Basel.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
badger "github.com/dgraph-io/badger/v4"
"github.com/je4/utils/v2/pkg/zLogger"
"github.com/je4/zsearch/v2/pkg/search"
"github.com/rs/zerolog"
"google.golang.org/api/customsearch/v1"
"google.golang.org/api/option"
"io"
"log"
"net"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"time"
/*
"github.com/ampproject/amppackager/packager/certcache"
"github.com/ampproject/amppackager/packager/certloader"
"github.com/ampproject/amppackager/packager/healthz"
"github.com/ampproject/amppackager/packager/mux"
"github.com/ampproject/amppackager/packager/rtv"
"github.com/ampproject/amppackager/packager/signer"
"github.com/ampproject/amppackager/packager/util"
"github.com/ampproject/amppackager/packager/validitymap"
*/)
/*
Mehrere PDFs
http://localhost:82/public/zotero-2206003.8JA5VIU9
Mehrere Videos
Viele Unterschiedliche Medien
Sehr viele Bilder
http://localhost:82/public/zotero-2206003.J6NR8ADD
http://localhost:82/public/zotero-2206003.BKPE95CU
http://localhost:82/public/zotero-2206003.PAASB4HF
http://localhost:82/public/zotero-2206003.CVCVRS7J
http://localhost:82/public/zotero-2206003.QWM6R7TT
http://localhost:82/public/zotero-2206003.SSEV6K83
Gedrehte Bilder
http://localhost:82/public/zotero-2206003.NJHHX5MB
Fehlerhafte Darstellung
http://localhost:82/public/zotero-2206003.5JH9WCM9
http://localhost:82/public/zotero-2206003.FG8UDJZG
Mehrere Audio
http://localhost:82/public/zotero-2180340.TPS2WVNX
https://mediathek.hgk.fhnw.ch/amp/detail/zotero-1624911.27CDTZ4B
-->
https://mediathek-hgk-fhnw-ch.cdn.ampproject.org/c/s/mediathek.hgk.fhnw.ch/amp/detail/zotero-1624911.27CDTZ4B
*/
func main() {
cfgfile := flag.String("cfg", "./search.toml", "locations of config file")
flag.Parse()
config := LoadConfig(*cfgfile)
var out io.Writer = os.Stdout
if config.Logfile != "" {
fp, err := os.OpenFile(config.Logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("cannot open logfile %s: %v", config.Logfile, err)
}
defer fp.Close()
out = fp
}
output := zerolog.ConsoleWriter{Out: out, TimeFormat: time.RFC3339}
_logger := zerolog.New(output).With().Timestamp().Logger()
_logger.Level(zLogger.LogLevel(config.Loglevel))
var logger zLogger.ZLogger = &_logger
var accesslog io.Writer
if config.AccessLog == "" {
accesslog = os.Stdout
} else {
f, err := os.OpenFile(config.AccessLog, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
logger.Panic().Msgf("cannot open file %s: %v", config.AccessLog, err)
return
}
defer f.Close()
accesslog = f
}
stat, err := os.Stat(config.CacheDir)
if err != nil {
logger.Panic().Msgf("cannot stat %s", config.CacheDir)
return
}
if !stat.IsDir() {
logger.Panic().Msgf("%s not a director", config.CacheDir)
return
}
if config.ClearCacheOnStartup {
logger.Info().Msgf("deleting cache files in %s", config.CacheDir)
if len(config.CacheDir) < 4 {
logger.Panic().Msgf("%s too short. will not clear cache", config.CacheDir)
return
}
d, err := os.Open(config.CacheDir)
if err != nil {
logger.Panic().Msgf("cannot open directory %s", config.CacheDir)
return
}
names, err := d.Readdirnames(-1)
if err != nil {
d.Close()
logger.Panic().Msgf("cannot read %s", config.CacheDir)
return
}
d.Close()
for _, name := range names {
fullpath := filepath.Join(config.CacheDir, name)
logger.Info().Msgf("delete %s", fullpath)
if err := os.Remove(fullpath); err != nil {
logger.Panic().Msgf("cannot delete %s", fullpath)
return
}
}
}
/*
if err := os.RemoveAll(config.CacheDir); err != nil {
logger.Error().Err(err).Msgf("cannot remove %s: %v", config.CacheDir, err)
}
*/
bconfig := badger.DefaultOptions(config.CacheDir)
if runtime.GOOS == "windows" {
// bconfig.Truncate = true
}
// bconfig.Logger = logger
db, err := badger.Open(bconfig)
if err != nil {
logger.Panic().Msgf("cannot open badger database: %v", err)
return
}
defer db.Close()
mtElasticWrapper, err := search.NewMTElasticSearch(config.ElasticSearch.Endpoint, config.ElasticSearch.Index, config.ElasticSearch.ApiKey, logger)
if err != nil {
logger.Panic().Msgf("cannot initialize solr search wrapper: %v", err)
return
}
searchEngine, err := search.NewSearch(mtElasticWrapper, config.Solr.CacheSize, config.CacheExpiry.Duration, db, logger)
if err != nil {
logger.Panic().Msgf("cannot initialize solr search engine: %v", err)
return
}
uc, err := search.NewUserCache(config.IdleTimeout.Duration, config.UserCacheSize)
if err != nil {
logger.Panic().Err(err)
}
googleSvc, err := customsearch.NewService(context.Background(), option.WithAPIKey(config.Google.Apikey))
if err != nil {
logger.Panic().Err(err)
}
facets := search.SolrFacetList{}
for _, facet := range config.Facets {
facets[facet.Name] = search.SolrFacet{
Label: facet.Name,
Name: facet.Name,
Field: facet.Field,
Prefix: facet.Prefix,
Restrict: facet.Restrict,
}
}
locations := search.NetGroups{}
for _, loc := range config.Locations {
locations[loc.Group] = []*net.IPNet{}
for _, n := range loc.Networks {
locations[loc.Group] = append(locations[loc.Group], &n.IPNet)
}
}
subfilters := []search.SubFilter{}
for _, sf := range config.Query.SubFilter {
subfilters = append(subfilters, search.SubFilter{
Name: sf.Name,
Label: sf.Label,
Filter: sf.Filter,
})
}
var kt = make(map[string]search.KV)
for k, v := range config.Google.CustomSearchKeys {
kt[k] = search.KV{Key: v.Key, Name: v.Name}
}
srv, err := search.NewServer(
config.ServiceName,
searchEngine,
uc,
googleSvc,
config.Template,
config.TemplateDev,
config.InstanceName,
config.Addr,
config.AddrExt,
config.Mediaserver,
config.MediaserverKey,
config.MediaserverExp.Duration,
logger,
accesslog,
config.Prefixes,
config.StaticDir,
config.SitemapDir,
config.StaticCacheControl,
config.TemplateDir,
config.JWTKey,
config.JWTAlg,
config.LinkTokenExp.Duration,
config.SessionTimeout.Duration,
config.LoginUrl,
config.LoginIssuer,
config.AccessGroup.Guest,
config.AccessGroup.Admin,
config.AmpCache,
config.AmpApiKey,
config.SearchFields,
facets,
locations,
config.Icons,
config.Query.BaseCatalog,
subfilters,
config.CollectionsCatalog,
config.ClusterCatalog,
kt,
config.FacebookAppId,
)
if err != nil {
logger.Error().Err(err).Msgf("error initializing server: %v", err)
return
}
go func() {
if err := srv.ListenAndServe(config.CertPEM, config.KeyPEM); err != nil {
logger.Fatal().Msgf("server died: %v", err)
}
}()
end := make(chan bool, 1)
// process waiting for interrupt signal (TERM or KILL)
go func() {
sigint := make(chan os.Signal, 1)
// interrupt signal sent from terminal
signal.Notify(sigint, os.Interrupt)
signal.Notify(sigint, syscall.SIGTERM)
signal.Notify(sigint, syscall.SIGKILL)
<-sigint
// We received an interrupt signal, shut down.
logger.Info().Msgf("shutdown requested")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
srv.Shutdown(ctx)
end <- true
}()
<-end
logger.Info().Msg("server stopped")
}