// dtp-webapp.js
// Copryright (C) 2022 DTP Technologies, LLC
// Licence: Apache-2.0
'use strict' ;
const DTP_COMPONENT = {
logId : 'dtp-core-app' ,
index : 'dtpCoreApp' ,
className : 'DtpCoreApp' ,
} ;
require ( 'dotenv' ) . config ( ) ;
const path = require ( 'path' ) ;
const { SitePlatform , SiteLog } = require ( path . join ( __ dirname , 'lib' , 'site-lib' ) ) ;
module . rootPath = __ dirname ;
module . pkg = require ( path . join ( module . rootPath , 'package.json' ) ) ;
module . config = {
environment : process . env . NODE_ENV ,
root : module . rootPath ,
component : DTP_COMPONENT ,
site : require ( path . join ( module . rootPath , 'config' , 'site' ) ) ,
http : require ( path . join ( module . rootPath , 'config' , 'http' ) ) ,
https : require ( path . join ( module . rootPath , 'config' , 'https' ) ) ,
registerMiddleware : async ( dtp , app ) => {
const { feed : feedService } = dtp . services ;
app . use ( feedService . middleware ( { maxEntryCount : 5 } ) ) ;
} ,
} ;
module . log = new SiteLog ( module , module . config . component ) ;
module . shutdown = async ( ) => {
return await SitePlatform . shutdown ( ) ;
} ;
( async ( ) => {
process . on ( 'unhandledRejection' , ( error , p ) => {
module . log . error ( 'Unhandled rejection' , {
error : error ,
promise : p ,
stack : error . stack
} ) ;
} ) ;
process . on ( 'warning' , ( error ) => {
module . log . alert ( 'warning' , { error } ) ;
} ) ;
process . once ( 'SIGINT' , async ( ) => {
module . log . info ( 'SIGINT received' ) ;
module . log . info ( 'requesting shutdown...' ) ;
const exitCode = await module . shutdown ( ) ;
process . nextTick ( ( ) => {
process . exit ( exitCode ) ;
} ) ;
} ) ;
process . once ( 'SIGUSR2' , async ( ) => {
await SitePlatform . shutdown ( ) ;
process . kill ( process . pid , 'SIGUSR2' ) ;
} ) ;
try {
await SitePlatform . start ( module ) ;
await SitePlatform . startWebServer ( module ) ;
} catch ( error ) {
module . log . error ( ` failed to start DTP ${ module . config . component . name } ` , { error } ) ;
process . exit ( - 1 ) ;
}
} ) ( ) ;