2021-01-05 16:12:38 -05:00
|
|
|
/*
|
2021-01-06 15:36:07 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 16:12:38 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2021-04-02 13:08:30 -04:00
|
|
|
import { LogLevel, ValidationPipe } from '@nestjs/common';
|
2020-10-30 17:35:57 -04:00
|
|
|
import { ConfigService } from '@nestjs/config';
|
2020-07-25 14:26:10 -04:00
|
|
|
import { NestFactory } from '@nestjs/core';
|
2020-10-24 05:32:23 -04:00
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
2021-08-29 12:45:46 -04:00
|
|
|
|
2020-07-25 14:26:10 -04:00
|
|
|
import { AppModule } from './app.module';
|
2020-10-30 17:35:57 -04:00
|
|
|
import { AppConfig } from './config/app.config';
|
2021-08-31 07:36:13 -04:00
|
|
|
import { AuthConfig } from './config/auth.config';
|
2021-01-15 10:57:04 -05:00
|
|
|
import { MediaConfig } from './config/media.config';
|
2021-04-02 13:08:30 -04:00
|
|
|
import { ConsoleLoggerService } from './logger/console-logger.service';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { BackendType } from './media/backends/backend-type.enum';
|
2021-08-31 07:36:13 -04:00
|
|
|
import { setupSessionMiddleware } from './utils/session';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { setupPrivateApiDocs, setupPublicApiDocs } from './utils/swagger';
|
2020-07-21 15:24:56 -04:00
|
|
|
|
2021-02-27 11:41:32 -05:00
|
|
|
async function bootstrap(): Promise<void> {
|
2021-04-02 13:08:30 -04:00
|
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
|
|
|
logger: ['error', 'warn', 'log'] as LogLevel[],
|
2021-07-15 14:21:42 -04:00
|
|
|
bufferLogs: true,
|
2021-04-02 13:08:30 -04:00
|
|
|
});
|
|
|
|
const logger = await app.resolve(ConsoleLoggerService);
|
2020-09-27 15:48:42 -04:00
|
|
|
logger.log('Switching logger', 'AppBootstrap');
|
|
|
|
app.useLogger(logger);
|
2020-10-30 17:35:57 -04:00
|
|
|
const configService = app.get(ConfigService);
|
|
|
|
const appConfig = configService.get<AppConfig>('appConfig');
|
2021-08-31 07:36:13 -04:00
|
|
|
const authConfig = configService.get<AuthConfig>('authConfig');
|
2021-01-15 10:57:04 -05:00
|
|
|
const mediaConfig = configService.get<MediaConfig>('mediaConfig');
|
2020-07-26 10:53:43 -04:00
|
|
|
|
2021-08-31 07:36:13 -04:00
|
|
|
if (!appConfig || !authConfig || !mediaConfig) {
|
2021-04-29 10:22:01 -04:00
|
|
|
logger.error('Could not initialize config, aborting.', 'AppBootstrap');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2021-01-25 17:11:56 -05:00
|
|
|
setupPublicApiDocs(app);
|
2021-04-02 11:40:44 -04:00
|
|
|
logger.log(
|
|
|
|
`Serving OpenAPI docs for public api under '/apidoc'`,
|
|
|
|
'AppBootstrap',
|
|
|
|
);
|
2021-01-25 17:11:56 -05:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
setupPrivateApiDocs(app);
|
2021-04-02 11:40:44 -04:00
|
|
|
logger.log(
|
|
|
|
`Serving OpenAPI docs for private api under '/private/apidoc'`,
|
|
|
|
'AppBootstrap',
|
|
|
|
);
|
2021-01-25 17:11:56 -05:00
|
|
|
}
|
2020-07-26 10:53:43 -04:00
|
|
|
|
2021-08-31 07:36:13 -04:00
|
|
|
setupSessionMiddleware(app, authConfig);
|
|
|
|
|
2021-04-02 11:36:51 -04:00
|
|
|
app.enableCors({
|
|
|
|
origin: appConfig.rendererOrigin,
|
|
|
|
});
|
|
|
|
logger.log(`Enabling CORS for '${appConfig.rendererOrigin}'`, 'AppBootstrap');
|
|
|
|
|
2020-07-25 14:26:10 -04:00
|
|
|
app.useGlobalPipes(
|
|
|
|
new ValidationPipe({
|
|
|
|
forbidUnknownValues: true,
|
|
|
|
skipMissingProperties: false,
|
|
|
|
transform: true,
|
|
|
|
}),
|
|
|
|
);
|
2021-02-15 01:39:52 -05:00
|
|
|
if (mediaConfig.backend.use === BackendType.FILESYSTEM) {
|
|
|
|
logger.log(
|
2021-04-02 11:38:22 -04:00
|
|
|
`Serving the local folder '${mediaConfig.backend.filesystem.uploadPath}' under '/uploads'`,
|
2021-02-15 01:39:52 -05:00
|
|
|
'AppBootstrap',
|
|
|
|
);
|
|
|
|
app.useStaticAssets(mediaConfig.backend.filesystem.uploadPath, {
|
|
|
|
prefix: '/uploads/',
|
2020-10-30 17:35:57 -04:00
|
|
|
});
|
|
|
|
}
|
2021-04-25 08:41:38 -04:00
|
|
|
logger.log(
|
|
|
|
`Serving the local folder 'public' under '/public'`,
|
|
|
|
'AppBootstrap',
|
|
|
|
);
|
|
|
|
app.useStaticAssets('public', {
|
|
|
|
prefix: '/public/',
|
|
|
|
});
|
2020-10-30 17:35:57 -04:00
|
|
|
await app.listen(appConfig.port);
|
|
|
|
logger.log(`Listening on port ${appConfig.port}`, 'AppBootstrap');
|
2020-07-21 15:24:56 -04:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:08:08 -05:00
|
|
|
void bootstrap();
|