handow-shm
Version:
E2E test server with Handow engine
85 lines (71 loc) • 3.56 kB
JavaScript
/* eslint-disable node/shebang */
;
/**
* Launch the server
*/
const util = require('util');
const handow = require('handow');
const open = require('open');
const app = require("./src/server");
const shmSetting = require("./src/services/shmSetting");
const sleep = util.promisify(setTimeout);
const doSchedule = require('./src/services/doShcedule');
// const logger = require( "winston" );
const logger = require( "./src/logger" );
/*
The shm running mode passed by process arg variable will override the "process.env.SHM_MODE". This is the way passing shmMode in real deployment.
E.g. $ node launch.js --secure // This will set shm running in secure mode no matter what is the process.env.SHM_MODE setting
*/
const _shmMode = process.argv[2] ? process.argv[2].replace("--", "").trim().toLocaleLowerCase() : null;
const _shmPort = process.argv[3] ? process.argv[3].replace("--", "").trim().toLocaleLowerCase() : null;
// const _shmPort = process.argv[3] ? process.argv[3].replace("--", "").trim().toLocaleLowerCase() : null;
if ( _shmMode ) {
process.env.SHM_MODE = _shmMode;
}
if (_shmPort) {
process.env.SHM_PORT = _shmPort;
}
/*
const target = process.argv[3] ? process.argv[3].replace("--", "").trim() : null;
*/
// Will update currrent shmSeeting object in app context, and also copy de default shmSetting.json to app-root if it not existed there.
shmSetting.update(false); // dry change, not trigger schedule
/*
app.set( "httpsPort", process.env.SHM_PORT || setting.httpsPort || 3443 );
*/
/*
// CORS is deployed with Nginx in production, DEV use internal cors
process.env.NODE_ENV === "dev" && app.use( cors( { "maxAge": "86400" } ) );
*/
// SHM server run on port 3333 by default, or specify by passing SHM_PORT to process.env
app.set( "httpPort", process.env.SHM_PORT || 3333 );
const server = app.listen( app.get( "httpPort" ) )
.on( "listening", async () => {
if (`${process.env['OPEN_BROWSER']}` === 'true' ) {
await sleep(1000); // wait a wile?
// Will search the static paths root to match existed /index.html
open( `http://localhost:${app.get( "httpPort" )}/`, { wait: true } );
}
const modeStr = `${shmSetting.get()['scheduleMode']}` === 'loop' ?
'INFINITE-LOOPING' :
`${shmSetting.get()['scheduleMode']}` === 'timer' ?
'TIME-SCHEDULE' :
`${shmSetting.get()['scheduleMode']}` === 'trigger' ?
'REQUEST-TRIGGER' : 'MANUAL-RUN';
// The logger.info will not work because we have set the log-level to 'error'
// logger.info( `SHM server started at port [${app.get( "httpPort" )}], current schedule-mode is [${modeStr}]` );
// process.env.NODE_ENV === 'development' && console.info( `SHM server started at port [${app.get( "httpPort" )}], current schedule-mode is [${modeStr}].\n` );
logger.info(`SHM server started at port [${app.get( "httpPort" )}], current schedule-mode is [${modeStr}].\n`);
await sleep(2000);
doSchedule("start");
handow.eventBus.on( "SSE_HANDOW_STREAM", async (_data) => {
if (_data['status'] === 'idle') {
await sleep(2000);
doSchedule("planFinished");
}
} );
} )
.on( "error", ( err ) => {
logger.error( `SHM startup failed - ${err}` );
} );