forked from C3BI-pasteur-fr/Cassandre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (57 loc) · 2.1 KB
/
app.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
#!/usr/bin/env node
/*
* Handle the connection to MongoDB and the server configuration.
* Then pass the app and the db to the router.
*
*/
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var format = require('util').format;
var config = require('./config');
var router = require('./router');
var host = config('db.host', 'localhost');
var port = config('db.port', 27017);
var database = config('db.dbName', 'cassandre');
var url = format('mongodb://%s:%d/%s', host, port, database);
MongoClient.connect(url, function (err, db) {
if (err) {
throw err;
}
console.log('Connected to the ' + database + ' database on port ' + port);
// DATABASE CONGIGURATION
// =========================================================================
db.collection("datasets").createIndex({ name: 1 }, { unique: true, background: true });
// SERVER CONGIGURATION
// =========================================================================
var app = express();
var serverPort = config('web.port', 8080);
var serverHost = config('web.host', 'localhost');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.listen(serverPort, serverHost, function () {
console.log('Server listening to ' + serverHost + ' on port ' + serverPort);
router(app, db);
});
// EXIT HANDLERS
// =========================================================================
var gracefulExit = function (signal, code) {
db.close(function (err) {
if (err) {
throw err;
}
console.log("Application terminated on " + signal);
process.exit(code);
});
}
db.on('close', function () {
console.log('Closing connection with the ' + database + ' database');
});
process.on('SIGINT', function () {
gracefulExit('SIGINT', 130);
});
process.on('SIGTERM', function () {
gracefulExit('SIGTERM', 143);
});
});