-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
212 lines (190 loc) · 5.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const config = require('config');
const fastify = require('fastify');
const fastifySwagger = require('fastify-swagger');
const fastifyJWT = require('fastify-jwt');
const fastifyHelmet = require('fastify-helmet');
const fastifyAuth = require('fastify-auth');
const fastifyCors = require('fastify-cors');
const routes = require('./routes');
const { auth } = require('./lib');
const APPLICATION_PORT = config.get('port');
const JWT_SECRET = config.get('jwt.secret');
const ROUTE_PREFIX = config.get('routePrefix');
const FASTIFY_OPTIONS = config.get('fastifyOptions');
// Initialize swagger
const initSwagger = () => {
const swaggerOptions = config.get('swagger');
return {
routePrefix: `${ROUTE_PREFIX}/documentation`,
swagger: {
info: {
title: 'Project AKL 2020 Web Backend - Core',
description: 'Project AKL 2020 Web Backend - Core',
version: '1.0.0',
},
host: swaggerOptions.host,
schemes: swaggerOptions.schemes,
securityDefinitions: {
bearerAuth: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
},
},
security: [{
bearerAuth: [],
}],
consumes: ['application/json'],
produces: ['application/json'],
tags: [
{
name: 'Integration',
description: '...',
}, {
name: 'Utility',
description: 'Utility endpoints',
}, {
name: 'User',
description: 'CRUD endpoints related to Users',
}, {
name: 'Team',
description: 'CRUD endpoints related to Teams',
}, {
name: 'Season',
description: 'CRUD endpoints related to Seasons',
}, {
name: 'Devtest',
description: 'These endpoints should only be used for testing. DO NOT USE IN REAL APPLICATION!',
}, {
name: 'Text',
description: 'Endpoints for managing text resources on the site',
},
],
},
exposeRoute: true,
};
};
/**
* Routes
* There is try catch inside the loop, because if we want to authenticate
* user, we must export route function instead of just options object.
*
* See routes/integration/login-email.js vs routes/integration/login-steam.js
* for more information.
*/
const userRoute = async (server) => {
Object.keys(routes.user).forEach((key) => {
try {
server.route(routes.user[key]);
} catch (error) {
routes.user[key](server);
}
});
};
const teamRoute = async (server) => {
Object.keys(routes.team).forEach((key) => {
try {
server.route(routes.team[key]);
} catch (error) {
routes.team[key](server);
}
});
};
const seasonRoute = async (server) => {
Object.keys(routes.season).forEach((key) => {
try {
server.route(routes.season[key]);
} catch (error) {
routes.season[key](server);
}
});
};
const integrationRoute = async (server) => {
Object.keys(routes.integration).forEach((key) => {
try {
server.route(routes.integration[key]);
} catch (error) {
routes.integration[key](server);
}
});
};
const utilityRoute = async (server) => {
Object.keys(routes.utility).forEach((key) => {
try {
server.route(routes.utility[key]);
} catch (error) {
routes.utility[key](server);
}
});
};
const textRoute = async (server) => {
Object.keys(routes.text).forEach((key) => {
try {
server.route(routes.text[key]);
} catch (error) {
routes.text[key](server);
}
});
};
const commentRoute = async (server) => {
Object.keys(routes.comment).forEach((key) => {
try {
server.route(routes.comment[key]);
} catch (error) {
routes.comment[key](server);
}
});
};
/**
* Init server
* @param {Object} options Optional.
*/
const initServer = async () => {
const server = fastify({
logger: FASTIFY_OPTIONS.logger,
ignoreTrailingSlash: FASTIFY_OPTIONS.ignoreTrailingSlash,
ajv: {
customOptions: {
removeAdditional: 'all',
},
},
});
// Register plugins and routes
server
.decorate('verifyEmailAndPassword', auth.verifyEmailAndPassword)
.decorate('verifyJWT', auth.verifyJWT)
.register(fastifySwagger, initSwagger())
.register(fastifyJWT, { secret: JWT_SECRET })
.register(fastifyHelmet, {
contentSecurityPolicy: {
directives: {
defaultSrc: ['\'self\''],
styleSrc: ['\'self\'', '\'unsafe-inline\''],
imgSrc: ['\'self\'', 'data:', 'validator.swagger.io'],
scriptSrc: ['\'self\'', 'https: \'unsafe-inline\''],
},
},
})
.register(fastifyAuth)
.register(fastifyCors, {
origin: (origin, cb) => {
cb(null, true);
},
})
.register(userRoute, { prefix: `${ROUTE_PREFIX}/user` })
.register(utilityRoute, { prefix: `${ROUTE_PREFIX}/utility` })
.register(teamRoute, { prefix: `${ROUTE_PREFIX}/team` })
.register(seasonRoute, { prefix: `${ROUTE_PREFIX}/season` })
.register(integrationRoute, { prefix: `${ROUTE_PREFIX}/integration` })
.register(textRoute, { prefix: `${ROUTE_PREFIX}/text` })
.register(commentRoute, { prefix: `${ROUTE_PREFIX}/comment` });
return {
start: async () => {
await server.listen(APPLICATION_PORT, '0.0.0.0');
return server;
},
};
};
module.exports = {
initServer,
};