import * as mongoose from 'mongoose' ; import { config } from '../config/config' ; import { Influx } from './_core/Influx/Influx' ; import { Mongo } from './_core/Mongo/Mongo' ; import { logger } from './logger' ; import { API } from './api/API' ; import { restartWatchers } from './api/modules/Watchers/helpers' ; import { Server } from 'hapi' ; returns { Promise < Influx > } async function init ( shouldRestartWatchers ) { try { await Mongo . connect ( config . mongo ) ; const influxClient = new Influx ( config . influx ) ; await influxClient . init ( ) ; if ( shouldRestartWatchers ) { await restartWatchers ( influxClient ) ; } return { influxClient } ; } catch ( error ) { logger . error ( error ) ; throw new Error ( 'Initialization failed' ) ; } } returns { Promise < void > } export async function startServer ( shouldRestartWatchers = true ) { try { const { influxClient } = await init ( shouldRestartWatchers ) ; const server = await API . create ( { host : config . api . host , port : config . api . port , influx : influxClient , } ) ; return server ; } catch ( error ) { logger . error ( error ) ; throw error ; } } export async function stopServer ( ) { try { logger . info ( 'Shutting down server...' ) ; await Mongo . close ( ) ; await API . stop ( ) ; } catch ( error ) { logger . error ( error ) ; throw new Error ( 'Error while stopping..' ) ; } }