import { CONFIG } from './config' ; import { AuthRepo } from './auth.repository' ; import { AuthRouter } from './auth.router' ; import { AuthServer } from './auth.server' ; import { authenticationMiddleware } from './auth.authorization.middleware' ; export class AuthModule { server : AuthServer ; router : AuthRouter ; repository : AuthRepo ; db : any ; serverConfig : CONFIG . IServerConfig ; serviceName ? : string = 'AuthModule' ; constructor ( datas ) { Object . assign ( this , datas ) ; } async init ( ) { await this . _initServer ( ) . catch ( err => console . log ( err ) ) ; await this . _initRepo ( ) . catch ( err => console . log ( err ) ) ; await this . _initRouter ( ) . catch ( err => console . log ( err ) ) ; await this . server . addDefaultRoute ( ) . catch ( err => console . log ( err ) ) ; await this . server . createHttpServer ( ) . catch ( err => console . log ( err ) ) ; await this . server . bootstrap ( ) . catch ( err => console . log ( err ) ) ; await this . server . connectApiGateway ( ) . catch ( err => console . log ( err ) ) ; } private async _initServer ( ) { if ( ! this . serverConfig ) return Promise . reject ( { code : 500 , message : 'Server need serverConfig' } ) ; return this . server = new AuthServer ( { serverConfig : this . serverConfig , serviceName : this . serviceName } ) ; } private async _initRepo ( ) { if ( ! this . db || ! this . serverConfig ) return Promise . reject ( { code : 500 , message : 'Repository need DB' } ) ; return this . repository = await new AuthRepo ( ) . init ( this . db , this . serverConfig ) ; } private async _initRouter ( ) { if ( ! this . repository ) return Promise . reject ( { code : 500 , message : 'Router need repository instance.' } ) ; this . router = new AuthRouter ( { repo : this . repository , serverConfig : this . serverConfig , middlewares : [ authenticationMiddleware . authenticatedRoute ] } ) ; const routes = await this . router . getRoutes ( ) . catch ( err => err ) ; if ( routes ) this . server . app . use ( '/api/v2/' , routes ) ; return this . router ; } }