2022-06-19 15:23:56 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import { HttpAdapterHost } from '@nestjs/core';
|
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
2022-04-02 17:45:46 -04:00
|
|
|
import { WsAdapter } from '@nestjs/platform-ws';
|
2022-06-19 15:23:56 -04:00
|
|
|
|
|
|
|
import { AppConfig } from './config/app.config';
|
|
|
|
import { AuthConfig } from './config/auth.config';
|
|
|
|
import { MediaConfig } from './config/media.config';
|
|
|
|
import { ErrorExceptionMapping } from './errors/error-mapping';
|
|
|
|
import { ConsoleLoggerService } from './logger/console-logger.service';
|
|
|
|
import { BackendType } from './media/backends/backend-type.enum';
|
2023-06-25 15:52:44 -04:00
|
|
|
import { SessionService } from './sessions/session.service';
|
2022-06-19 15:23:56 -04:00
|
|
|
import { setupSpecialGroups } from './utils/createSpecialGroups';
|
|
|
|
import { setupSessionMiddleware } from './utils/session';
|
|
|
|
import { setupValidationPipe } from './utils/setup-pipes';
|
|
|
|
import { setupPrivateApiDocs, setupPublicApiDocs } from './utils/swagger';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common setup function which is called by main.ts and the E2E tests.
|
|
|
|
*/
|
|
|
|
export async function setupApp(
|
|
|
|
app: NestExpressApplication,
|
|
|
|
appConfig: AppConfig,
|
|
|
|
authConfig: AuthConfig,
|
|
|
|
mediaConfig: MediaConfig,
|
|
|
|
logger: ConsoleLoggerService,
|
|
|
|
): Promise<void> {
|
2023-01-08 18:27:29 -05:00
|
|
|
await setupPublicApiDocs(app);
|
2022-06-19 15:23:56 -04:00
|
|
|
logger.log(
|
2023-01-08 18:27:29 -05:00
|
|
|
`Serving OpenAPI docs for public API under '/apidoc/v2'`,
|
2022-06-19 15:23:56 -04:00
|
|
|
'AppBootstrap',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
2023-01-08 18:27:29 -05:00
|
|
|
await setupPrivateApiDocs(app);
|
2022-06-19 15:23:56 -04:00
|
|
|
logger.log(
|
2023-01-08 18:27:29 -05:00
|
|
|
`Serving OpenAPI docs for private API under '/apidoc/private'`,
|
2022-06-19 15:23:56 -04:00
|
|
|
'AppBootstrap',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await setupSpecialGroups(app);
|
|
|
|
|
2022-06-21 10:16:40 -04:00
|
|
|
setupSessionMiddleware(
|
|
|
|
app,
|
|
|
|
authConfig,
|
|
|
|
app.get(SessionService).getTypeormStore(),
|
|
|
|
);
|
2022-06-19 15:23:56 -04:00
|
|
|
|
|
|
|
app.enableCors({
|
2022-10-02 16:07:32 -04:00
|
|
|
origin: appConfig.rendererBaseUrl,
|
2022-06-19 15:23:56 -04:00
|
|
|
});
|
2022-10-02 16:07:32 -04:00
|
|
|
logger.log(
|
|
|
|
`Enabling CORS for '${appConfig.rendererBaseUrl}'`,
|
|
|
|
'AppBootstrap',
|
|
|
|
);
|
2022-06-19 15:23:56 -04:00
|
|
|
|
|
|
|
app.useGlobalPipes(setupValidationPipe(logger));
|
|
|
|
|
|
|
|
if (mediaConfig.backend.use === BackendType.FILESYSTEM) {
|
|
|
|
logger.log(
|
|
|
|
`Serving the local folder '${mediaConfig.backend.filesystem.uploadPath}' under '/uploads'`,
|
|
|
|
'AppBootstrap',
|
|
|
|
);
|
|
|
|
app.useStaticAssets(mediaConfig.backend.filesystem.uploadPath, {
|
|
|
|
prefix: '/uploads/',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.log(
|
|
|
|
`Serving the local folder 'public' under '/public'`,
|
|
|
|
'AppBootstrap',
|
|
|
|
);
|
|
|
|
app.useStaticAssets('public', {
|
|
|
|
prefix: '/public/',
|
|
|
|
});
|
|
|
|
|
|
|
|
const { httpAdapter } = app.get(HttpAdapterHost);
|
|
|
|
app.useGlobalFilters(new ErrorExceptionMapping(httpAdapter));
|
2022-04-02 17:45:46 -04:00
|
|
|
app.useWebSocketAdapter(new WsAdapter(app));
|
2022-08-07 16:31:20 -04:00
|
|
|
app.enableShutdownHooks();
|
2022-06-19 15:23:56 -04:00
|
|
|
}
|