hexabot/api/src/main.ts

102 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-09-10 09:50:11 +00:00
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import bodyParser from 'body-parser';
import session from 'express-session';
import moduleAlias from 'module-alias';
import { resolveDynamicProviders } from 'nestjs-dynamic-providers';
import passport from 'passport';
moduleAlias.addAliases({
'@': __dirname,
});
2024-10-26 09:36:47 +00:00
import { HexabotModule } from './app.module';
2024-09-10 09:50:11 +00:00
import { config } from './config';
import { LoggerService } from './logger/logger.service';
2024-09-29 10:49:05 +00:00
import { seedDatabase } from './seeder';
2024-09-10 09:50:11 +00:00
import { swagger } from './swagger';
2024-10-26 11:02:39 +00:00
import { getSessionStore } from './utils/constants/session-store';
2024-09-10 09:50:11 +00:00
import { ObjectIdPipe } from './utils/pipes/object-id.pipe';
2024-11-15 16:39:12 +00:00
import { RedisIoAdapter } from './websocket/adapters/redis-io.adapter';
2024-09-10 09:50:11 +00:00
async function bootstrap() {
const isProduction = config.env.toLowerCase().includes('prod');
await resolveDynamicProviders();
2024-10-26 09:36:47 +00:00
const app = await NestFactory.create(HexabotModule, {
2024-09-10 09:50:11 +00:00
bodyParser: false,
});
const rawBodyBuffer = (req, res, buf, encoding) => {
if (buf?.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
};
app.use(bodyParser.urlencoded({ verify: rawBodyBuffer, extended: true }));
app.use(bodyParser.json({ verify: rawBodyBuffer }));
app.enableCors({
origin: config.security.cors.allowOrigins,
methods: config.security.cors.methods,
credentials: config.security.cors.allowCredentials,
allowedHeaders: config.security.cors.headers.split(','),
});
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
2024-09-24 11:15:30 +00:00
transform: true,
2024-09-10 09:50:11 +00:00
// forbidNonWhitelisted: true,
}),
new ObjectIdPipe(),
);
app.use(
session({
name: config.session.name,
secret: config.session.secret,
proxy: config.security.trustProxy,
resave: true,
saveUninitialized: false,
2024-10-26 11:02:39 +00:00
store: getSessionStore(),
2024-09-10 09:50:11 +00:00
cookie: {
httpOnly: true,
secure: config.security.httpsEnabled,
path: '/',
maxAge: isProduction
? 1000 * 60 * 60 * 24 //prod 24h
: 1000 * 60 * 60, //dev 1h
},
}),
);
app.use(passport.initialize());
app.use(passport.session());
2024-11-15 16:39:12 +00:00
if (config.cache.type === 'redis') {
const redisIoAdapter = new RedisIoAdapter(app);
await redisIoAdapter.connectToRedis();
app.useWebSocketAdapter(redisIoAdapter);
}
2024-09-10 09:50:11 +00:00
process.on('uncaughtException', (error) => {
if (error.stack.toLowerCase().includes('smtp'))
app.get(LoggerService).error('SMTP error', error.stack);
else throw error;
});
2024-09-29 10:49:05 +00:00
if (!isProduction) {
await seedDatabase(app);
swagger(app);
}
2024-09-10 09:50:11 +00:00
await app.listen(3000);
}
bootstrap();