|
@@ -1,5 +1,5 @@
|
|
|
import { Configuration, Inject, InjectorService } from '@tsed/di';
|
|
import { Configuration, Inject, InjectorService } from '@tsed/di';
|
|
|
-import { PlatformApplication } from '@tsed/common';
|
|
|
|
|
|
|
+import { HttpServer, PlatformApplication } from '@tsed/common';
|
|
|
import '@tsed/platform-express'; // !! DO NOT MODIFY !!
|
|
import '@tsed/platform-express'; // !! DO NOT MODIFY !!
|
|
|
import '@tsed/typeorm'; // !! DO NOT MODIFY !! -- https://github.com/tsedio/tsed/issues/1332#issuecomment-837840612
|
|
import '@tsed/typeorm'; // !! DO NOT MODIFY !! -- https://github.com/tsedio/tsed/issues/1332#issuecomment-837840612
|
|
|
import '@tsed/swagger';
|
|
import '@tsed/swagger';
|
|
@@ -9,9 +9,12 @@ import compress from 'compression';
|
|
|
import cookieParser from 'cookie-parser';
|
|
import cookieParser from 'cookie-parser';
|
|
|
import methodOverride from 'method-override';
|
|
import methodOverride from 'method-override';
|
|
|
import helmet from 'helmet';
|
|
import helmet from 'helmet';
|
|
|
|
|
+import { Express } from 'express';
|
|
|
import expressBunyanLogger from 'express-bunyan-logger';
|
|
import expressBunyanLogger from 'express-bunyan-logger';
|
|
|
|
|
+import gracefulExit from 'express-graceful-exit';
|
|
|
|
|
|
|
|
import { ConnectionOptions } from 'typeorm';
|
|
import { ConnectionOptions } from 'typeorm';
|
|
|
|
|
+import { createTerminus } from '@godaddy/terminus';
|
|
|
|
|
|
|
|
import swaggerSettingsForDev from '~/config/swagger/config.dev';
|
|
import swaggerSettingsForDev from '~/config/swagger/config.dev';
|
|
|
import swaggerSettingsForProd from '~/config/swagger/config.prod';
|
|
import swaggerSettingsForProd from '~/config/swagger/config.prod';
|
|
@@ -20,6 +23,9 @@ import loggerFactory from '~/utils/logger';
|
|
|
export const rootDir = __dirname;
|
|
export const rootDir = __dirname;
|
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
|
|
|
|
|
|
+const logger = loggerFactory('slackbot-proxy:server');
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
const connectionOptions: ConnectionOptions = {
|
|
const connectionOptions: ConnectionOptions = {
|
|
|
// The 'name' property must be set. Otherwise, the 'name' will be '0' and won't work well. -- 2021.04.05 Yuki Takei
|
|
// The 'name' property must be set. Otherwise, the 'name' will be '0' and won't work well. -- 2021.04.05 Yuki Takei
|
|
|
// see: https://github.com/TypedProject/tsed/blob/7630cda20a1f6fa3a692ecc3e6cd51d37bc3c45f/packages/typeorm/src/utils/createConnection.ts#L10
|
|
// see: https://github.com/TypedProject/tsed/blob/7630cda20a1f6fa3a692ecc3e6cd51d37bc3c45f/packages/typeorm/src/utils/createConnection.ts#L10
|
|
@@ -86,7 +92,7 @@ const helmetOptions = isProduction ? {} : {
|
|
|
export class Server {
|
|
export class Server {
|
|
|
|
|
|
|
|
@Inject()
|
|
@Inject()
|
|
|
- app: PlatformApplication;
|
|
|
|
|
|
|
+ app: PlatformApplication<Express>;
|
|
|
|
|
|
|
|
@Configuration()
|
|
@Configuration()
|
|
|
settings: Configuration;
|
|
settings: Configuration;
|
|
@@ -100,10 +106,19 @@ export class Server {
|
|
|
if (serverUri === undefined) {
|
|
if (serverUri === undefined) {
|
|
|
throw new Error('The environment variable \'SERVER_URI\' must be defined.');
|
|
throw new Error('The environment variable \'SERVER_URI\' must be defined.');
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ const server = this.injector.get<HttpServer>(HttpServer);
|
|
|
|
|
+
|
|
|
|
|
+ // init express-graceful-exit
|
|
|
|
|
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
+ gracefulExit.init(server!);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$beforeRoutesInit(): void {
|
|
$beforeRoutesInit(): void {
|
|
|
|
|
+ const expressApp = this.app.getApp();
|
|
|
|
|
+
|
|
|
this.app
|
|
this.app
|
|
|
|
|
+ .use(gracefulExit.middleware(expressApp))
|
|
|
.use(cookieParser())
|
|
.use(cookieParser())
|
|
|
.use(compress({}))
|
|
.use(compress({}))
|
|
|
.use(methodOverride())
|
|
.use(methodOverride())
|
|
@@ -115,6 +130,22 @@ export class Server {
|
|
|
this.setupLogger();
|
|
this.setupLogger();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ $beforeListen(): void {
|
|
|
|
|
+ const expressApp = this.app.getApp();
|
|
|
|
|
+ const server = this.injector.get<HttpServer>(HttpServer);
|
|
|
|
|
+
|
|
|
|
|
+ // init terminus
|
|
|
|
|
+ createTerminus(server, {
|
|
|
|
|
+ onSignal: async() => {
|
|
|
|
|
+ logger.info('server is starting cleanup');
|
|
|
|
|
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
+ gracefulExit.gracefulExitHandler(expressApp, server!);
|
|
|
|
|
+ },
|
|
|
|
|
+ onShutdown: async() => {
|
|
|
|
|
+ logger.info('cleanup finished, server is shutting down');
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Setup logger for requests
|
|
* Setup logger for requests
|