动态端口,统一config

This commit is contained in:
gaoshuaixing
2020-11-13 12:09:55 +08:00
parent 84c7dad0f2
commit 9cae92d069
6 changed files with 161 additions and 63 deletions

View File

@@ -2,6 +2,7 @@
const path = require('path');
const dayjs = require('dayjs');
const storage = require('./storage');
const config = {
log: {
@@ -34,8 +35,31 @@ const config = {
}
}
exports.get = function () {
return config;
exports.get = function (flag = '') {
console.log('config flag:', flag);
if (flag === 'log') {
return config.log;
}
if (flag === 'windowsOption') {
return config.windowsOption;
}
if (flag === 'web-egg') {
return config.egg;
}
if (flag === 'egg') {
const eggConfig = storage.getEggConfig();
console.log('eggConfig:', eggConfig);
if (eggConfig.port) {
console.log('eggConfig.port:', eggConfig.port);
config.egg.port = eggConfig.port;
}
return config.egg;
}
return {};
};
exports = module.exports;

View File

@@ -44,11 +44,7 @@ exports.start = function (argv) {
const ignoreKeys = [ '_', '$0', 'env', 'daemon', 'stdout', 'stderr', 'timeout', 'ignore-stderr', 'node' ];
const clusterOptions = stringify(argv, ignoreKeys);
const options = JSON.parse(clusterOptions);
// console.log('options:', {
// argv,
// options
// });
console.log('[lanucher] options:', options)
return new Promise((resolve, reject) => {
startCluster(options, function(){
resolve('success');

View File

@@ -1,14 +1,16 @@
'use strict';
global.ELog = require('electron-log')
const config = require('./config')
global.ELog = require('electron-log');
const storage = require('./storage');
const config = require('./config');
module.exports = () => {
storage.setup();
logger();
}
function logger () {
let logConfig = config.get().log;
let logConfig = config.get('log');
for (let transport in logConfig) {
const configInfo = logConfig[transport];
if (transport === 'file') {

64
electron/storage.js Normal file
View File

@@ -0,0 +1,64 @@
'use strict';
const path = require('path');
const lowdb = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const fs = require('fs');
const getPort = require('get-port');
const utils = require('../app/utils/utils');
const config = require('./config');
const storageDir = path.normalize('./storage/');
exports.setup = function () {
if (!fs.existsSync(storageDir)) {
utils.mkdir(storageDir);
utils.chmodPath(storageDir, '777');
}
const file = storageDir + 'db.json';
const adapter = new FileSync(file);
const db = lowdb(adapter);
if (!db.has('egg_config').value()) {
db.set('egg_config', {}).write();
}
return true;
};
exports.instance = function (file = null) {
if (!file) {
file = path.normalize('./storage/db.json');
}
const isExist = fs.existsSync(file);
if (!isExist) {
return null;
}
const adapter = new FileSync(file);
const db = lowdb(adapter);
return db;
};
exports.getEggConfig = function () {
const res = this.instance()
.get('egg_config')
.value();
return res;
};
exports.setDynamicPort = async function () {
const eggConfig = config.get('egg');
console.log('setDynamicPort eggConfig:', eggConfig);
// const dynamicPort = await getPort({port: eggConfig.port})
const dynamicPort = await getPort();
const res = this.instance()
.set('egg_config.port', dynamicPort)
.write();
return res;
};
exports = module.exports;