|
@@ -1,23 +1,91 @@
|
|
|
+const _ = require('lodash');
|
|
|
const fs = require('fs-extra');
|
|
|
-const utils = require('../core/utils');
|
|
|
|
|
|
const branchFilename = __dirname + '/application_env';
|
|
|
|
|
|
-let branch = 'production';
|
|
|
-try {
|
|
|
- fs.accessSync(branchFilename);
|
|
|
- branch = fs.readFileSync(branchFilename, 'utf8').trim();
|
|
|
-} catch (err) {
|
|
|
-}
|
|
|
+const propsToSave = [
|
|
|
+ 'maxUploadFileSize',
|
|
|
+ 'maxTempPublicDirSize',
|
|
|
+ 'maxUploadPublicDirSize',
|
|
|
+ '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 = branch;
|
|
|
+ process.env.NODE_ENV = this.branch;
|
|
|
|
|
|
-const confFilename = __dirname + `/${branch}.js`;
|
|
|
+ this.branchConfigFile = __dirname + `/${this.branch}.js`;
|
|
|
+ await fs.access(this.branchConfigFile);
|
|
|
+ this._config = require(this.branchConfigFile);
|
|
|
|
|
|
-fs.accessSync(confFilename);
|
|
|
+ this._userConfigFile = `${this._config.dataDir}/config.json`;
|
|
|
|
|
|
-const config = require(confFilename);
|
|
|
+ this.inited = true;
|
|
|
+ }
|
|
|
|
|
|
-//fs.ensureDirSync(config.dataDir);
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-module.exports = config;
|
|
|
+module.exports = ConfigManager;
|