-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
143 lines (116 loc) · 4.29 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
/*******************************************************************************
* Copyright (c) 2019 sensinov (www.sensinov.com)
* 41 Rue de la découverte, 31676 Labège, France
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Mahdi Ben Alaya (Project co-founder) - Management and initial specification,
* design, implementation, test and documentation.
* Ghada Gharbi (Project co-founder) - Management and initial specification,
* design, implementation, test and documentation.
* Authors:
* Ghada Gharbi < ghada.gharbi@sensinov.com >
******************************************************************************/
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const createError = require('http-errors');
const config = require ('./config/config');
const mongo = require ('./lib/mongo');
const auth= require('./auth/auth');
//bodyParser middleware to parse the request body and place the result in request.body of a route.
app.use(bodyParser.json( {type : ['application/json', 'application/ld+json']}));
app.use(function (err, req, res, next) {
let msg = {};
try {
if (err instanceof Error) {
if (err.statusCode ) {
msg = {
"title" : "Bad Request - Invalid body",
"type" : err.type,
"detail" : err.body
}
return res.status(err.statusCode ).send(msg)
}
}
throw err;
} catch (e) {
const finalError = new InternalServerError(err)
return res.status(finalError.statusCode).json()
}
});
//The urlencoded method within body-parser tells body-parser to extract data from the <form> element and add them to the body property in the request object.
app.use(bodyParser.urlencoded({extended: true}));
var checker = function (req, res, next) {
var headerAccept=req.header('Accept');
const headerRegExp=RegExp('.*/.*');
var headerContentType=req.header('Content-Type');
var headerContentLength=req.header('Content-Length');
if (req.method == "GET") {
if ((headerRegExp.test(headerAccept)) || (headerAccept === 'undefined')) {
next();
} else {
res.status(400);
res.send('Please Check Accept request header');
}
} else if ((req.method == "POST") || (req.method == "PATCH")) {
if (headerContentLength === undefined) {
res.status(411);
res.send('Please Check Content-Length request header');
} else {
if ((headerContentType == 'application/ld+json') || (headerContentType == 'application/json')) {
next();
} else {
res.status(400);
res.send('Please Check Content-Type request header');
}
}
} else if (req.method == "DELETE") {
next();
}
};
app.use(checker);
//Default page
app.get('/', function(req, res) {
res.send('Welcome to NGSI-LD Open source implementation');
});
function bootstrap (){
mongo.bootstrap(function (err, result) {
const entitiesRouter = require('./routes/entities');
const subscriptionsRouter = require('./routes/subscriptions');
const csRegistrationsRouter = require('./routes/csourceRegistrations');
const csSubscriptionsRouter = require('./routes/csourceSubscriptions');
const entityOperationsRouter = require ('./routes/entityOperations');
const temporalRouter = require ('./routes/temporal');
const usersRouter = require ('./routes/users');
app.post('/login', auth.authenticate);
// user Router
app.use(config.basePath, usersRouter);
// entities router
app.use(config.basePath, entitiesRouter);
//subscription router
app.use(config.basePath, subscriptionsRouter);
/*csourceRegistration router */
app.use(config.basePath, csRegistrationsRouter);
// csourceSubscription router
app.use(config.basePath, csSubscriptionsRouter);
//entityOperations router
app.use (config.basePath, entityOperationsRouter);
//entities temporal evolution Router
app.use(config.basePath, temporalRouter);
const serer=app.listen(config.serverPort, function () {
console.log('Listening on port: ' + config.serverPort);
});
});
}
bootstrap();
process.on('SIGTERM', function () {
server.close( function () {
console.log('Server terminated');
process.exit(0);
})
});