icynet-auth-server/src/main.ts

76 lines
2.3 KiB
TypeScript
Raw Permalink 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';
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';
2022-09-20 14:49:22 +00:00
import { OAuth2RouterModule } from './modules/ssr-front-end/oauth2-router/oauth2-router.module';
import { ConfigurationService } from './modules/config/config.service';
2022-09-17 07:13:52 +00:00
import { ApiModule } from './modules/api/api.module';
2022-03-09 18:37:04 +00:00
dotenv.config();
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
2022-09-15 16:21:05 +00:00
app.enableCors({ origin: false });
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')
2022-09-17 07:13:52 +00:00
.addTag('api')
.addTag('admin')
.addTag('oauth2')
2022-09-17 07:13:52 +00:00
.addBearerAuth()
.addOAuth2({
type: 'oauth2',
flows: {
authorizationCode: {
authorizationUrl: '/oauth2/authorize',
tokenUrl: '/oauth2/token',
scopes: {
email: 'Access email address',
image: 'Access profile picture',
privileges: 'List of user privileges',
management: 'Administrative access',
openid: 'Get ID token',
},
},
},
})
.build();
2022-09-17 07:13:52 +00:00
const document = SwaggerModule.createDocument(app, docBuilder, {
2022-09-17 07:13:52 +00:00
include: [ApiModule, AdminApiModule, OAuth2RouterModule],
});
2022-09-17 07:13:52 +00:00
SwaggerModule.setup('api/openapi', app, document);
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.useStaticAssets(join(__dirname, '..', 'public'), {
prefix: '/public/',
2023-10-10 17:14:53 +00:00
maxAge: 8 * 60 * 60 * 1000,
2022-03-09 18:37:04 +00:00
});
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();