-
Notifications
You must be signed in to change notification settings - Fork 56
/
server.js
109 lines (91 loc) · 3.6 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
import replace from 'replace-in-file';
import config from 'config';
import path from 'path';
import { fileURLToPath } from 'url';
import importFastify from 'fastify';
import helmet from '@fastify/helmet';
import cors from '@fastify/cors';
import multipart from '@fastify/multipart';
import fstatic from '@fastify/static';
import jwt from './src/server/decorators/jwt.js';
import userFeatures from './src/server/decorators/user-features.js';
import rateLimit from './src/server/decorators/rate-limit.js';
import allowedIp from './src/server/decorators/allowed-ip.js';
import attachment from './src/server/decorators/attachment-upload.js';
import keyGeneration from './src/server/decorators/key-generation.js';
import authenticationRoute from './src/server/controllers/authentication.js';
import accountRoute from './src/server/controllers/account.js';
import downloadROute from './src/server/controllers/download.js';
import secretRoute from './src/server/controllers/secret.js';
import statsRoute from './src/server/controllers/stats.js';
import healthzRoute from './src/server/controllers/healthz.js';
const fastify = importFastify({
logger: config.get('logger'),
});
const MAX_FILE_BYTES = 1024 * config.get('file.size') * 1000; // Example: 1024 * 2 * 1000 = 2 024 000 bytes
fastify.register(import('@fastify/compress'));
// https://github.com/fastify/fastify-helmet
fastify.register(helmet, { contentSecurityPolicy: false, crossOriginEmbedderPolicy: false });
// https://github.com/fastify/fastify-cors
fastify.register(cors, { origin: config.get('cors') });
fastify.register(multipart, {
attachFieldsToBody: true,
limits: {
files: config.get('file.limit'),
fileSize: MAX_FILE_BYTES,
},
});
// Define decorators
fastify.register(jwt);
fastify.register(userFeatures);
fastify.register(rateLimit);
fastify.register(allowedIp);
fastify.register(attachment);
fastify.register(keyGeneration);
// Register our routes before the static content
fastify.register(authenticationRoute, {
prefix: '/api/authentication',
});
fastify.register(accountRoute, {
prefix: '/api/account',
});
fastify.register(downloadROute, { prefix: '/api/download' });
fastify.register(secretRoute, { prefix: '/api/secret' });
fastify.register(statsRoute, { prefix: '/api/stats' });
fastify.register(healthzRoute, { prefix: '/api/healthz' });
fastify.register(healthzRoute, { prefix: '/healthz' });
// Static frontend for the production build
if (process.env.NODE_ENV !== 'development') {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const staticPath = path.join(__dirname, 'build');
// Filthy hack, but it works for now. Soon to implement config from the server.
replace.sync({
files: staticPath + '/**/*.html',
from: [/{{NODE_ENV}}/g, /__SECRET_CONFIG__/g],
to: [process.env.NODE_ENV, `'${JSON.stringify(config.get('__client_config'))}';`],
});
fastify.register(fstatic, {
root: staticPath,
route: '/*',
});
function serveIndex(_, reply) {
return reply.sendFile('index.html');
}
fastify.get('/secret/*', serveIndex);
fastify.get('/about', serveIndex);
fastify.get('/privacy', serveIndex);
fastify.get('/api-docs', serveIndex);
fastify.get('/signin', serveIndex);
fastify.get('/signup', serveIndex);
fastify.get('/account', serveIndex);
fastify.get('/terms', serveIndex);
}
const startServer = async () => {
try {
await fastify.listen({ port: config.get('port'), host: config.get('localHostname') });
} catch (err) {
fastify.log.error(err);
}
};
startServer();