Рефакторинг конфига
This commit is contained in:
@@ -1,37 +0,0 @@
|
|||||||
const fs = require('fs-extra');
|
|
||||||
const _ = require('lodash');
|
|
||||||
|
|
||||||
const propsToSave = [
|
|
||||||
'maxUploadFileSize',
|
|
||||||
'maxTempPublicDirSize',
|
|
||||||
'maxUploadPublicDirSize',
|
|
||||||
'useExternalBookConverter',
|
|
||||||
|
|
||||||
'servers',
|
|
||||||
];
|
|
||||||
|
|
||||||
async function load(config, configFilename) {
|
|
||||||
if (!configFilename) {
|
|
||||||
configFilename = `${config.dataDir}/config.json`;
|
|
||||||
|
|
||||||
if (!await fs.pathExists(configFilename)) {
|
|
||||||
save(config);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await fs.readFile(configFilename, 'utf8');
|
|
||||||
Object.assign(config, JSON.parse(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function save(config) {
|
|
||||||
const configFilename = `${config.dataDir}/config.json`;
|
|
||||||
const dataToSave = _.pick(config, propsToSave);
|
|
||||||
|
|
||||||
await fs.writeFile(configFilename, JSON.stringify(dataToSave, null, 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
load,
|
|
||||||
save
|
|
||||||
};
|
|
||||||
@@ -1,23 +1,91 @@
|
|||||||
|
const _ = require('lodash');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const utils = require('../core/utils');
|
|
||||||
|
|
||||||
const branchFilename = __dirname + '/application_env';
|
const branchFilename = __dirname + '/application_env';
|
||||||
|
|
||||||
let branch = 'production';
|
const propsToSave = [
|
||||||
try {
|
'maxUploadFileSize',
|
||||||
fs.accessSync(branchFilename);
|
'maxTempPublicDirSize',
|
||||||
branch = fs.readFileSync(branchFilename, 'utf8').trim();
|
'maxUploadPublicDirSize',
|
||||||
} catch (err) {
|
'useExternalBookConverter',
|
||||||
|
|
||||||
|
'servers',
|
||||||
|
];
|
||||||
|
|
||||||
|
let instance = null;
|
||||||
|
|
||||||
|
//singleton
|
||||||
|
class ConfigManager {
|
||||||
|
constructor() {
|
||||||
|
if (!instance) {
|
||||||
|
this.inited = false;
|
||||||
|
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
if (this.inited)
|
||||||
|
throw new Error('already inited');
|
||||||
|
|
||||||
|
this.branch = 'production';
|
||||||
|
try {
|
||||||
|
await fs.access(branchFilename);
|
||||||
|
this.branch = (await fs.readFile(branchFilename, 'utf8')).trim();
|
||||||
|
} catch (err) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
process.env.NODE_ENV = this.branch;
|
||||||
|
|
||||||
|
this.branchConfigFile = __dirname + `/${this.branch}.js`;
|
||||||
|
await fs.access(this.branchConfigFile);
|
||||||
|
this._config = require(this.branchConfigFile);
|
||||||
|
|
||||||
|
this._userConfigFile = `${this._config.dataDir}/config.json`;
|
||||||
|
|
||||||
|
this.inited = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
get config() {
|
||||||
|
if (!this.inited)
|
||||||
|
throw new Error('not inited');
|
||||||
|
return _.cloneDeep(this._config);
|
||||||
|
}
|
||||||
|
|
||||||
|
set config(value) {
|
||||||
|
Object.assign(this._config, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
get userConfigFile() {
|
||||||
|
return this._userConfigFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
set userConfigFile(value) {
|
||||||
|
if (value)
|
||||||
|
this._userConfigFile = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
async load() {
|
||||||
|
if (!this.inited)
|
||||||
|
throw new Error('not inited');
|
||||||
|
if (!await fs.pathExists(this.userConfigFile)) {
|
||||||
|
await this.save();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await fs.readFile(this.userConfigFile, 'utf8');
|
||||||
|
this.config = JSON.parse(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
async save() {
|
||||||
|
if (!this.inited)
|
||||||
|
throw new Error('not inited');
|
||||||
|
const dataToSave = _.pick(this._config, propsToSave);
|
||||||
|
await fs.writeFile(this.userConfigFile, JSON.stringify(dataToSave, null, 4));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.NODE_ENV = branch;
|
module.exports = ConfigManager;
|
||||||
|
|
||||||
const confFilename = __dirname + `/${branch}.js`;
|
|
||||||
|
|
||||||
fs.accessSync(confFilename);
|
|
||||||
|
|
||||||
const config = require(confFilename);
|
|
||||||
|
|
||||||
//fs.ensureDirSync(config.dataDir);
|
|
||||||
|
|
||||||
module.exports = config;
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const Logger = require('./Logger');
|
const Logger = require('./Logger');
|
||||||
|
const configManager = new (require('../config'))();//singleton
|
||||||
|
|
||||||
let instance = null;
|
let instance = null;
|
||||||
|
|
||||||
@@ -14,14 +15,15 @@ class AppLogger {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
init(config) {
|
async init() {
|
||||||
if (this.inited)
|
if (this.inited)
|
||||||
throw new Error('already inited');
|
throw new Error('already inited');
|
||||||
|
|
||||||
|
let config = configManager.config;
|
||||||
let loggerParams = null;
|
let loggerParams = null;
|
||||||
|
|
||||||
if (config.loggingEnabled) {
|
if (config.loggingEnabled) {
|
||||||
fs.ensureDirSync(config.logDir);
|
await fs.ensureDir(config.logDir);
|
||||||
loggerParams = [
|
loggerParams = [
|
||||||
{log: 'ConsoleLog'},
|
{log: 'ConsoleLog'},
|
||||||
{log: 'FileLog', fileName: `${config.logDir}/${config.name}.log`},
|
{log: 'FileLog', fileName: `${config.logDir}/${config.name}.log`},
|
||||||
@@ -35,9 +37,9 @@ class AppLogger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get logger() {
|
get logger() {
|
||||||
if (this.inited)
|
if (!this.inited)
|
||||||
return this._logger;
|
throw new Error('not inited');
|
||||||
throw new Error('not inited');
|
return this._logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
get log() {
|
get log() {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
|
|
||||||
const SqliteConnectionPool = require('./SqliteConnectionPool');
|
const SqliteConnectionPool = require('./SqliteConnectionPool');
|
||||||
|
const configManager = new (require('../config'))();//singleton
|
||||||
const log = new (require('../core/AppLogger'))().log;//singleton
|
const log = new (require('../core/AppLogger'))().log;//singleton
|
||||||
|
|
||||||
const migrations = {
|
const migrations = {
|
||||||
@@ -23,7 +24,7 @@ class ConnManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async init(config) {
|
async init(config) {
|
||||||
this.config = config;
|
this.config = configManager.config;
|
||||||
this._pool = {};
|
this._pool = {};
|
||||||
|
|
||||||
const force = null;//(config.branch == 'development' ? 'last' : null);
|
const force = null;//(config.branch == 'development' ? 'last' : null);
|
||||||
|
|||||||
@@ -1,19 +1,26 @@
|
|||||||
const config = require('./config');
|
|
||||||
const appLogger = new (require('./core/AppLogger'))();//singleton
|
|
||||||
appLogger.init(config);
|
|
||||||
const log = appLogger.log;
|
|
||||||
|
|
||||||
const configSaver = require('./config/configSaver');
|
|
||||||
const argv = require('minimist')(process.argv.slice(2));
|
|
||||||
|
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const argv = require('minimist')(process.argv.slice(2));
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const compression = require('compression');
|
const compression = require('compression');
|
||||||
|
|
||||||
const connManager = new(require('./db/ConnManager'))();//singleton
|
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
|
//config
|
||||||
|
const configManager = new (require('./config'))();//singleton
|
||||||
|
await configManager.init();
|
||||||
|
configManager.userConfigFile = argv.config;
|
||||||
|
await configManager.load();
|
||||||
|
const config = configManager.config;
|
||||||
|
|
||||||
|
//logger
|
||||||
|
const appLogger = new (require('./core/AppLogger'))();//singleton
|
||||||
|
await appLogger.init();
|
||||||
|
const log = appLogger.log;
|
||||||
|
|
||||||
|
//dirs
|
||||||
|
log(`${config.name} v${config.version}`);
|
||||||
|
log('Initializing');
|
||||||
|
|
||||||
await fs.ensureDir(config.dataDir);
|
await fs.ensureDir(config.dataDir);
|
||||||
await fs.ensureDir(config.uploadDir);
|
await fs.ensureDir(config.uploadDir);
|
||||||
await fs.ensureDir(config.sharedDir);
|
await fs.ensureDir(config.sharedDir);
|
||||||
@@ -28,16 +35,14 @@ async function init() {
|
|||||||
await fs.move(appNewDir, appDir);
|
await fs.move(appNewDir, appDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
//загружаем конфиг из файла
|
//connections
|
||||||
await configSaver.load(config, argv.config);
|
const connManager = new (require('./db/ConnManager'))();//singleton
|
||||||
|
await connManager.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
log(`${config.name} v${config.version}`);
|
const log = new (require('./core/AppLogger'))().log;//singleton
|
||||||
log('Initializing');
|
const config = new (require('./config'))().config;//singleton
|
||||||
await init();
|
|
||||||
|
|
||||||
await connManager.init(config);
|
|
||||||
|
|
||||||
//servers
|
//servers
|
||||||
for (let server of config.servers) {
|
for (let server of config.servers) {
|
||||||
@@ -88,6 +93,7 @@ async function main() {
|
|||||||
|
|
||||||
(async() => {
|
(async() => {
|
||||||
try {
|
try {
|
||||||
|
await init();
|
||||||
await main();
|
await main();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user