This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (75 loc) · 1.78 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const Koa = require('koa');
const protect = require('koa-protect');
const ratelimit = require('koa-ratelimit');
const bodyParser = require('koa-bodyparser')();
const compress = require('koa-compress')();
const cors = require('@koa/cors')();
const helmet = require('koa-helmet')();
const logger = require('koa-logger')();
const session = require('koa-session');
const passport = require('koa-passport');
const errorHandler = require('./middleware/error.middleware');
const applyApiMiddleware = require('./api');
const { isDevelopment } = require('./config');
const server = new Koa();
server.use(
require('koa-body')({
multipart: true,
formidable: { keepExtensions: true },
}),
);
server.keys = ['secret'];
server.use(session({}, server));
require('./lib/auth');
server.use(passport.initialize());
server.use(passport.session());
server.use(
protect.koa.sqlInjection({
body: true,
loggerFunction: console.error,
}),
);
server.use(
protect.koa.xss({
body: true,
loggerFunction: console.error,
}),
);
server.use(require('koa-static')(`${process.env.STORAGE_DIR}`))
const db = new Map();
server.use(
ratelimit({
driver: 'memory',
db,
duration: 60000,
errorMessage: 'Sometimes You Just Have to Slow Down.',
id: ctx => ctx.ip,
headers: {
remaining: 'Rate-Limit-Remaining',
reset: 'Rate-Limit-Reset',
total: 'Rate-Limit-Total',
},
max: 100,
}),
);
require('koa2-ctx-validator')(server);
/**
* Add here only development middlewares
*/
if (isDevelopment) {
server.use(logger);
}
/**
* Pass to our server instance middlewares
*/
server
.use(errorHandler)
.use(helmet)
.use(compress)
.use(cors)
.use(bodyParser);
/**
* Apply to our server the api router
*/
applyApiMiddleware(server);
module.exports = server;