import express from 'express' ; import proxy from 'express-http-proxy' ; import util from 'util' ; import Optional from 'optional-js' ; import path from 'path' ; import { publicRoot } from './const/appPaths' ; import { SelfReportingMetricsRegistry , LoggingReporter , Dimensions } from 'measured-reporting' ; import { createProcessMetrics , createOSMetrics , createExpressMiddleware } from 'measured-node-metrics' ; import bodyParser from 'body-parser' ; const app = express ( ) ; app . use ( bodyParser . json ( ) ) ; app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; interface CustomizableConfiguration { metricsRegistry : SelfReportingMetricsRegistry ; } const customizations = require ( '../../../customizations' ) || { } ; const metricsRegistry = Optional . ofNullable ( customizations . metricsRegistry ) . orElse ( new SelfReportingMetricsRegistry ( new LoggingReporter ( { defaultDimensions : { } } ) ) ) ; createOSMetrics ( metricsRegistry ) ; createProcessMetrics ( metricsRegistry ) ; app . use ( createExpressMiddleware ( metricsRegistry ) ) ; const port = process . env . PORT || 3001 ; app . get ( '/' , ( req , res ) => res . redirect ( 301 , '/dashboard' ) ) ; interface MetricsProxyRequest { name : string ; dimensions ? : Dimensions ; } app . post ( '/metrics' , ( req , res ) => { const request = req . body as MetricsProxyRequest ; metricsRegistry . getOrCreateCounter ( request . name , request . dimensions ) . inc ( ) ; res . status ( 201 ) . end ( ) ; } ) ; app . use ( '/kayenta' , proxy ( Optional . ofNullable ( process . env . KAYENTA_BASE_URL ) . orElse ( 'http://localhost:8090' ) , { proxyReqOptDecorator : ( proxyReqOpts ) => { proxyReqOpts . rejectUnauthorized = ! ! process . env . DISABLE_KAYENTA_SSL_VERIFICATION ; return proxyReqOpts ; } , proxyReqPathResolver : req => { return process . env . KAYENTA_BASE_PATH ? `${ process . env . KAYENTA_BASE_PATH }${ req . url }` : req . url ; } } ) ) ; app . get ( '/health' , ( req , res ) => { res . status ( 204 ) . end ( ) ; } ) ; app . use ( '/dashboard/' , express . static ( publicRoot , { setHeaders : ( res , path ) => { if ( path . endsWith ( '.html' ) ) { res . setHeader ( 'Cache-Control' , 'no-cache' ) ; } } } ) ) ; app . get ( '/dashboard/*' , ( req , res ) => { res . setHeader ( 'Cache-Control' , 'no-cache' ) ; res . sendFile ( path . join ( publicRoot , '/index.html' ) , err => { if ( err ) { res . status ( 500 ) . send ( util . inspect ( err ) ) ; } } ) ; } ) ; app . listen ( port , ( ) => console . log ( `Referee listening on port ${ port }!` ) ) ;