-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
280 lines (240 loc) · 8.84 KB
/
index.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
// SPDX-FileCopyrightText: 2021 UdS AES <https://www.uni-saarland.de/lehrstuhl/frey.html>
// SPDX-License-Identifier: MIT
'use strict'
// Instantiate logger
const log = require('./source/logger.js')
log.info({ code: 300000 }, 'service instance started')
// Exit immediately on uncaught errors or unhandled promise rejections
process.on('unhandledRejection', function (error) {
log.fatal({ err: error, code: 600050 }, 'unhandled promise rejection')
process.exit(1)
})
process.on('uncaughtException', function (error) {
log.fatal({ err: error, code: 600050 }, 'uncaught exception')
process.exit(2)
})
// Handle shutdown signals gracefully
let server // global server object, populated when binding to port in init()
process.on('SIGINT', shutDownGracefully)
process.on('SIGTERM', shutDownGracefully)
// Load modules
const express = require('express')
const bodyParser = require('body-parser')
require('express-async-errors')
const cors = require('cors')
const _ = require('lodash')
const fs = require('fs-extra')
const delay = require('delay')
const addRequestId = require('express-request-id')()
const nunjucks = require('nunjucks')
const { createProxyMiddleware, responseInterceptor } = require('http-proxy-middleware')
const { processenv } = require('processenv')
const handlers = require('./source/simaas.js')
const responseUtils = require('./source/response_utils.js')
log.info({ code: 300010 }, 'successfully loaded modules')
// Declare global object for storing config; populated in `loadConfig`
let cfg = {}
// Define functions
async function checkIfConfigIsValid () {
const config = {
listenPort: parseInt(process.env.SIMAAS_LISTEN_PORT),
heartbeatPeriod: parseInt(process.env.SIMAAS_HEARTBEAT_PERIOD) || 3600 * 1000,
ui: {
staticFilesPath:
String(process.env.UI_STATIC_FILES_PATH) || './source/redoc.html',
urlPath: String(process.env.UI_URL_PATH) || '/ui'
},
oas: {
filePathStatic: './oas/simaas_oas3.json'
},
qpf: {
expose: processenv('QPF_SERVER_EXPOSE', false),
path: processenv('QPF_SERVER_PATH', '/knowledge-graph'),
target: processenv('QPF_SERVER_ORIGIN'),
configTemplate: processenv(
'QPF_SERVER_CONFIG',
'./templates/ldf-server_config.json'
),
dataTemplate: './templates/ldf-server_data.trig'
},
fs: process.env.SIMAAS_FS_PATH
}
config.oas.filePathDynamic = `${config.fs}/OAS.json` // careful, also in `simaas.js`
config.qpf.configFilePath = `${config.fs}/ldf-server_config.json`
config.qpf.sourceFilePath = `${config.fs}/data.trig` // careful, also in `simaas.js`
if (
!(
_.isNumber(config.listenPort) &&
config.listenPort > 0 &&
config.listenPort < 65535
)
) {
log.fatal(
{ code: 600020 },
'SIMAAS_LISTEN_PORT is ' +
config.listenPort +
' but must be an integer number larger than 0 and smaller than 65535'
)
process.exit(3)
}
if (!(_.isNumber(config.heartbeatPeriod) && config.heartbeatPeriod > 0)) {
log.fatal(
{ code: 600020 },
'SIMAAS_HEARTBEAT_PERIOD is ' +
config.heartbeatPeriod +
' but must be positive integer number larger than 0'
)
process.exit(4)
}
// TODO use ?? instead of || or processenv()-syntax
// TODO check validity of UI_STATIC_FILES_PATH
// TODO check validity of UI_URL_PATH
// TODO check validity of LOG_LEVEL?
// TODO check validity of ALIVE_EVENT_WAIT_TIME
// TODO check validity of API_SPECIFICATION_FILE_PATH
log.info({ code: 300020 }, 'configuration is valid, moving on')
return config
}
async function aliveLoop () {
while (true) {
await delay(cfg.heartbeatPeriod)
log.info({ code: 300100 }, 'service instance still running')
}
}
function shutDownGracefully () {
log.info({ code: 300050 }, 'received request to shut down')
// Stop receiving new requests, close server when all connections are ended
server.close(() => {
log.info('closed HTTP server')
// TODO Clean up resources
// Shut down the process
log.info({ code: 300060 }, 'shut down gracefully')
process.exit(0)
})
}
// Define main program
async function init () {
cfg = await checkIfConfigIsValid()
// Instantiate express-application and set up middleware-stack
const app = express()
app.options('*', handlers.serveRESTdesc)
app.use(bodyParser.json({ type: ['application/json', 'application/*+json'] }))
app.use(
bodyParser.text({
type: ['application/trig', 'text/turtle', 'text/n3'],
limit: '2mb'
})
)
app.use(bodyParser.raw({ type: ['application/octet-stream'], limit: '50mb' }))
app.use(cors())
app.use(addRequestId)
nunjucks.configure('templates', { autoescape: true, express: app })
// Expose UI iff UI_URL_PATH is not empty
if (cfg.ui.urlPath !== '') {
if (cfg.ui.staticFilesPath !== '') {
// Expose locally defined UI
app.use(cfg.ui.urlPath, express.static(cfg.ui.staticFilesPath))
log.info({ code: 300020 }, 'exposing UI as ' + cfg.ui.urlPath)
} else {
// Fall back to default-UI
log.fatal({ code: 600020 }, 'default-UI not implemented')
process.exit(6)
}
}
// Create child logger including req_id to be used in handlers
app.use((req, res, next) => {
req.log = log.child({ req_id: req.id })
req.log.info({ req: req }, `received ${req.method}-request on ${req.originalUrl}`) // XXX incompatible with GDPR!!
next()
})
// Expose Quad/Triple Pattern Fragmens interface by proxying requests to LDF-server
if (cfg.qpf.expose === true) {
// Ensure that config-file and data source exist
const qpfConfigExists = await fs.pathExists(cfg.qpf.configFilePath)
const qpfDataExists = await fs.pathExists(cfg.qpf.sourceFilePath)
if (qpfConfigExists === false) {
await fs.copy(cfg.qpf.configTemplate, cfg.qpf.configFilePath)
}
if (qpfDataExists === false) {
await fs.copy(cfg.qpf.dataTemplate, cfg.qpf.sourceFilePath)
}
// Proxy requests at designated path to instance of @ldf/server
app.use(
[cfg.qpf.path, '/assets'],
createProxyMiddleware({
target: cfg.qpf.target,
changeOrigin: true, // idk if this is really necessary..
// https://github.com/chimurai/http-proxy-middleware#intercept-and-manipulate-responses
selfHandleResponse: true,
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
const response = responseBuffer.toString('utf-8')
const oldURL = `${cfg.qpf.target}${cfg.qpf.path}`
const newURL = `${req.protocol}://${req.headers.host}${req.path}`
return response.replaceAll(oldURL, newURL)
})
})
)
}
// Rebuild dynamic OAS to ensure that upgrades are propagated but models are kept
await fs.remove(cfg.oas.filePathDynamic)
await handlers.updateOpenAPISpecification(null, null, 'read') // ensure existence
const objOfModels = await handlers.updateInternalListOfModels(null, null, 'read')
const modelIds = _.keys(objOfModels)
for (const modelId of modelIds) {
const modelRepresentation = objOfModels[modelId]
await handlers.updateOpenAPISpecification(
modelRepresentation.modelName,
modelRepresentation.schemata.parameter,
'set'
)
await handlers.updateOpenAPISpecification(
modelRepresentation.guid,
modelRepresentation.schemata.input,
'set'
)
}
// Read API-specification and initialize backend
const backend = handlers.initializeBackend(cfg.oas.filePathDynamic)
// Pass requests to middleware
app.get('/', handlers.getApiRoot)
app.get('/vocabulary', handlers.getApiVocabulary)
app.get('/models', handlers.getModelCollection)
app.get('/models/:id/types', handlers.getModelTypes)
app.get('/models/:id/units', handlers.getModelUnits)
app.get('/models/:id/variables', handlers.getModelVariables)
app.get('/models/:id/instances', handlers.getModelInstanceCollection)
app.get('/models/:id/instances/:id/experiments', handlers.getExperimentCollection)
app.use((req, res, next) => backend.handleRequest(req, req, res, next))
// Serialize any remaining errors as JSON
app.use(function (err, req, res, next) {
log.error(
'an internal server error occured and was caught at the end of the chain',
err
)
if (res.headersSent) {
return next(err)
}
responseUtils.sendProblemDetail(res, {
title: 'Internal Server Error',
status: 500
})
})
log.info({ code: 300030 }, 'configuration successfull')
// Start listening to incoming requests
server = app.listen(cfg.listenPort, function () {
log.info({ code: 300040 }, 'now listening on port ' + cfg.listenPort)
})
// XXX is this even functional?
app.on('error', function (error) {
log.fatal(
{ code: 600030, err: error },
'cannot bind to listening port ' + cfg.listenPort
)
process.exit(7)
})
}
// Enter main tasks
if (require.main === module) {
init()
aliveLoop()
}