import { ZipkinTracingExpress } from '@creditkarma/zipkin-tracing-express' ; import { createHttpClient , IRequest } from '@creditkarma/thrift-client' ; import { ThriftClientZipkinFilter } from '@creditkarma/thrift-client-zipkin-filter' ; import { ThriftClientTimingFilter } from '@creditkarma/thrift-client-timing-filter' ; import * as express from 'express' ; import * as net from 'net' ; import { Calculator , IWorkArgs , Operation , Work , } from '../generated/calculator-service' ; import { ProtocolType } from '@creditkarma/thrift-server-core' ; import { CLIENT_CONFIG , HAPI_CALC_SERVER_CONFIG } from '../config' ; export function createClientServer ( sampleRate = 0 , protocolType = 'binary' ) { const app = express ( ) ; if ( sampleRate > 0 ) { app . use ( [ ZipkinTracingExpress ( { localServiceName : 'calculator-client' , tracerConfig : { endpoint : process . env . ZIPKIN_ENDPOINT , zipkinVersion : process . env . ZIPKIN_VERSION === 'v2' ? 'v2' : 'v1' , sampleRate , httpInterval : 0 , } , } ) , ] ) ; } const thriftClient = createHttpClient ( Calculator . Client , { hostName : HAPI_CALC_SERVER_CONFIG . hostName , port : HAPI_CALC_SERVER_CONFIG . port , protocol : protocolType , register : sampleRate > 0 ? [ ThriftClientZipkinFilter ( { localServiceName : 'calculator-client' , remoteServiceName : 'calculator-service' , tracerConfig : { endpoint : process . env . ZIPKIN_ENDPOINT , zipkinVersion : process . env . ZIPKIN_VERSION === 'v2' ? 'v2' : 'v1' , sampleRate , httpInterval : 0 , } , } ) , ThriftClientTimingFilter ( { remoteServiceName : 'calculator-service' , } ) , ] : [ ThriftClientTimingFilter ( { remoteServiceName : 'calculator-service' , } ) , ] , } ) ; 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 ( '/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 = { 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 . get ( '/calculate-overwrite' , ( 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 : { 'x-b3-traceid' : '411d1802c9151ded' , 'x-b3-spanid' : 'c3ba1a6560ca0c48' , 'x-b3-parentspanid' : '2b5189ffa013ad73' , 'x-b3-sampled' : '1' , } , } ) . then ( ( val ) => { res . send ( `result: ${ val }` ) ; } , ( err ) => { res . status ( 500 ) . send ( err ) ; } ) ; } ) ; return new Promise ( ( resolve , reject ) => { const server = app . listen ( CLIENT_CONFIG . port , ( ) => { console . log ( `Web server listening on port[${ CLIENT_CONFIG . port }]` ) ; resolve ( server ) ; } ) ; } ) ; }