2022-08-27 19:25:14 +02:00
|
|
|
import config from 'config';
|
|
|
|
import path from 'path';
|
2022-08-27 19:44:13 +02:00
|
|
|
import { fileURLToPath } from 'url';
|
2022-08-27 19:25:14 +02:00
|
|
|
import importFastify from 'fastify';
|
2022-08-27 19:34:22 +02:00
|
|
|
import helmet from '@fastify/helmet';
|
|
|
|
import cors from '@fastify/cors';
|
|
|
|
import fstatic from '@fastify/static';
|
2023-03-11 17:58:35 +01:00
|
|
|
import cookie from '@fastify/cookie';
|
|
|
|
import jwt from '@fastify/jwt';
|
|
|
|
import jwtDecorator from './src/server/decorators/jwt.js';
|
2022-08-27 19:25:14 +02:00
|
|
|
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';
|
2022-09-07 20:31:51 +02:00
|
|
|
import keyGeneration from './src/server/decorators/key-generation.js';
|
2021-06-13 18:11:25 +02:00
|
|
|
|
2022-08-27 19:25:14 +02:00
|
|
|
import authenticationRoute from './src/server/controllers/authentication.js';
|
|
|
|
import accountRoute from './src/server/controllers/account.js';
|
2022-09-08 17:06:09 +02:00
|
|
|
import downloadRoute from './src/server/controllers/download.js';
|
2022-08-27 19:25:14 +02:00
|
|
|
import secretRoute from './src/server/controllers/secret.js';
|
2022-08-29 22:28:48 +02:00
|
|
|
import statsRoute from './src/server/controllers/stats.js';
|
2022-08-27 19:25:14 +02:00
|
|
|
import healthzRoute from './src/server/controllers/healthz.js';
|
|
|
|
|
2023-04-02 17:18:19 +02:00
|
|
|
const isDev = process.env.NODE_ENV === 'development';
|
|
|
|
|
2022-09-07 19:19:35 +02:00
|
|
|
const MAX_FILE_BYTES = 1024 * config.get('file.size') * 1000; // Example: 1024 * 2 * 1000 = 2 024 000 bytes
|
|
|
|
|
2022-08-27 19:25:14 +02:00
|
|
|
const fastify = importFastify({
|
2021-06-13 18:11:25 +02:00
|
|
|
logger: config.get('logger'),
|
2022-09-07 19:19:35 +02:00
|
|
|
bodyLimit: MAX_FILE_BYTES,
|
2021-06-13 18:11:25 +02:00
|
|
|
});
|
|
|
|
|
2021-06-22 20:26:25 +02:00
|
|
|
// https://github.com/fastify/fastify-helmet
|
2023-02-13 19:16:20 +01:00
|
|
|
fastify.register(helmet, {
|
|
|
|
contentSecurityPolicy: {
|
|
|
|
directives: {
|
|
|
|
'font-src': ["'self'", 'https://rsms.me/'],
|
|
|
|
'script-src': ["'self'", "'unsafe-inline'"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
crossOriginEmbedderPolicy: false,
|
|
|
|
});
|
2021-06-22 20:26:25 +02:00
|
|
|
|
2021-06-13 18:11:25 +02:00
|
|
|
// https://github.com/fastify/fastify-cors
|
2022-08-27 19:25:14 +02:00
|
|
|
fastify.register(cors, { origin: config.get('cors') });
|
2021-06-13 18:11:25 +02:00
|
|
|
|
2023-03-11 17:58:35 +01:00
|
|
|
// https://github.com/fastify/fastify-jwt#cookie
|
|
|
|
fastify.register(jwt, {
|
|
|
|
secret: config.get('jwt.secret'),
|
|
|
|
cookie: {
|
|
|
|
cookieName: config.get('jwt.cookie'),
|
|
|
|
signed: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
fastify.register(cookie);
|
|
|
|
|
2021-06-18 14:57:10 +02:00
|
|
|
// Define decorators
|
2023-03-11 17:58:35 +01:00
|
|
|
fastify.register(jwtDecorator);
|
2022-08-27 19:25:14 +02:00
|
|
|
fastify.register(userFeatures);
|
|
|
|
fastify.register(rateLimit);
|
|
|
|
fastify.register(allowedIp);
|
|
|
|
fastify.register(attachment);
|
2022-09-07 20:31:51 +02:00
|
|
|
fastify.register(keyGeneration);
|
2021-06-18 14:57:10 +02:00
|
|
|
|
2021-06-13 18:11:25 +02:00
|
|
|
// Register our routes before the static content
|
2022-09-08 17:06:09 +02:00
|
|
|
if (!config.get('user.disabled')) {
|
|
|
|
fastify.register(authenticationRoute, {
|
|
|
|
prefix: '/api/authentication',
|
|
|
|
});
|
2021-06-18 14:57:10 +02:00
|
|
|
|
2022-09-08 17:06:09 +02:00
|
|
|
fastify.register(accountRoute, {
|
|
|
|
prefix: '/api/account',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fastify.register(downloadRoute, { prefix: '/api/download' });
|
2022-08-27 19:25:14 +02:00
|
|
|
fastify.register(secretRoute, { prefix: '/api/secret' });
|
2022-08-29 22:28:48 +02:00
|
|
|
fastify.register(statsRoute, { prefix: '/api/stats' });
|
2022-08-27 19:25:14 +02:00
|
|
|
fastify.register(healthzRoute, { prefix: '/api/healthz' });
|
|
|
|
fastify.register(healthzRoute, { prefix: '/healthz' });
|
2021-06-13 18:11:25 +02:00
|
|
|
|
2023-04-02 17:18:19 +02:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = path.dirname(__filename);
|
2022-08-27 19:44:13 +02:00
|
|
|
|
2023-04-02 20:23:20 +02:00
|
|
|
const staticPath = path.join(__dirname, !isDev ? 'build' : '');
|
2021-06-13 18:11:25 +02:00
|
|
|
|
2023-04-02 17:18:19 +02:00
|
|
|
// Static frontend for the production build
|
|
|
|
if (!isDev) {
|
2022-08-27 19:25:14 +02:00
|
|
|
fastify.register(fstatic, {
|
2021-06-13 18:11:25 +02:00
|
|
|
root: staticPath,
|
|
|
|
route: '/*',
|
|
|
|
});
|
|
|
|
|
2021-06-13 18:57:41 +02:00
|
|
|
function serveIndex(_, reply) {
|
|
|
|
return reply.sendFile('index.html');
|
|
|
|
}
|
|
|
|
|
2021-07-08 14:05:45 +02:00
|
|
|
fastify.get('/secret/*', serveIndex);
|
2021-06-13 18:57:41 +02:00
|
|
|
fastify.get('/about', serveIndex);
|
2021-07-08 14:05:45 +02:00
|
|
|
fastify.get('/privacy', serveIndex);
|
|
|
|
fastify.get('/api-docs', serveIndex);
|
|
|
|
fastify.get('/signin', serveIndex);
|
|
|
|
fastify.get('/signup', serveIndex);
|
2021-06-21 16:51:57 +02:00
|
|
|
fastify.get('/account', serveIndex);
|
2022-08-21 18:32:39 +02:00
|
|
|
fastify.get('/terms', serveIndex);
|
2021-06-13 18:57:41 +02:00
|
|
|
}
|
2021-06-13 18:11:25 +02:00
|
|
|
|
|
|
|
const startServer = async () => {
|
|
|
|
try {
|
2022-08-27 19:34:22 +02:00
|
|
|
await fastify.listen({ port: config.get('port'), host: config.get('localHostname') });
|
2021-06-13 18:11:25 +02:00
|
|
|
} catch (err) {
|
|
|
|
fastify.log.error(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
startServer();
|