-
Notifications
You must be signed in to change notification settings - Fork 8
/
options.go
346 lines (300 loc) · 8.91 KB
/
options.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2020 Delving B.V.
//
// 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 ikuzo
import (
"io/fs"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/delving/hub3/config"
"github.com/delving/hub3/ikuzo/logger"
"github.com/delving/hub3/ikuzo/service/organization"
"github.com/delving/hub3/ikuzo/service/x/bulk"
"github.com/delving/hub3/ikuzo/service/x/ead"
"github.com/delving/hub3/ikuzo/service/x/imageproxy"
"github.com/delving/hub3/ikuzo/service/x/oaipmh"
"github.com/delving/hub3/ikuzo/service/x/revision"
"github.com/delving/hub3/ikuzo/storage/x/elasticsearch"
"github.com/delving/hub3/ikuzo/webapp"
"github.com/go-chi/chi"
"github.com/pacedotdev/oto/otohttp"
)
const (
taskIDRoute = "/api/ead/tasks/{id}"
datasetIDRoute = "/api/datasets/{spec}"
)
// RouterFunc is a callback that registers routes to the ikuzo.Server.
type RouterFunc func(router chi.Router)
// Option is a closure to configure the Server.
// It is used in NewServer.
type Option func(*server) error
// SetPort sets the TCP port for the Server.
//
// The Server listens on :3000 by default.
func SetPort(port int) Option {
return func(s *server) error {
s.port = port
return nil
}
}
// SetMetricsPort sets the TCP port for the metrics server.
//
// No default. When set to 0 the metrics server is not started
func SetMetricsPort(port int) Option {
return func(s *server) error {
s.metricsPort = port
return nil
}
}
// SetTLS sets the TLS key and certificate.
//
// When both are set the server starts in TLS mode.
func SetTLS(cert, key string) Option {
return func(s *server) error {
s.certFile = cert
s.keyFile = key
return nil
}
}
// SetLogger configures the global logger for the server.
func SetLogger(l *logger.CustomLogger) Option {
return func(s *server) error {
s.logger = l
return nil
}
}
// SetDisableRequestLogger disables logging of HTTP request
func SetDisableRequestLogger() Option {
return func(s *server) error {
s.disableRequestLogger = true
return nil
}
}
// SetMiddleware configures the global middleware for the HTTP router.
func SetMiddleware(middleware ...func(next http.Handler) http.Handler) Option {
return func(s *server) error {
s.middleware = append(s.middleware, middleware...)
return nil
}
}
// SetRouters adds all HTTP routes for the server.
func SetRouters(rb ...RouterFunc) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs, rb...)
return nil
}
}
// RegisterOtoServer registers an otohttp.Server.
//
// This enables the server to expose RPC on the '/oto/' endpoint
func RegisterOtoServer(otoServer *otohttp.Server) Option {
return func(s *server) error {
s.oto = otoServer
return nil
}
}
// SetOrganisationService configures the organization service.
// When no service is set a default transient memory-based service is used.
func SetOrganisationService(service *organization.Service) Option {
return func(s *server) error {
s.organizations = service
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Mount("/organizations", service.Routes())
},
)
s.middleware = append(s.middleware, service.ResolveOrgByDomain)
return nil
}
}
// SetRevisionService configures the organization service.
// When no service is set a default transient memory-based service is used.
func SetRevisionService(service *revision.Service) Option {
return func(s *server) error {
s.revision = service
s.routerFuncs = append(s.routerFuncs, func(r chi.Router) {
r.HandleFunc("/git/{user}/{collection}.git/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := strings.TrimPrefix(r.URL.Path, "/git")
if !service.BareRepo {
p = strings.ReplaceAll(p, ".git/", "/.git/")
}
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = p
service.ServeHTTP(w, r2)
}))
})
return nil
}
}
func SetElasticSearchProxy(proxy *elasticsearch.Proxy) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Handle("/{index}/_search", proxy)
r.Handle("/{index}/{documentType}/_search", proxy)
},
)
return nil
}
}
func SetBuildVersionInfo(info *BuildVersionInfo) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Get("/version", func(w http.ResponseWriter, r *http.Request) {
s.respond(w, r, info, http.StatusOK)
})
},
)
return nil
}
}
func SetEnableLegacyConfig(cfgFile string) Option {
return func(s *server) error {
// this initializes the hub3 configuration object that has global state
// TODO(kiivihal): remove this after legacy hub3/server/http/handlers are migrated
config.SetCfgFile(cfgFile)
config.InitConfig()
return nil
}
}
func SetLegacyRouters(routers ...RouterFunc) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs, routers...)
return nil
}
}
func SetStaticFS(static fs.FS) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Get("/static/*", webapp.NewStaticHandler(static))
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/favicon.ico", http.StatusMovedPermanently)
})
},
)
return nil
}
}
func SetEADService(svc *ead.Service) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Post("/api/ead", svc.Upload)
r.Get("/api/ead/tasks", svc.Tasks)
r.Get(taskIDRoute, svc.GetTask)
r.Delete(taskIDRoute, svc.CancelTask)
},
)
s.addShutdown("EAD service", svc)
return nil
}
}
func SetOAIPMHService(svc *oaipmh.Service) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Get("/oai/!open_oai.OAIHandler", svc.ServeHTTP)
r.Post("/oai/harvest-now", svc.HarvestNow)
},
)
s.addShutdown("oai-pmh service", svc)
return nil
}
}
func SetBulkService(svc *bulk.Service) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Post("/api/index/bulk", svc.Handle)
r.Post("/api/index/rdf", svc.HandleRDF)
},
)
return nil
}
}
func SetShutdownHook(name string, hook Shutdown) Option {
return func(s *server) error {
if _, ok := s.shutdownHooks[name]; !ok {
s.shutdownHooks[name] = hook
}
return nil
}
}
func SetImageProxyService(service *imageproxy.Service) Option {
return func(s *server) error {
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
r.Mount("/", service.Routes())
},
)
return nil
}
}
type ProxyRoute struct {
Method string
Pattern string
}
// SetDataNodeProxy creates a reverse proxy to the dataNode and set override routes.
//
// The 'proxyRoutes' argument can be used to add additional override routes.
func SetDataNodeProxy(dataNode string, proxyRoutes ...ProxyRoute) Option {
return func(s *server) error {
nodeURL, _ := url.Parse(dataNode)
s.dataNodeProxy = httputil.NewSingleHostReverseProxy(nodeURL)
s.routerFuncs = append(s.routerFuncs,
func(r chi.Router) {
// ead
r.Post("/api/ead", s.proxyDataNode)
r.Get("/api/ead/tasks", s.proxyDataNode)
r.Get(taskIDRoute, s.proxyDataNode)
r.Delete(taskIDRoute, s.proxyDataNode)
r.Post("/api/index/bulk", s.proxyDataNode)
r.Get("/api/ead/{spec}/download", s.proxyDataNode)
r.Get("/api/ead/{spec}/mets/{inventoryID}", s.proxyDataNode)
r.Get("/api/ead/{spec}/desc", s.proxyDataNode)
r.Get("/api/ead/{spec}/desc/index", s.proxyDataNode)
r.Get("/api/ead/{spec}/meta", s.proxyDataNode)
// datasets
r.Get("/api/datasets/", s.proxyDataNode)
r.Get("/api/datasets/histogram", s.proxyDataNode)
r.Post("/api/datasets/", s.proxyDataNode)
r.Get(datasetIDRoute, s.proxyDataNode)
r.Get("/api/datasets/{spec}/stats", s.proxyDataNode)
// later change to update dataset
r.Post(datasetIDRoute, s.proxyDataNode)
r.Delete(datasetIDRoute, s.proxyDataNode)
// custom routes
for _, route := range proxyRoutes {
switch {
case strings.EqualFold("get", route.Method):
r.Get(route.Pattern, s.proxyDataNode)
case strings.EqualFold("post", route.Method):
r.Post(route.Pattern, s.proxyDataNode)
case strings.EqualFold("put", route.Method):
r.Put(route.Pattern, s.proxyDataNode)
case strings.EqualFold("delete", route.Method):
r.Delete(route.Pattern, s.proxyDataNode)
}
}
},
)
return nil
}
}