import * as express from 'express' ; import { createThriftServer } from '@creditkarma/thrift-server-express' ; import { Int64 } from '@creditkarma/thrift-server-core' ; import { createHttpClient , IRequest } from '@creditkarma/thrift-client' ; import { ThriftClientZipkinFilter } from '@creditkarma/thrift-client-zipkin-filter' ; import { ThriftClientTimingFilter } from '@creditkarma/thrift-client-timing-filter' ; import { IMappedStruct , ISharedStruct , ISharedUnion } from './generated/shared' ; import { Calculator , IChoice , ICommonStruct , Operation , Work , } from './generated/calculator-service' ; import { AddService } from './generated/add-service' ; import { ADD_SERVER_CONFIG , EXPRESS_CALC_SERVER_CONFIG } from './config' ; import { ICommonUnion } from './generated/common' ; export function createServer ( sampleRate = 0 ) { const addServiceClient = createHttpClient ( AddService . Client , { hostName : ADD_SERVER_CONFIG . hostName , port : ADD_SERVER_CONFIG . port , register : sampleRate > 0 ? [ ThriftClientZipkinFilter ( { localServiceName : 'calculator-service' , remoteServiceName : 'add-service' , tracerConfig : { endpoint : process . env . ZIPKIN_ENDPOINT , zipkinVersion : process . env . ZIPKIN_VERSION === 'v2' ? 'v2' : 'v1' , httpInterval : 0 , } , } ) , ThriftClientTimingFilter ( { remoteServiceName : 'add-service' , } ) , ] : [ ThriftClientTimingFilter ( { remoteServiceName : 'add-service' , } ) , ] , } ) ; const serviceHandlers = { ping ( ) { return ; } , add ( a , b , context ) { return addServiceClient . add ( a , b , { headers : context . headers } ) ; } , addInt64 ( a , b , context ) { return addServiceClient . addInt64 ( a , b , context ) ; } , addWithContext ( a , b , context ) { if ( context !== undefined && context . headers [ 'x-fake-token' ] === 'fake-token' ) { return a + b ; } else { throw new Error ( 'Unauthorized' ) ; } } , calculate ( logId , work , context ) { switch ( work . op ) { case Operation . ADD : return addServiceClient . 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 ; } } , zip ( ) { return ; } , getStruct ( ) { return { code : { status : new Int64 ( 0 ) , } , value : 'test' , } ; } , getUnion ( index ) { if ( index === 1 ) { return { option1 : 'foo' } ; } else { return { option2 : 'bar' } ; } } , getMappedStruct ( index ) { const map = new Map ( ) ; map . set ( 'one' , { code : { status : 5 , } , value : 'test' , } ) ; return { data : map , } ; } , echoBinary ( word ) { return word . toString ( 'utf-8' ) ; } , echoString ( word ) { return word ; } , checkName ( choice ) { if ( choice . firstName !== undefined ) { return `FirstName: ${ choice . firstName . name }` ; } else if ( choice . lastName !== undefined ) { return `LastName: ${ choice . lastName . name }` ; } else { throw new Error ( `Unknown choice` ) ; } } , checkOptional ( type ? ) { if ( type === undefined ) { return 'undefined' ; } else { return type ; } } , mapOneList ( list ) { return list . map ( ( next ) => next + 1 ) ; } , mapValues ( map ) { return Array . from ( map . values ( ) ) ; } , listToMap ( list ) { return list . reduce ( ( acc , next ) => { acc . set ( next [ 0 ] , next [ 1 ] ) ; return acc ; } , new Map ( ) ) ; } , fetchThing ( ) { return { code : { status : new Int64 ( 0 ) , } , value : 'test' , } ; } , fetchUnion ( ) { return { option1 : 'test' } ; } , broken ( ) { throw new Error ( `Yeah, this didn't work` ) ; } , } ; const app = createThriftServer ( { path : EXPRESS_CALC_SERVER_CONFIG . path , thriftOptions : { serviceName : 'calculator-service' , handler : new Calculator . Processor ( serviceHandlers ) , } , } ) ; app . get ( '/control' , ( req , res ) => { res . send ( 'PASS' ) ; } ) ; return app ; }