import fs from 'fs' ; import { promisify } from 'util' ; import { ServerUnaryCall , sendUnaryData } from 'grpc' ; import { Framework , Controller , Context } from '../class' ; import util from '../util' ; export default async ( app ) => { const possibleDist = /\.ts$/ . test ( __filename ) ? '' : '/dist' ; const controllerPath = `${ app . appInfo . rootPath }${ possibleDist }/controller` ; const files = await promisify ( fs . readdir ) ( controllerPath ) ; for ( const file of files ) { const module = await import ( `${ controllerPath }/${ file }` ) ; const controller = new module . default ( app ) ; const impl = { } ; for ( const member of Object . getOwnPropertyNames ( Object . getPrototypeOf ( controller ) ) ) { if ( member === 'constructor' || typeof controller [ member ] !== 'function' ) continue ; impl [ member ] = async ( call , callback ) => { try { await app . midware . execute ( new Context ( controller . app , controller . service , member , call . metadata . getMap ( ) ) , call . request , async ( ctx , req ) => { const data = await controller [ member ] ( ctx , req ) ; callback ( undefined , data ) ; ctx . response = data ; } ) ; } catch ( error ) { console . log ( error ) ; } } ; } const pack = util . loadPackage ( controller . package ) ; app . server . addService ( pack [ controller . service ] . service , impl ) ; } } ;