Начальная структура директорий, каркас проекта

This commit is contained in:
Book Pauk
2022-08-15 18:38:31 +07:00
parent 5739fef2b7
commit c3a0ce183e
19 changed files with 16271 additions and 0 deletions

View File

@@ -0,0 +1 @@
development

28
server/config/base.js Normal file
View File

@@ -0,0 +1,28 @@
const path = require('path');
const pckg = require('../../package.json');
const execDir = path.resolve(__dirname, '..');
const dataDir = `${execDir}/.${pckg.name}/data`;
module.exports = {
branch: 'unknown',
version: pckg.version,
name: pckg.name,
dataDir,
tempDir: `${dataDir}/tmp`,
logDir: `${dataDir}/log`,
publicDir: `${dataDir}/public`,
loggingEnabled: true,
maxFilesDirSize: 1024*1024*1024,//1Gb
webConfigParams: ['name', 'version', 'branch'],
server: {
ip: '0.0.0.0',
port: '22380',
},
};

View File

@@ -0,0 +1,5 @@
const base = require('./base');
module.exports = Object.assign({}, base, {
branch: 'development',
});

89
server/config/index.js Normal file
View File

@@ -0,0 +1,89 @@
const _ = require('lodash');
const fs = require('fs-extra');
const branchFilename = __dirname + '/application_env';
const propsToSave = [
'loggingEnabled',
'maxFilesDirSize',
'server',
];
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`;
this._config = require(this.branchConfigFile);
await fs.ensureDir(this._config.dataDir);
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));
}
}
module.exports = ConfigManager;

View File

@@ -0,0 +1,20 @@
const path = require('path');
const pckg = require('../../package.json');
const base = require('./base');
const execDir = path.dirname(process.execPath);
const dataDir = `${execDir}/.${pckg.name}/data`;
module.exports = Object.assign({}, base, {
branch: 'production',
dataDir,
tempDir: `${dataDir}/tmp`,
logDir: `${dataDir}/log`,
publicDir: `${dataDir}/public`,
server: {
ip: '0.0.0.0',
port: '12380',
},
});