-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
111 lines (92 loc) · 3.83 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const newrelic = require('newrelic');
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const rateLimit = require('express-rate-limit');
const analyticsRouter = require('./routes/analytics');
const healthcareInstitutionRouter = require('./routes/healthcareInstitution');
const indexRouter = require('./routes/index');
const newsRouter = require('./routes/news');
const statsRouter = require('./routes/stats');
const travelAlertRouter = require('./routes/travelAlert');
const imageProxyRouter = require('./routes/imageProxy');
const sharingRouter = require('./routes/sharing');
const v2AnalyticsRouter = require('./routes/v2/analytics');
const v2CacheRouter = require('./routes/v2/cache');
const v2StatsRouter = require('./routes/v2/stats');
const v3AnalyticsRouter = require('./routes/v3/analytics');
const v4AnalyticsRouter = require('./routes/v4/analytics');
const v5AnalyticsRouter = require('./routes/v5/analytics');
const v3StatsRouter = require('./routes/v3/stats');
const v3StatsBnoRouter = require('./routes/v3/stats/bno');
const v3StatsWorldometerRouter = require('./routes/v3/stats/worldometer');
const cors = require('cors');
const app = express();
app.use(cors({ origin: ["https://coronatracker.com", /\.coronatracker\.com$/] }));
//rate limiting
const rateLimitTimeMinutes = process.env.RATE_LIMIT_TIME_MINUTES;
const rateLimitRequests = process.env.RATE_LIMIT_REQUESTS;
const whitelist = process.env.IP_WHITELIST ? process.env.IP_WHITELIST.split(',') : [];
if (rateLimitTimeMinutes && rateLimitRequests) {
app.use(rateLimit({
windowMs: parseInt(rateLimitTimeMinutes) * 60 * 1000,
max: parseInt(rateLimitRequests),
skip: function(req) {
//skipping whitelisted and internal IP's
return !req.ip || req.ip === '::1' || whitelist.indexOf(req.ip) > -1;
}
}));
// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// see https://expressjs.com/en/guide/behind-proxies.html
app.set('trust proxy', 1);
}
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('dev', {
skip: function(req, res) { return res.statusCode < 400 }
}));
app.use('/', indexRouter);
app.use('/news', newsRouter);
app.use('/stats', statsRouter);
app.use('/analytics', analyticsRouter);
app.use('/v1/news', newsRouter);
app.use('/v1/healthcare-institution', healthcareInstitutionRouter);
app.use('/v1/stats', statsRouter);
app.use('/v1/travel-alert', travelAlertRouter);
app.use('/v1/analytics', analyticsRouter);
app.use('/v2/analytics', v2AnalyticsRouter);
app.use('/v2/stats', v2StatsRouter);
app.use('/v3/analytics', v3AnalyticsRouter);
app.use('/v4/analytics', v4AnalyticsRouter);
app.use('/v5/analytics', v5AnalyticsRouter);
app.use('/v3/stats', v3StatsRouter);
app.use('/v3/stats/bno', v3StatsBnoRouter);
app.use('/v3/stats/worldometer', v3StatsWorldometerRouter);
app.use('/v2/cache', v2CacheRouter);
app.use('/image-proxy', imageProxyRouter);
app.use('/sharing', sharingRouter);
app.use('/doc', express.static(__dirname + '/public'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
console.log('[ERROR]', err.message);
// render the error page
const status = err.status || 500;
res.status(status);
// res.render('error');
res.json({ status, message: 'An error has occurred.' })
});
module.exports = app;