-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
96 lines (83 loc) · 3.21 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require('dotenv').config();
const fs = require('fs');
const glob = require('glob');
const cluster = require('cluster');
const redis = require('redis');
process.env.EGS_APP_ROOT = __dirname;
if (cluster.isMaster) {
let processes = require('os').cpus().length;
if (process.env.WORKER_PROCESSES) {
processes = parseInt(process.env.WORKER_PROCESSES);
}
console.log("Spawning " + processes + " worker processes...");
for (let i = 0; i < processes; i += 1) {
cluster.fork();
}
// TODO: Consider making master process a worker
} else {
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const http = require('http');
const path = require('path');
const app = express();
const helmet = require('helmet');
// preload INI into RAM on bootstrap/fork, everything needs it
// and we want to avoid synchronous processes in the request pipeline
const settings = require('./lib/EGSSettings');
const limiter = require('./lib/RateLimiter');
app.set('port', process.env.PORT || 8080);
// disable helmet, as these are being set upstream
// app.use(helmet());
app.disable('x-powered-by');
app.use(cors());
app.use(bodyParser.json());
// configure rate limiting
if (process.env.NO_RATE_LIMIT ||
settings.getSetting('api', 'rate_limit_disable') === true) {
console.warn("EXPLICIT SECURITY BYPASS: Rate limiting security disabled");
} else {
if (settings.getSetting('api', 'behind_reverse_proxy') === true) {
app.enable('trust proxy');
}
app.use(limiter);
}
let controller_path = path.join(__dirname, 'controllers');
let folders = glob.sync(path.join(controller_path, '**/'));
folders = folders.slice(1,);
folders.forEach((version_folder) => {
let split = version_folder.split(path.sep);
let version = split[split.length - 2];
if (version === 'base') {
return;
}
console.info("Loading controllers for API version " + version + "...");
fs.readdirSync(version_folder).forEach((jsfile) => {
let router = express.Router();
if (jsfile.substr(-3) === '.js') {
let controller_path = path.join(version_folder, jsfile);
console.info(" > " + path.basename(controller_path));
let controller = require(controller_path);
controller(router);
}
app.use('/' + version, router);
});
});
console.info("Controllers loaded.\n");
app.get('/', (req, res) => {
res.json({
success: true,
message: 'EthGasStation API OK'
});
})
// default 404 handler
app.use((req, res, next) => {
res.status(404).json({
'success': false,
'message': 'The requested endpoint does not exist.'
});
});
http.createServer(app).listen(app.get('port'), () => {
console.log('Express worker process ' + cluster.worker.id + ' listening on ' + app.get('port'));
});
}