Hemmelig.app/server.js

110 lines
3.5 KiB
JavaScript
Raw Normal View History

import replace from 'replace-in-file';
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 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';
2021-06-13 18:11:25 +02:00
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 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
});
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
fastify.register(cors, { origin: config.get('cors') });
2021-06-13 18:11:25 +02:00
// Define decorators
fastify.register(jwt);
fastify.register(userFeatures);
fastify.register(rateLimit);
fastify.register(allowedIp);
fastify.register(attachment);
fastify.register(keyGeneration);
2021-06-13 18:11:25 +02:00
// Register our routes before the static content
if (!config.get('user.disabled')) {
fastify.register(authenticationRoute, {
prefix: '/api/authentication',
});
fastify.register(accountRoute, {
prefix: '/api/account',
});
}
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
// Static frontend for the production build
if (process.env.NODE_ENV !== 'development') {
2022-08-27 19:44:13 +02:00
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
2021-06-13 18:11:25 +02:00
const staticPath = path.join(__dirname, 'build');
replace.sync({
files: staticPath + '/**/*.html',
2021-07-08 14:05:45 +02:00
from: [/{{NODE_ENV}}/g, /__SECRET_CONFIG__/g],
to: [process.env.NODE_ENV, `'${JSON.stringify(config.get('__client_config'))}';`],
2021-06-13 18:11:25 +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);
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();