-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
191 lines (169 loc) · 4.52 KB
/
server.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
package cli
import (
"context"
"fmt"
"html/template"
"net/http"
"net/url"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/go-chi/chi/v5"
"github.com/gorilla/sessions"
"github.com/leonelquinteros/gotext"
"github.com/nics/ich"
"github.com/ory/graceful"
"github.com/spf13/cobra"
"github.com/ugent-library/biblio-backoffice/backends"
"github.com/ugent-library/biblio-backoffice/helpers"
"github.com/ugent-library/biblio-backoffice/render"
"github.com/ugent-library/biblio-backoffice/routes"
"github.com/ugent-library/biblio-backoffice/urls"
"github.com/ugent-library/biblio-backoffice/vocabularies"
"github.com/ugent-library/bind"
"github.com/ugent-library/mix"
"github.com/ugent-library/oidc"
)
func init() {
rootCmd.AddCommand(serverCmd)
serverCmd.AddCommand(serverStartCmd)
}
var serverCmd = &cobra.Command{
Use: "server",
Short: "Biblio backoffice HTTP server",
}
var serverStartCmd = &cobra.Command{
Use: "start",
Short: "start the http server",
RunE: func(cmd *cobra.Command, args []string) error {
services := newServices()
services.MediaTypeSearchService.IndexAll()
// e.LicenseSearchService.IndexAll()
// setup router
router, err := buildRouter(services)
if err != nil {
return err
}
// setup server
addr := fmt.Sprintf("%s:%d", config.Host, config.Port)
server := graceful.WithDefaults(&http.Server{
Addr: addr,
Handler: router,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 5 * time.Minute,
})
logger.Infof("starting server at %s", addr)
if err := graceful.Graceful(server.ListenAndServe, server.Shutdown); err != nil {
return err
}
logger.Info("gracefully stopped server")
return nil
},
}
func buildRouter(services *backends.Services) (*ich.Mux, error) {
b := config.BaseURL
if b == "" {
if config.Host == "" {
b = "http://localhost"
} else {
b = "http://" + config.Host
}
if config.Port != 80 {
b = fmt.Sprintf("%s:%d", b, config.Port)
}
}
baseURL, err := url.Parse(b)
if err != nil {
return nil, err
}
// router
router := ich.New()
// assets
assets, err := mix.New(mix.Config{
ManifestFile: "static/mix-manifest.json",
PublicPath: baseURL.Path + "/static/",
})
if err != nil {
return nil, err
}
// renderer
funcMaps := []template.FuncMap{
sprig.FuncMap(),
urls.FuncMap(router, baseURL.Scheme, baseURL.Host),
helpers.FuncMap(),
{
"assetPath": assets.AssetPath,
"appMode": func() string { // TODO eliminate need for this
return config.Env
},
"vocabulary": func(k string) []string { // TODO eliminate need for this?
return vocabularies.Map[k]
},
},
}
// init render
render.AuthURL = "/login"
for _, funcs := range funcMaps {
render.Funcs(funcs)
}
render.MustParse()
// init bind
bind.PathValueFunc = chi.URLParam
// locale
loc := gotext.NewLocale("locales", "en")
loc.AddDomain("default")
// timezone
timezone, err := time.LoadLocation(config.Timezone)
if err != nil {
return nil, err
}
// sessions & auth
sessionSecret := []byte(config.Session.Secret)
sessionName := config.Session.Name
sessionStore := sessions.NewCookieStore(sessionSecret)
sessionStore.MaxAge(config.Session.MaxAge)
sessionStore.Options.Path = "/"
if baseURL.Path != "" {
sessionStore.Options.Path = baseURL.Path
}
sessionStore.Options.HttpOnly = true
sessionStore.Options.Secure = baseURL.Scheme == "https"
oidcAuth, err := oidc.NewAuth(context.TODO(), oidc.Config{
URL: config.OIDC.URL,
ClientID: config.OIDC.ClientID,
ClientSecret: config.OIDC.ClientSecret,
RedirectURL: baseURL.String() + "/auth/openid-connect/callback",
CookieName: config.Session.Name + ".state",
CookieSecret: []byte(config.Session.Secret),
Insecure: config.Env != "local",
})
if err != nil {
return nil, err
}
// add routes
routes.Register(routes.Config{
Version: routes.Version{
Branch: version.Branch,
Commit: version.Commit,
Image: version.Image,
},
Env: config.Env,
Services: services,
BaseURL: baseURL,
Router: router,
Assets: assets,
SessionStore: sessionStore,
SessionName: sessionName,
Timezone: timezone,
Loc: loc,
Logger: logger,
OIDCAuth: oidcAuth,
FrontendURL: config.Frontend.URL,
FrontendUsername: config.Frontend.Username,
FrontendPassword: config.Frontend.Password,
IPRanges: config.IPRanges,
MaxFileSize: config.MaxFileSize,
CSRFName: config.CSRF.Name,
CSRFSecret: config.CSRF.Secret,
})
return router, nil
}