Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
245 lines (205 loc) · 7.63 KB
/
Copy pathserver.js
File metadata and controls
245 lines (205 loc) · 7.63 KB
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
import config from "./config/index.js"
import { addMiddlewareProperties } from "./utils/defaults.js"
import express from "express"
import { createDatabase } from "./utils/db.js"
import morgan from "morgan"
import nocache from "nocache"
import createRouter from "./routes/index.js"
import { serverStatus, OpenAPI } from "./utils/status.js"
import { Authenticator } from "./utils/auth.js"
import { urlForLinkHeader } from "./utils/url-for-link-header.js"
import { ipcheck } from "./utils/ipcheck.js"
import { getUser } from "./utils/users.js"
import * as errors from "./errors/index.js"
import portfinder from "portfinder"
import expressWs from "express-ws"
import { setupChangesApi } from "./utils/changes.js"
const __dirname = import.meta.dirname
const db = createDatabase(config)
const openapi = new OpenAPI(config)
function getEndpoint(path, summary, ...callback) {
app.use(path, ...callback)
openapi.getEndpoint(path, { summary })
}
function enableRoutes(basePath, router) {
app.use(basePath, router.router)
openapi.enableRoutes(basePath, router.spec)
}
config.log(`Running in ${config.env} mode.`)
if (config.env == "test") {
portfinder.basePort = config.port
// TODO: update config.baseUrl
config.port = await portfinder.getPortPromise()
}
if (!config.baseUrl) {
config.warn("Warning: If you're using jskos-server behind a reverse proxy, it is necessary to add `baseUrl` to the configuration file!")
}
// Initialize express with settings
const app = express()
app.use(express.static("public")) // JS and CSS of UI at root
// Initialize WebSocket support
expressWs(app)
app.set("json spaces", 2)
if (config.proxies && config.proxies.length) {
app.set("trust proxy", config.proxies)
}
import ejs from "ejs"
// Configure view engine to render EJS templates.
app.set("views", __dirname + "/views")
app.set("view engine", "ejs")
app.engine("ejs", (filepath, data, callback) => {
return ejs.renderFile(filepath, data, {
openDelimiter: "[",
closeDelimiter: "]",
}, callback)
})
// Database connection (TODO: move to db module)
const connect = async () => {
try {
await db.connect(true)
// TODO: `indexExists` causes a deprecation warning. Find a different solution.
if (config.schemes && !(await db.connection.collection("terminologies").indexExists("text"))) {
config.warn("Text index on terminologies collection missing. /voc/search and /voc/suggest are disabled. Run `npm run import -- --indexes` or `npm run import -- -i schemes` to created indexes.")
config.status["voc-search"] = null
config.status["voc-suggest"] = null
}
if (config.concepts && !(await db.connection.collection("concepts").indexExists("text"))) {
config.warn("Text index on concepts collection missing. /concepts/search and /concepts/suggest are disabled. Run `npm run import -- --indexes` or `npm run import -- -i concepts` to created indexes.")
config.status.search = null
config.status.suggest = null
}
if (config.annotations?.mismatchTagVocabulary?.uri) {
const model = await import("./models/concepts.js")
const concepts = await model.Concept.find({ "inScheme.uri": config.annotations.mismatchTagVocabulary.uri })
if (concepts.length === 0) {
config.warn("annotations.mismatchTagVocabulary is configured, but no data for that vocabulary could be found in the database. Import the vocabulary data into this instance for the setting to work.")
}
}
} catch (error) {
config.warn("Error connecting to database, reconnect in a few seconds...")
}
}
// Logging for access logs
if (config.verbosity === true || config.verbosity === "log") {
app.use(morgan(":date[iso] \":method :url HTTP/:http-version\" :status :res[content-length] \":referrer\" \":user-agent\""))
}
// Add default headers
app.use((req, res, next) => {
if (req.headers.origin) {
// Allow all origins by returning the request origin in the header
res.setHeader("Access-Control-Allow-Origin", req.headers.origin)
} else {
// Fallback to * if there is no origin in header
res.setHeader("Access-Control-Allow-Origin", "*")
}
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
res.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,PATCH,DELETE")
res.setHeader("Access-Control-Expose-Headers", "X-Total-Count, Link")
res.setHeader("Content-Type", "application/json; charset=utf-8")
const links = [
urlForLinkHeader({ base: config.baseUrl, req: { path: "/openapi.json" }, rel: "service-desc" }),
]
// Deprecation headers for /narrower, /ancestors, /search, and /suggest
// TODO for 3.0: Remove these headers
if (["/narrower", "/ancestors", "/search", "/suggest"].includes(req.path)) {
res.setHeader("Deprecation", true)
links.push(urlForLinkHeader({ base: config.baseUrl, req, rel: "alternate" }))
links[0] = links[0].replace(req.path, `/concepts${req.path}`)
links.push("<https://github.com/gbv/jskos-server/releases/tag/v2.0.0>; rel=\"deprecation\"")
}
res.set("Link", links.join(","))
next()
})
// Disable client side caching
app.use(nocache())
// Disable ETags
app.set("etag", false)
app.use(express.urlencoded({ extended: false }))
// Set some properties on req that will be used by other middleware
app.use(addMiddlewareProperties(config))
// ---- Add routes
// Root path for static page
app.get("/", (req, res) => {
res.setHeader("Content-Type", "text/html")
res.render("base", openapi.description)
})
// JSON Schema for /status
app.use("/status.schema.json", express.static(__dirname + "/status.schema.json"))
getEndpoint(
"/status",
"Returns status of the service",
(req, res) => {
const status = serverStatus(config, db.connection.readyState === 1)
status.openapi = openapi.description
return res.json(status)
})
getEndpoint(
"/openapi.json",
"Return API specification as OpenAPI Description",
(req, res) => res.json(openapi.description))
// IP check middleware
app.use(ipcheck(config))
// /checkAuth
const authenticator = new Authenticator(config)
getEndpoint(
"/checkAuth",
"Check whether a user is authorized and which access rights are granted",
authenticator.checkAuth(),
(req, res) => {
const info = { user: getUser(req) }
if (res.access) {
info.access = res.access
}
res.json(info)
})
// Database check middleware
app.use((req, res, next) => {
if (db.connection.readyState === 1) {
next()
} else {
// No connection to database, return error
next(new errors.DatabaseAccessError())
}
})
// Add conditional routes
for (let type of ["schemes", "mappings", "concordances", "annotations", "registries"]) {
if (config[type]) {
const path = type === "schemes" ? "voc" : type
enableRoutes(`/${path}`, createRouter[type](config))
}
}
if (config.concepts) {
enableRoutes("", createRouter.concepts(config))
}
// These routes are always enabled
enableRoutes("/data", createRouter.data(config))
enableRoutes("/validate", createRouter.validate(config))
// Error handling
app.use((error, req, res, next) => {
// Check if error is defined in errors
if (Object.values(errors).includes(error.constructor)) {
const body = {
error: error.constructor.name,
status: error.statusCode,
message: error.message,
}
if (error.position !== undefined) {
body.position = error.position
}
res.status(error.statusCode).send(body)
} else {
next(error)
}
})
// Connect immediately on startup
connect()
// Changes API
if (config.changes) {
// TODO: add to openapi
await setupChangesApi(app, config, db)
}
app.listen(config.port, () => {
config.log(`Now listening on port ${config.port}`)
})
app.connection = db.connection
export { app }