This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
73 lines (60 loc) · 2.49 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
const express = require('express');
const Http = require('http');
const Https = require('https');
var auth = require('http-auth');
const fs = require('fs');
const mongoose = require('mongoose');
const subscribeapi = require('./subscribeapi');
const queryapi = require('./queryapi');
const tasks = require('./tasks');
const bodyParser = require('body-parser');
const Config = require('./config.js');
let DB_URL = Config.mongodb_server_url;
console.log("Connecting to " + DB_URL + "...");
mongoose.connect(DB_URL, { useMongoClient: true }).then(
() => {
console.log("Connected to database");
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/subscribe', subscribeapi);
app.use('/', queryapi);
if (!Config.server_port) {
let server_port = process.env.PORT;
}
if (Config.enable_https) {
console.log("Https Server is enabled. Now loading credentials...");
let privateKey = fs.readFileSync(Config.private_key_path, 'utf8');
let certificate = fs.readFileSync(Config.certificate_path, 'utf8');
let credentials = {key: privateKey, cert: certificate};
if (!Config.auth) {
let basicAuth = auth.basic(Config.auth);
let httpsServer = Https.createServer(basicAuth, credentials, app);
console.log("Http authorization is enabled.");
} else {
let httpsServer = Https.createServer(credentials, app);
}
httpsServer.listen(Config.server_port, () => {
let host = httpsServer.address().address;
let port = httpsServer.address().port;
console.log("Https Server running at https://%s:%s", host, port);
});
} else {
if (!Config.auth) {
let basicAuth = auth.basic(Config.auth);
let httpServer = Http.createServer(basicAuth, app);
console.log("Http authorization is enabled.");
} else {
let httpServer = Http.createServer(app);
}
httpServer.listen(Config.server_port, () => {
let host = httpServer.address().address;
let port = httpServer.address().port;
console.log("Http Server running at http://%s:%s", host, port);
});
}
tasks.scheduleQueryTask();
},
err => { console.log(err.message) }
);
mongoose.connection.on('disconnected', () => { console.log('Disconnected.')});