import { config } from '@creditkarma/dynamic-config' ; import { createThriftServer } from '@creditkarma/thrift-server-hapi' ; import { ZipkinTracingHapi } from '@creditkarma/zipkin-tracing-hapi' ; import { createHttpClient , IRequest } from '@creditkarma/thrift-client' ; import { ThriftClientZipkinFilter } from '@creditkarma/thrift-client-zipkin-filter' ; import * as Hapi from '@hapi/hapi' ; import { AddService } from './generated/add-service' ; import { Calculator , IWork , Operation } from './generated/calculator-service' ; async function init ( ) { const SERVER_CONFIG = await config ( ) . get ( 'calculator-service' ) ; const ADD_SERVER_CONFIG = await config ( ) . get ( 'add-service' ) ; const thriftClient = createHttpClient ( AddService . Client , { hostName : ADD_SERVER_CONFIG . host , port : ADD_SERVER_CONFIG . port , register : [ ThriftClientZipkinFilter ( { localServiceName : 'calculator-service' , remoteServiceName : 'add-service' , tracerConfig : { endpoint : 'http://localhost:9411/api/v1/spans' , httpInterval : 1000 , httpTimeout : 5000 , sampleRate : 1.0 , } , } ) , ] , } ) ; const impl = new Calculator . Processor ( { ping ( ) { return ; } , add ( a , b ) { return thriftClient . add ( a , b ) ; } , calculate ( logId , work , context ) { switch ( work . op ) { case Operation . ADD : return thriftClient . add ( work . num1 , work . num2 , { headers : context . headers , } ) ; case Operation . SUBTRACT : return work . num1 - work . num2 ; case Operation . DIVIDE : return work . num1 / work . num2 ; case Operation . MULTIPLY : return work . num1 * work . num2 ; default : throw new Error ( `Invalid operation[${ work . op }]` ) ; } } , } ) ; const server = await createThriftServer ( { port : SERVER_CONFIG . port , path : SERVER_CONFIG . path , thriftOptions : { serviceName : 'calculator-service' , handler : impl , } , } ) ; await server . register ( { plugin : ZipkinTracingHapi ( { localServiceName : 'calculator-service' , tracerConfig : { endpoint : 'http://localhost:9411/api/v1/spans' , httpInterval : 1000 , httpTimeout : 5000 , sampleRate : 1.0 , } , } ) , } ) ; await server . start ( ) ; } init ( ) ;