import * as express from 'express' ; import * as url from 'url' ; import { wrapPromise } from '../../utils/express-promise' ; import R5Router from './r5/r5' ; const gzipStatic = require ( 'connect-gzip-static' ) ; const program = require ( 'commander' ) ; program . version ( '1.0.0' ) . description ( 'Serve geographical data from a PostGIS database.' ) . option ( '-p, --port <port>' , 'Port on which to serve traffic (default: 1337)' , Number , 1337 ) . option ( '--router-url <url>' , 'URL for the router server.' , String , 'http://router-totx' ) . option ( '--timeout <timeout>' , 'The maximum amount of time we will wait for a request to finish, in seconds' , Number , 1200 ) . parse ( process . argv ) ; const isProd = process . env . NODE_ENV === 'production' ; console . log ( 'Starting up...' ) ; console . log ( isProd ? 'Production mode' : 'Dev mode' ) ; console . log ( 'Using routing server at' , program . routerUrl ) ; const r5Router = new R5Router ( program . routerUrl ) ; const app = express ( ) ; app . use ( require ( 'morgan' ) ( 'dev' ) ) ; app . get ( '/healthy' , ( expressRequest , response ) => { response . send ( 'OK' ) ; } ) ; app . get ( '/route' , wrapPromise ( async ( expressRequest , response ) => { const params = parseRequestURL ( expressRequest . url ) ; const route = await r5Router . getRoute ( params . origin , params . destination , params . options ) ; if ( isProd ) { response . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ; } response . send ( route ) ; } ) ) ; app . get ( '/one-to-city' , wrapPromise ( async ( expressRequest , response ) => { const params = parseRequestURL ( expressRequest . url ) ; const travelTimes = await r5Router . getTravelTimes ( params . origin , params . options ) ; if ( isProd ) { response . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ; } response . send ( travelTimes ) ; } ) ) ; param requestURL function parseRequestURL ( requestURL ) { let parsedQuery = url . parse ( requestURL ) . query ; if ( parsedQuery . slice ( - 1 ) === '=' ) { parsedQuery = parsedQuery . slice ( 0 , - 1 ) ; } return JSON . parse ( decodeURIComponent ( parsedQuery ) ) ; } const maxAge = process . env . NODE_ENV === 'production' ? '1d' : null ; app . use ( gzipStatic ( __dirname + '/../static' , { maxAge , } ) ) ; const server = app . listen ( program . port ) ; server . timeout = program . timeout * 1000 ; console . log ( `Listening on port ${ program . port }` ) ;