mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-23 11:37:02 -04:00

While the DI and database initialization is running, NestJSs default logger is normally used. Our custom logger was only being initialized after DI setup is complete. Errors encountered during DI setup were buffered and only printed after DI init was complete, or the app exited on error. This led to the app not printing anything for a minute in certain cases. This commit replaces the initial logger with our ConsoleLoggerService that logs everything. After DI init is complete, that logger is replaced with a normal instance of ConsoleLoggerService that uses the real config from DI. Fixes https://github.com/hedgedoc/hedgedoc/issues/4306 Signed-off-by: David Mehren <git@herrmehren.de>
51 lines
2 KiB
TypeScript
51 lines
2 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
|
|
import { setupApp } from './app-init';
|
|
import { AppModule } from './app.module';
|
|
import { AppConfig } from './config/app.config';
|
|
import { AuthConfig } from './config/auth.config';
|
|
import { Loglevel } from './config/loglevel.enum';
|
|
import { MediaConfig } from './config/media.config';
|
|
import { ConsoleLoggerService } from './logger/console-logger.service';
|
|
|
|
async function bootstrap(): Promise<void> {
|
|
// Initialize AppModule
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
|
// ConsoleLoggerService only uses the loglevel, so we can give it an incomplete AppConfig to log everything
|
|
// This Logger instance will be replaced by a proper one with config from DI below
|
|
logger: new ConsoleLoggerService({ loglevel: Loglevel.TRACE } as AppConfig),
|
|
});
|
|
|
|
// Set up our custom logger
|
|
const logger = await app.resolve(ConsoleLoggerService);
|
|
logger.log('Switching logger', 'AppBootstrap');
|
|
app.useLogger(logger);
|
|
|
|
// Initialize config and abort if we don't have a valid config
|
|
const configService = app.get(ConfigService);
|
|
const appConfig = configService.get<AppConfig>('appConfig');
|
|
const authConfig = configService.get<AuthConfig>('authConfig');
|
|
const mediaConfig = configService.get<MediaConfig>('mediaConfig');
|
|
|
|
if (!appConfig || !authConfig || !mediaConfig) {
|
|
logger.error('Could not initialize config, aborting.', 'AppBootstrap');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Call common setup function which handles the rest
|
|
// Setup code must be added there!
|
|
await setupApp(app, appConfig, authConfig, mediaConfig, logger);
|
|
|
|
// Start the server
|
|
await app.listen(appConfig.port);
|
|
logger.log(`Listening on port ${appConfig.port}`, 'AppBootstrap');
|
|
}
|
|
|
|
void bootstrap();
|