Рефакторинг
This commit is contained in:
40
server/core/getLogger.js
Normal file
40
server/core/getLogger.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const utils = require('./utils');
|
||||
const Logger = require('./Logger');
|
||||
|
||||
let logger = null;
|
||||
|
||||
function initLogger(config) {
|
||||
if (logger)
|
||||
logger.close();
|
||||
|
||||
let loggerParams = null;
|
||||
|
||||
if (config.loggingEnabled) {
|
||||
utils.mkDirIfNotExistsSync(config.logDir);
|
||||
loggerParams = [
|
||||
{log: 'ConsoleLog'},
|
||||
{log: 'FileLog', fileName: `${config.logDir}/${config.name}.log`},
|
||||
];
|
||||
}
|
||||
|
||||
logger = new Logger(loggerParams);
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
function getLogger() {
|
||||
if (logger)
|
||||
return logger;
|
||||
throw new Error('getLogger error: logger not initialized');
|
||||
}
|
||||
|
||||
function getLog() {
|
||||
const l = getLogger();
|
||||
return l.log.bind(l);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initLogger,
|
||||
getLogger,
|
||||
getLog,
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
const utils = require('./utils');
|
||||
const Logger = require('./Logger');
|
||||
|
||||
module.exports = function(config) {
|
||||
let loggerParams = null;
|
||||
|
||||
if (config.loggingEnabled) {
|
||||
utils.mkDirIfNotExistsSync(config.logDir);
|
||||
loggerParams = [
|
||||
{log: 'ConsoleLog'},
|
||||
{log: 'FileLog', fileName: `${config.logDir}/${config.name}.log`},
|
||||
];
|
||||
}
|
||||
|
||||
return new Logger(loggerParams);
|
||||
}
|
||||
@@ -1,24 +1,26 @@
|
||||
const config = require('./config/config');
|
||||
const {initLogger, getLog} = require('./core/getLogger');
|
||||
initLogger(config);
|
||||
const log = getLog();
|
||||
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
const config = require('./config/config');
|
||||
const logger = require('./core/loggerInit')(config);
|
||||
|
||||
const SqliteConnectionPool = require('./core/SqliteConnectionPool');
|
||||
|
||||
async function main() {
|
||||
const connPool = new SqliteConnectionPool(20, config);
|
||||
|
||||
logger.log('Opening database');
|
||||
log('Opening database');
|
||||
await connPool.init();
|
||||
|
||||
app.use(express.static('public'));
|
||||
app.use(express.json());
|
||||
|
||||
require('./routes')(app, connPool, logger, config);
|
||||
require('./routes').initRoutes(app, connPool, config);
|
||||
|
||||
app.listen(config.port, config.ip, function() {
|
||||
logger.log('Server is ready');
|
||||
log('Server is ready');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
module.exports = function(app, connPool, logger, config) {
|
||||
const log = require('./core/getLogger').getLog();
|
||||
|
||||
function initRoutes(app, connPool, config) {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
initRoutes
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user