99 lines
2.6 KiB
JavaScript
99 lines
2.6 KiB
JavaScript
const express = require('express');
|
|
const session = require('express-session');
|
|
|
|
require('dotenv').config();
|
|
const { env } = require("process");
|
|
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
const log4js = require('log4js');
|
|
log4js.configure('log4js-conf.json');
|
|
global.logger = log4js.getLogger();
|
|
|
|
const mysql = require('mysql2');
|
|
const { log } = require('console');
|
|
|
|
global.mysql_pool = mysql.createPool({
|
|
connectionLimit : process.env.MYSQL_POOL_LIMIT || 10,
|
|
host: process.env.MYSQL_HOST || 'localhost',
|
|
database: process.env.MYSQL_DATABASE || 'sisai_world_db',
|
|
user: process.env.MYSQL_USER || 'root',
|
|
password: process.env.MYSQL_PASSWD || '',
|
|
stringifyObjects: true,
|
|
});
|
|
|
|
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0; // Protected by Nginx.(for self-signed certificate. Comment out in product env.)
|
|
|
|
// Shutdown function to flush logs and clean up
|
|
const shutdown = () => {
|
|
logger.info('Shutting down sisai server...');
|
|
|
|
// Flush log4js logs
|
|
log4js.shutdown((error) => {
|
|
if (error) {
|
|
console.error('Error shutting down log4js:', error);
|
|
} else {
|
|
console.log('Log4js has been shut down.');
|
|
}
|
|
|
|
// Exit the process after logs have been flushed
|
|
process.exit();
|
|
});
|
|
};
|
|
|
|
// Handle various shutdown signals
|
|
['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGBREAK', 'exit'].forEach(signal => {
|
|
process.on(signal, () => shutdown());
|
|
});
|
|
|
|
global.app = express();
|
|
|
|
app.use(express.static('views'));
|
|
|
|
app.use(session({
|
|
secret: 'sisai_world_wEb_962287769',
|
|
resave: false,
|
|
saveUninitialized: true,
|
|
cookie: { secure: false, maxAge: 1000*60*60*24*300 } //300 days
|
|
}));
|
|
|
|
logger.info('Sisai World running on nodejs:' + process.version)
|
|
|
|
var server
|
|
if(process.env.SSLKEY && process.env.SSLCRT){
|
|
const options = {
|
|
key: fs.readFileSync(process.env.SSLKEY),
|
|
cert: fs.readFileSync(process.env.SSLCRT)
|
|
};
|
|
const https = require("https");
|
|
server = https.createServer(options, app)
|
|
}else{
|
|
const http = require("http");
|
|
server = http.createServer({}, app)
|
|
}
|
|
|
|
const port = process.env.PORT || 443;
|
|
server.listen(port, () => {
|
|
logger.info(`Started sisai world server: ${port}`);
|
|
});
|
|
|
|
//Map of session time
|
|
global.session_time = new Map();
|
|
|
|
const mainJS = './process.js';
|
|
|
|
function loadModule(modulePath) {
|
|
const resolvedPath = path.resolve(modulePath);
|
|
delete require.cache[require.resolve(resolvedPath)];
|
|
return require(modulePath);
|
|
}
|
|
|
|
global.processModule = loadModule(mainJS);
|
|
|
|
// Watch for changes in the main module
|
|
fs.watchFile(path.resolve(__dirname, mainJS), () => {
|
|
processModule = loadModule(mainJS);
|
|
logger.info('mainJS reloaded');
|
|
});
|