|
| 1 | +import * as bodyParser from 'body-parser'; |
| 2 | +import * as cors from 'cors'; |
| 3 | +import * as express from 'express'; |
| 4 | +// tslint:disable-next-line:no-implicit-dependencies |
| 5 | +import * as core from 'express-serve-static-core'; |
| 6 | +import { createServer, Server } from 'http'; |
| 7 | + |
| 8 | +import { AppDependencies } from '../app'; |
| 9 | +import { HEALTHCHECK_PATH } from '../constants'; |
| 10 | +import { logger } from '../logger'; |
| 11 | +import { requestLogger } from '../middleware/request_logger'; |
| 12 | +import { createHealthcheckRouter } from '../routers/healthcheck_router'; |
| 13 | +import { HealthcheckService } from '../services/healthcheck_service'; |
| 14 | +import { HttpServiceConfig } from '../types'; |
| 15 | + |
| 16 | +/** |
| 17 | + * creates the NodeJS http server with graceful shutdowns, healthchecks, |
| 18 | + * configured header timeouts and other sane defaults set. |
| 19 | + */ |
| 20 | +export function createDefaultServer( |
| 21 | + dependencies: AppDependencies, |
| 22 | + config: HttpServiceConfig, |
| 23 | + app: core.Express, |
| 24 | +): Server { |
| 25 | + app.use(requestLogger()); |
| 26 | + app.use(cors()); |
| 27 | + app.use(bodyParser.json()); |
| 28 | + |
| 29 | + const server = createServer(app); |
| 30 | + server.keepAliveTimeout = config.httpKeepAliveTimeout; |
| 31 | + server.headersTimeout = config.httpHeadersTimeout; |
| 32 | + const healthcheckService = new HealthcheckService(); |
| 33 | + |
| 34 | + server.on('close', () => { |
| 35 | + logger.info('http server shutdown'); |
| 36 | + }); |
| 37 | + server.on('listening', () => { |
| 38 | + logger.info(`server listening on ${config.httpPort}`); |
| 39 | + healthcheckService.setHealth(true); |
| 40 | + }); |
| 41 | + |
| 42 | + const shutdownFunc = (sig: string) => { |
| 43 | + logger.info(`received: ${sig}, shutting down server`); |
| 44 | + healthcheckService.setHealth(false); |
| 45 | + server.close(async err => { |
| 46 | + if (dependencies.meshClient) { |
| 47 | + dependencies.meshClient.destroy(); |
| 48 | + } |
| 49 | + if (dependencies.connection) { |
| 50 | + await dependencies.connection.close(); |
| 51 | + } |
| 52 | + if (!server.listening) { |
| 53 | + process.exit(0); |
| 54 | + } |
| 55 | + if (err) { |
| 56 | + logger.error(`server closed with an error: ${err}, exiting`); |
| 57 | + process.exit(1); |
| 58 | + } |
| 59 | + logger.info('successful shutdown, exiting'); |
| 60 | + process.exit(0); |
| 61 | + }); |
| 62 | + }; |
| 63 | + if (config.httpPort === config.healthcheckHttpPort) { |
| 64 | + app.use(HEALTHCHECK_PATH, createHealthcheckRouter(healthcheckService)); |
| 65 | + } else { |
| 66 | + // if we don't want to expose the /healthz healthcheck service route to |
| 67 | + // the public, we serve it from a different port. Serving it through a |
| 68 | + // different express app also removes the unnecessary request logging. |
| 69 | + const healthcheckApp = express(); |
| 70 | + healthcheckApp.use(HEALTHCHECK_PATH, createHealthcheckRouter(healthcheckService)); |
| 71 | + healthcheckApp.listen(config.healthcheckHttpPort, () => { |
| 72 | + logger.info(`healthcheckApp listening on ${config.healthcheckHttpPort}`); |
| 73 | + }); |
| 74 | + } |
| 75 | + process.on('SIGINT', shutdownFunc); |
| 76 | + process.on('SIGTERM', shutdownFunc); |
| 77 | + process.on('SIGQUIT', shutdownFunc); |
| 78 | + return server; |
| 79 | +} |
0 commit comments