import { config } from '@creditkarma/dynamic-config' ; import { ZipkinTracingExpress } from '@creditkarma/zipkin-tracing-express' ; import { createHttpClient , IRequest } from '@creditkarma/thrift-client' ; import { ThriftClientZipkinFilter } from '@creditkarma/thrift-client-zipkin-filter' ; import * as express from 'express' ; import * as path from 'path' ; import { Calculator , Operation , Work } from './generated/calculator-service' ; async function init ( ) { const SERVER_CONFIG = await config ( ) . get ( 'calculator-service' ) ; const CLIENT_CONFIG = await config ( ) . get ( 'client' ) ; const app = express ( ) ; app . use ( ZipkinTracingExpress ( { localServiceName : 'calculator-client' , tracerConfig : { endpoint : 'http://localhost:9411/api/v1/spans' , httpInterval : 1000 , httpTimeout : 5000 , sampleRate : 1.0 , } , } ) ) ; const thriftClient = createHttpClient ( Calculator . Client , { hostName : SERVER_CONFIG . host , port : SERVER_CONFIG . port , register : [ ThriftClientZipkinFilter ( { localServiceName : 'calculator-client' , remoteServiceName : 'calculator-service' , tracerConfig : { endpoint : 'http://localhost:9411/api/v1/spans' , httpInterval : 1000 , httpTimeout : 5000 , sampleRate : 1.0 , } , } ) , ] , } ) ; function symbolToOperation ( sym ) { switch ( sym ) { case 'add' : return Operation . ADD ; case 'subtract' : return Operation . SUBTRACT ; case 'multiply' : return Operation . MULTIPLY ; case 'divide' : return Operation . DIVIDE ; default : throw new Error ( `Unrecognized operation: ${ sym }` ) ; } } app . get ( '/' , ( req , res ) : void => { res . sendFile ( path . join ( __dirname , './index.html' ) ) ; } ) ; app . get ( '/ping' , ( req , res ) : void => { thriftClient . ping ( { headers : req . headers } ) . then ( ( ) => { res . send ( 'success' ) ; } , ( err ) => { console . log ( 'err: ' , err ) ; res . status ( 500 ) . send ( err ) ; } ) ; } ) ; app . get ( '/calculate' , ( req , res ) : void => { const work = new Work ( { num1 : Number ( req . query . left ) , num2 : Number ( req . query . right ) , op : symbolToOperation ( req . query . op as string ) , } ) ; thriftClient . calculate ( 1 , work , { headers : req . headers } ) . then ( ( val ) => { res . send ( `result: ${ val }` ) ; } , ( err ) => { res . status ( 500 ) . send ( err ) ; } ) ; } ) ; app . listen ( CLIENT_CONFIG . port , ( ) => { console . log ( `Web server listening at http://${ CLIENT_CONFIG . host }:${ CLIENT_CONFIG . port }` ) ; } ) ; } init ( ) ;