icynet-auth-server/src/main.ts

78 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-03-09 18:37:04 +00:00
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as dotenv from 'dotenv';
import * as session from 'express-session';
2022-03-20 14:50:12 +00:00
import * as connectRedis from 'connect-redis';
import * as redis from 'redis';
2022-08-17 18:56:47 +00:00
import * as cookieParser from 'cookie-parser';
2022-03-09 18:37:04 +00:00
import { join } from 'path';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
2022-03-09 18:37:04 +00:00
import { NestExpressApplication } from '@nestjs/platform-express';
import { AdminApiModule } from './modules/api/admin/admin.module';
import { OAuth2RouterModule } from './modules/static-front-end/oauth2-router/oauth2-router.module';
import { ConfigurationService } from './modules/config/config.service';
2022-03-09 18:37:04 +00:00
dotenv.config();
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
const config = app.get(ConfigurationService);
2022-03-20 14:50:12 +00:00
const docBuilder = new DocumentBuilder()
.setTitle('Icy Network Authentication Server')
.setDescription('Central authentication and management server')
.setVersion('1.0')
.addTag('admin')
.addTag('oauth2')
.build();
const document = SwaggerModule.createDocument(app, docBuilder, {
include: [AdminApiModule, OAuth2RouterModule],
});
SwaggerModule.setup('api', app, document);
2022-03-20 14:50:12 +00:00
const RedisStore = connectRedis(session);
const redisClient = redis.createClient({
2022-08-27 08:59:26 +00:00
url: process.env.REDIS_URL || 'redis://localhost:6379',
legacyMode: true,
2022-03-20 14:50:12 +00:00
});
2022-08-27 08:59:26 +00:00
redisClient.connect();
2022-03-20 14:50:12 +00:00
// app.use(express.urlencoded());
2022-08-17 18:56:47 +00:00
app.use(cookieParser());
2022-03-20 14:50:12 +00:00
2022-03-26 07:22:14 +00:00
// Production servers have to be behind a proxy.
if (process.env.NODE_ENV === 'production') {
2022-09-11 08:58:15 +00:00
app.set('trust proxy', 1);
app.disable('x-powered-by');
2022-03-26 07:22:14 +00:00
}
2022-03-09 18:37:04 +00:00
app.use(
2022-09-01 17:19:08 +00:00
/\/((?!api).)*/,
2022-03-09 18:37:04 +00:00
session({
name: config.get<string>('app.session_name'),
secret: config.get<string>('app.session_secret'),
2022-03-09 18:37:04 +00:00
resave: true,
saveUninitialized: false,
2022-03-20 14:50:12 +00:00
store: new RedisStore({ client: redisClient }),
2022-03-09 18:37:04 +00:00
cookie: {
2022-08-26 14:14:40 +00:00
sameSite: 'strict',
2022-03-09 18:37:04 +00:00
secure: process.env.NODE_ENV === 'production',
},
}),
);
app.useStaticAssets(join(__dirname, '..', 'public'), {
prefix: '/public/',
});
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('pug');
2022-08-22 17:51:37 +00:00
await app.listen(
parseInt(process.env.NEST_PORT, 10) ||
config.get<number>('app.port') ||
3000,
process.env.NEST_HOST || config.get<string>('app.host') || '0.0.0.0',
2022-08-22 17:51:37 +00:00
);
2022-03-09 18:37:04 +00:00
}
bootstrap();