Hemmelig.app/server.js

151 lines
4.8 KiB
JavaScript
Raw Normal View History

// Boot scripts
import('./src/server/bootstrap.js');
import config from 'config';
import path from 'path';
2022-08-27 19:44:13 +02:00
import { fileURLToPath } from 'url';
import importFastify from 'fastify';
import helmet from '@fastify/helmet';
import cors from '@fastify/cors';
import fstatic from '@fastify/static';
import cookie from '@fastify/cookie';
import jwt from '@fastify/jwt';
import rateLimit from '@fastify/rate-limit';
import adminDecorator from './src/server/decorators/admin.js';
import jwtDecorator from './src/server/decorators/jwt.js';
import userFeatures from './src/server/decorators/user-features.js';
import allowedIp from './src/server/decorators/allowed-ip.js';
import attachment from './src/server/decorators/attachment-upload.js';
2021-06-13 18:11:25 +02:00
import readCookieAllRoutesHandler from './src/server/prehandlers/cookie-all-routes.js';
import readOnlyHandler from './src/server/prehandlers/read-only.js';
import disableUserHandler from './src/server/prehandlers/disable-users.js';
import disableUserAccountCreationHandler from './src/server/prehandlers/disable-user-account-creation.js';
import restrictOrganizationEmailHandler from './src/server/prehandlers/restrict-organization-email.js';
import usersRoute from './src/server/controllers/admin/users.js';
import adminSettingsRoute from './src/server/controllers/admin/settings.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';
2022-08-29 22:28:48 +02:00
import statsRoute from './src/server/controllers/stats.js';
import healthzRoute from './src/server/controllers/healthz.js';
const isDev = process.env.NODE_ENV === 'development';
const MAX_FILE_BYTES = 1024 * config.get('file.size') * 1000; // Example: 1024 * 2 * 1000 = 2 024 000 bytes
const fastify = importFastify({
2021-06-13 18:11:25 +02:00
logger: config.get('logger'),
bodyLimit: MAX_FILE_BYTES,
2021-06-13 18:11:25 +02:00
});
// https://github.com/fastify/fastify-rate-limit
fastify.register(rateLimit, {
prefix: '/api/',
max: 10000,
timeWindow: '1 minute',
});
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,
strictTransportSecurity: false,
2023-02-13 19:16:20 +01:00
});
2021-06-22 20:26:25 +02:00
2021-06-13 18:11:25 +02:00
// https://github.com/fastify/fastify-cors
fastify.register(cors, { origin: config.get('cors') });
2021-06-13 18:11:25 +02: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);
// Define decorators
fastify.register(adminDecorator);
fastify.register(jwtDecorator);
fastify.register(userFeatures);
fastify.register(allowedIp);
fastify.register(attachment);
// Define pre handlers
fastify.addHook('preHandler', readCookieAllRoutesHandler(fastify));
fastify.addHook('preHandler', disableUserHandler);
fastify.addHook('preHandler', disableUserAccountCreationHandler);
fastify.addHook('preHandler', readOnlyHandler);
fastify.addHook('preHandler', restrictOrganizationEmailHandler);
2021-06-13 18:11:25 +02:00
// Register our routes before the static content
fastify.register(authenticationRoute, {
prefix: '/api/authentication',
});
fastify.register(accountRoute, {
prefix: '/api/account',
});
fastify.register(usersRoute, {
prefix: '/api/admin/users',
});
fastify.register(adminSettingsRoute, {
prefix: '/api/admin/settings',
});
fastify.register(downloadRoute, { prefix: '/api/download' });
fastify.register(secretRoute, { prefix: '/api/secret' });
2022-08-29 22:28:48 +02:00
fastify.register(statsRoute, { prefix: '/api/stats' });
fastify.register(healthzRoute, { prefix: '/api/healthz' });
fastify.register(healthzRoute, { prefix: '/healthz' });
2021-06-13 18:11:25 +02:00
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
2022-08-27 19:44:13 +02:00
const staticPath = path.join(__dirname, !isDev ? 'build' : '');
2021-06-13 18:11:25 +02:00
// Static frontend for the production build
if (!isDev) {
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);
2023-04-16 18:35:56 +02:00
fastify.get('/signout', serveIndex);
fastify.get('/account*', serveIndex);
fastify.get('/terms', serveIndex);
2021-06-13 18:57:41 +02:00
}
2021-06-13 18:11:25 +02:00
const startServer = async () => {
try {
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();