-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
290 lines (256 loc) · 8.3 KB
/
server.js
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
import config from "./config.js"
import _ from "lodash"
import express from "express"
import bodyParser from "body-parser"
import { WikidataService } from "./lib/wikidata-wrapper.js"
import { loadMappingSchemes } from "./lib/mapping-schemes.js"
import getConcepts from "./lib/queries/get-concepts.js"
import * as jskos from "jskos-tools"
import path, { dirname } from "node:path"
import { fileURLToPath } from "node:url"
import passport from "passport"
import { Strategy as JwtStrategy } from "passport-jwt"
import { ExtractJwt } from "passport-jwt"
const __dirname = dirname(fileURLToPath(import.meta.url))
const { addContext, addMappingIdentifiers } = jskos
const port = config.port
const app = express()
app.set("json spaces", 2)
if (config.auth.key && config.oauth.consumer_key && config.oauth.consumer_secret) {
config.log("Authentication and therefore saving/removing mappings is configured.")
} else {
config.log("Note: To allow saving/removing mappings, authentication has to be configured (see documentation).")
}
function errorHandler(res) {
return (err) => {
console.error(err)
res.status(err.status || 500).json({ status: err.status, message: err.message })
}
}
// Prepare authorization via JWT
let auth
if (config.auth.algorithm && config.auth.key) {
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.auth.key,
algorithms: [config.auth.algorithm],
}
try {
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
done(null, jwt_payload.user)
}))
// Use like this: app.get("/secureEndpoint", auth, (req, res) => { ... })
// res.user will contain the current authorized user.
auth = passport.authenticate("jwt", { session: false })
} catch (error) {
console.error("Error setting up JWT authentication")
}
}
// serve static files from assets directory
app.use(express.static(path.join(__dirname, "/assets")))
// configure view engine to render EJS templates
app.set("views", __dirname + "/views")
app.set("view engine", "ejs")
app.get("/", (req, res) => res.render("base", { config }))
// add default JSKOS headers
app.use(function (req, res, next) {
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/ld+json")
next()
})
// Add body-parser middleware
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use((req, res, next) => {
if (req.query) {
// Limit for pagination
const defaultLimit = 1000
req.query.limit = parseInt(req.query.limit)
req.query.limit = (req.query.limit && req.query.limit > 0) ? req.query.limit : defaultLimit
// Offset for pagination
const defaultOffset = 0
req.query.offset = parseInt(req.query.offset)
req.query.offset = (req.query.offset && req.query.offset >= 0) ? req.query.offset : defaultOffset
}
next()
})
const addPaginationHeaders = (req, res, data) => {
const limit = req.query.limit
const offset = req.query.offset
const total = parseInt((data && data.totalCount) || (data && data.length))
if (req == null || res == null || limit == null || offset == null || total == null) {
return
}
const baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1) + req.path
const url = (query, rel) => {
let url = baseUrl
let index = 0
_.forOwn(query, (value, key) => {
url += `${(index == 0 ? "?" : "&")}${key}=${encodeURIComponent(value)}`
index += 1
})
return `<${url}>; rel="${rel}"`
}
// Set X-Total-Count header
res.set("X-Total-Count", total)
let links = []
let query = _.cloneDeep(req.query)
query.limit = limit
// rel: first
query.offset = 0
links.push(url(query, "first"))
// rel: prev
if (offset > 0) {
query.offset = Math.max(offset - limit, 0)
links.push(url(query, "prev"))
}
// rel: next
if (limit + offset < total) {
query.offset = offset + limit
links.push(url(query, "next"))
}
// rel: last
let current = 0
while (current + limit < total) {
current += limit
}
query.offset = current
links.push(url(query, "last"))
// Set Link header
res.set("Link", links.join(","))
}
function rewriteMappingUri(mapping) {
const { uri } = mapping
if (uri.startsWith("http://www.wikidata.org/entity/statement/")) {
mapping.uri = `${config.baseUrl}mappings/${uri.substring(41)}`
mapping.identifier = [uri]
mapping = addMappingIdentifiers(mapping)
}
return mapping
}
function paginate(jskos, req, res) {
if (_.isArray(jskos)) {
// Split result if necessary
if (jskos.totalCount == null || jskos.length > jskos.totalCount) {
let totalCount = jskos.totalCount || jskos.length
jskos = jskos.slice(req.query.offset, req.query.offset + req.query.limit)
jskos.totalCount = totalCount
}
// Add pagination headers
addPaginationHeaders(req, res, jskos)
}
res.json(jskos)
return jskos
}
// load schemes
const schemes = loadMappingSchemes()
// initialize service
const service = new WikidataService(schemes)
config.log(`loaded ${schemes.length} mapping schemes`)
// enable endpoints
const conceptHandler = (req, res) => {
getConcepts(req.query)
.then(addContext)
.then(jskos => paginate(jskos, req, res))
.catch(errorHandler(res))
}
app.get("/concepts", conceptHandler)
app.get("/concept", conceptHandler) // deprecated
app.get("/data", conceptHandler)
app.get("/mappings", (req, res) => {
service.getMappings(req.query)
.then(jskos => {
// Preserve totalCount property
const rewrittenJskos = jskos.map(rewriteMappingUri)
rewrittenJskos.totalCount = jskos.totalCount
return rewrittenJskos
})
.then(addContext)
.then(jskos => paginate(jskos, req, res))
.catch(errorHandler(res))
})
async function suggest(req, res) {
const { default: suggest } = await import("./lib/suggest.js")
return suggest(req.query)
.then(result => res.json(result))
.catch(errorHandler(res))
}
app.get("/concepts/suggest", suggest)
app.get("/concept/suggest", suggest) // deprecated
app.get("/suggest", suggest) // deprecated
app.get("/mappings/voc", (req, res) => {
const schemes = Promise.resolve(service.getSchemes())
schemes
.then(addContext)
.then(jskos => paginate(jskos, req, res))
.catch(errorHandler(res))
})
// status endpoint
app.get("/status", (req, res) => {
let status = {
config: _.omit(config, ["verbosity", "port", "mongo", "oauth", "wikibase"]),
}
let baseUrl = status.config.baseUrl
if (status.config.concepts) {
// Add endpoints related to concepts
status.data = `${baseUrl}data`
status.concepts = `${baseUrl}concepts`
status.suggest = `${baseUrl}concepts/suggest?search={searchTerms}`
}
if (status.config.mappings) {
// Add endpoints related to mappings
status.mappings = `${baseUrl}mappings`
status.config.mappings.toSchemeWhitelist = Object.values(service.schemes).map(scheme => ({ uri: scheme.uri, identifier: scheme.identifier }))
}
status.ok = 1
res.json(status)
})
// get single mapping
app.get("/mappings/:_id", (req, res) => {
service.getMapping(req.params._id)
.then(addContext)
.then(rewriteMappingUri)
.then(jskos => res.json(jskos))
.catch(errorHandler(res))
})
if (auth) {
// save a new mapping
app.post("/mappings", auth, (req, res) => {
service.saveMapping({
user: req.user,
body: req.body,
})
.then(addContext)
.then(rewriteMappingUri)
.then(jskos => res.status(201).json(jskos))
.catch(errorHandler(res))
})
// edit mapping
app.put("/mappings/:_id", auth, (req, res) => {
service.saveMapping({
_id: req.params._id,
user: req.user,
body: req.body,
})
.then(addContext)
.then(rewriteMappingUri)
.then(jskos => res.json(jskos))
.catch(errorHandler(res))
})
// delete mapping endpoint
app.delete("/mappings/:_id", auth, (req, res) => {
service.deleteMapping({
_id: req.params._id,
user: req.user,
})
.then(() => res.sendStatus(204))
.catch(errorHandler(res))
})
}
// start application
app.listen(port, () => {
config.log(`listening on port ${port}`)
})