-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
126 lines (113 loc) · 3.69 KB
/
server.js
File metadata and controls
126 lines (113 loc) · 3.69 KB
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
import bodyParser from '@fastify/formbody';
import cors from '@fastify/cors';
import fastify from 'fastify';
import { fastifyRequestContext } from '@fastify/request-context';
import favicon from 'fastify-favicon';
import helmet from '@fastify/helmet';
import { join } from 'node:path';
import serveStatic from '@fastify/static';
import { stat } from 'node:fs/promises';
import { CONTEXT_STORE_KEYS, ENVS } from './constants/index.js';
import globalErrorHandler from './utilities/global-error-handler.js';
import gracefulShutdown from './hooks/graceful-shutdown.js';
import incomingTimestamp from './hooks/incoming-timestamp.js';
import logger from './utilities/logger.js';
import notFoundHandler from './utilities/not-found-handler.js';
import changePasswordAPI from './apis/change-password/index.js';
import deleteAccountAPI from './apis/delete-account/index.js';
import indexAPI from './apis/index/index.js';
import meAPI from './apis/me/index.js';
import refreshTokensAPI from './apis/refresh-tokens/index.js';
import signInAPI from './apis/sign-in/index.js';
import signOutAPI from './apis/sign-out/index.js';
import signOutFullAPI from './apis/sign-out-full/index.js';
import signUpAPI from './apis/sign-up/index.js';
import updateAccountAPI from './apis/update-account/index.js';
import userAPI from './apis/user/index.js';
import usersAPI from './apis/users/index.js';
/**
* Create Fastify server
* @param {string} APP_ENV application environment
* @returns {Promise<FastifyInstance>}
*/
export default async function createServer(APP_ENV) {
const server = fastify({
logger: APP_ENV === ENVS.development,
});
await server.register(bodyParser);
await server.register(cors);
const helmetOptions = {};
if (APP_ENV !== ENVS.production) {
helmetOptions.contentSecurityPolicy = false;
}
await server.register(helmet, helmetOptions);
await server.register(
fastifyRequestContext,
{
defaultStoreValues: {
[CONTEXT_STORE_KEYS.incomingTimestamp]: null,
[CONTEXT_STORE_KEYS.paginationQueryData]: null,
[CONTEXT_STORE_KEYS.searchQueryData]: null,
[CONTEXT_STORE_KEYS.userId]: null,
},
},
);
await server.register(
favicon,
{
name: 'favicon.ico',
path: './assets',
},
);
if (APP_ENV === ENVS.development) {
const documentationPath = join(process.cwd(), 'documentation');
try {
await stat(documentationPath);
await server.register(
serveStatic,
{
cacheControl: false,
prefix: '/docs/',
prefixAvoidTrailingSlash: true,
root: join(process.cwd(), 'documentation'),
},
);
logger('API documentation will be served');
} catch {
logger('API documentation will not be served: no static files found');
}
}
server.setErrorHandler(globalErrorHandler);
server.setNotFoundHandler(notFoundHandler);
server.addHook('onClose', gracefulShutdown);
server.addHook('onRequest', incomingTimestamp);
await Promise.all([
server.register(changePasswordAPI),
server.register(deleteAccountAPI),
server.register(indexAPI),
server.register(meAPI),
server.register(refreshTokensAPI),
server.register(signInAPI),
server.register(signOutAPI),
server.register(signOutFullAPI),
server.register(signUpAPI),
server.register(updateAccountAPI),
server.register(userAPI),
server.register(usersAPI),
]);
process.on(
'SIGINT',
async (signal) => {
logger(`Stopping the server (signal: ${signal})`);
await server.close();
},
);
process.on(
'SIGTERM',
async (signal) => {
logger(`Stopping the server (signal: ${signal})`);
await server.close();
},
);
return server;
}