forked from chrisleekr/binance-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-frontend.js
83 lines (69 loc) · 2.45 KB
/
server-frontend.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
const compression = require('compression');
const express = require('express');
const cors = require('cors');
const path = require('path');
const config = require('config');
const requestIp = require('request-ip');
const { RateLimiterRedis } = require('rate-limiter-flexible');
const fileUpload = require('express-fileupload');
const { maskConfig } = require('./cronjob/trailingTradeHelper/util');
const { cache } = require('./helpers');
const maxConsecutiveFails = config.get(
'authentication.loginLimiter.maxConsecutiveFails'
);
const loginLimiter = new RateLimiterRedis({
redis: cache.redis,
keyPrefix: 'login',
points: maxConsecutiveFails,
duration: config.get('authentication.loginLimiter.duration'),
blockDuration: config.get('authentication.loginLimiter.blockDuration')
});
const { configureWebServer } = require('./frontend/webserver/configure');
const { configureWebSocket } = require('./frontend/websocket/configure');
const { configureLocalTunnel } = require('./frontend/local-tunnel/configure');
const { configureBullBoard } = require('./frontend/bull-board/configure');
const runFrontend = async serverLogger => {
const logger = serverLogger.child({ server: 'frontend' });
logger.info(
{ config: maskConfig(config) },
`API ${config.get('mode')} frontend started on`
);
const app = express();
app.use(compression());
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(
fileUpload({
safeFileNames: true,
useTempFiles: true,
tempFileDir: '/tmp/'
})
);
app.use(express.static(path.join(__dirname, '/../public')));
// Must configure bull board before listen.
configureBullBoard(app, logger);
const server = app.listen(80);
if (config.get('authentication.enabled')) {
const rateLimiterMiddleware = async (req, res, next) => {
const clientIp = requestIp.getClientIp(req);
const rateLimiterLogin = await loginLimiter.get(clientIp);
if (rateLimiterLogin.remainingPoints <= 0) {
res
.status(403)
.send(
`You are blocked until ${new Date(
Date.now() + rateLimiterLogin.msBeforeNext
)}.`
);
} else {
next();
}
};
app.use(rateLimiterMiddleware);
}
await configureWebServer(app, logger, { loginLimiter });
await configureWebSocket(server, logger, { loginLimiter });
await configureLocalTunnel(logger);
};
module.exports = { runFrontend };