import { Server } from 'http' ; import { checkMetricsConfiguration } from '@payid-org/server-metrics' ; import * as express from 'express' ; import config from './config' ; import syncDatabaseSchema from './db/syncDatabaseSchema' ; import sendSuccess from './middlewares/sendSuccess' ; import { metricsRouter , adminApiRouter , publicApiRouter } from './routes' ; import metrics from './services/metrics' ; import logger from './utils/logger' ; /**
 * The PayID application. Runs two Express servers on different ports.
 *
 * One server responds to PayID Protocol requests (the public API),
 * while the other server exposes CRUD commands for PayIDs stored in the database (the Admin API).
 */ export default class App { public readonly publicApiExpress : express . Application ; public readonly adminApiExpress : express . Application ; private publicApiServer ? : Server ; private adminApiServer ? : Server ; public constructor ( ) { this . publicApiExpress = express ( ) ; this . adminApiExpress = express ( ) ; } param initConfig public async init ( initConfig = config ) { await syncDatabaseSchema ( initConfig . database ) ; this . publicApiServer = this . launchPublicApi ( initConfig . app ) ; this . adminApiServer = this . launchAdminApi ( initConfig . app ) ; checkMetricsConfiguration ( initConfig . metrics ) ; if ( initConfig . metrics . pushMetrics ) { logger . info ( `Pushing metrics is enabled.

      Metrics only capture the total number of PayIDs grouped by (paymentNetwork, environment),
      and the (paymentNetwork, environment) tuple of public requests to the PayID server.
      No identifying information is captured.

      If you would like to opt out of pushing metrics, set the environment variable PUSH_PAYID_METRICS to "false".
    ` ) ; } } /**
     * Shuts down the PayID server, and cleans up the recurring metric operations.
     */ public close ( ) { this . publicApiServer ?. close ( ) ; this . adminApiServer ?. close ( ) ; metrics . stopMetrics ( ) ; } param appConfig returns private launchPublicApi ( appConfig ) { this . publicApiExpress . use ( '/' , publicApiRouter ) ; return this . publicApiExpress . listen ( appConfig . publicApiPort , ( ) => logger . info ( `Public API listening on ${ appConfig . publicApiPort }` ) ) ; } param appConfig returns private launchAdminApi ( appConfig ) { this . adminApiExpress . use ( '/users' , adminApiRouter ) ; this . adminApiExpress . use ( '/metrics' , metricsRouter ) ; this . adminApiExpress . use ( '/status/health' , sendSuccess ) ; return this . adminApiExpress . listen ( appConfig . adminApiPort , ( ) => logger . info ( `Admin API listening on ${ appConfig . adminApiPort }` ) ) ; } }