import * as express from 'express' ; import { IRepository } from '../repository' ; export const monitorService = ( repo ) => { if ( ! repo ) { console . log ( '[error]: no repository available' ) ; return express . Router ( ) ; } return express . Router ( ) . get ( '/config' , async ( req , res , next ) => { await repo . config ( ) . then ( ( ) => res . status ( 200 ) . json ( { code : 200 , message : 'success' } ) ) . catch ( err => next ( err ) ) ; } ) . get ( '/serviceListAll' , async ( req , res , next ) => { await repo . findAll ( ) . then ( result => ( console . log ( 'Registered api route: %j' , result ) , result ) ) . then ( result => res . status ( 200 ) . json ( { code : 200 , message : result } ) ) . catch ( err => next ( err ) ) ; } ) . post ( '/serviceUp' , async ( req , res , next ) => { const { service = null } = req . body || null ; if ( ! service ) return next ( { code : 400 , message : 'Unexisting routes' } ) ; const { label = null , host = null , port = null } = service ; if ( ! label || ! host || ! port ) return next ( '' ) ; const route = { label , host , port } ; await repo . add ( route ) . then ( result => res . status ( 200 ) . json ( { code : 200 , message : result } ) ) . catch ( err => next ( err ) ) ; } ) . post ( '/serviceDown' , async ( req , res , next ) => { const { container : { Actor : ID = null } = { } } = req . body ; if ( ! ID ) return next ( 'unexisting ID' ) ; await repo . remove ( ID ) . then ( result => console . log ( 'Removed api route: %j' , result ) ) . then ( result => res . status ( 200 ) . json ( { code : 200 , message : result } ) ) . catch ( err => next ( err ) ) ; } ) ; } ;