Spaces:
Sleeping
Sleeping
File size: 7,464 Bytes
e4bf523 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 'use strict'
/**
* Copyright (c) 2017~2019, OBCon Inc.
* All rights reserved.
*/
/**
* @file
* @copyright 2017~2019, OBCon Inc.
* @author gye hyun james kim [pnuskgh@gmail.com]
*/
const fs = require('fs');
//--- Service, Site, Module별 설정 관리
//--- 동적 설정 : files/settings/${service}/setting.json, files/settings/${service}/${site}/setting.json
//--- 정적 설정 : custom/${service}/modules/${module}/setting.js, modules/${module}/setting.js
//--- To-Do: site_${siteKey} 형식 대신 site_${site_ID}를 사용한다. siteKey는 변경될 수 있기 때문에 문제가 발생할 수 있다.
//--- global.settingSync
class Settings {
constructor() {
this._filename = 'setting.json'; //--- 설정 파일명
this._common = '_common_'; //--- 모듈의 공통 설정의 저장 이름
this.init();
}
init() {
let paths = [
utils.getPath('files'),
utils.getPath('files', 'settings'),
utils.getPath('files', 'settings', config.service.name)
];
paths.forEach(pathTemp => {
if (fs.existsSync(pathTemp) == false) {
fs.mkdirSync(pathTemp);
}
});
}
// _sampleSettingJson() {
// return {
// _common_: { //--- 공통 설정
// },
// _module_: { //--- 모듈(_module_)의 설정
// }
// };
// }
//--- 설정을 읽어서 반환 한다.
//--- 동적 설정에 없으면 정적 설정을 읽고 그래도 없으면 defaultValue를 반환 한다.
//--- site : site_${siteKey} 형식을 사용 한다.
//--- To-Do: 정적 설정 + service 동적 설정 + site 동적 설정 + default 순서로 더한 후 최종 결과를 반환 한다.
_getSetting(service, site=null, moduleName=null, name=null, defaultValue=null) {
let fileSetting = null;
let mySettings = {};
let tmpStr = null;
try {
//--- 1. 동적 설정을 읽어서 반환 한다.
//--- 1-1. site의 동적 설정을 읽어서 반환 한다.
if (site != null) {
fileSetting = utils.getPath('files', 'settings', service, site, this._filename);
if (fs.existsSync(fileSetting)) {
let buf = fs.readFileSync(fileSetting, { encoding: 'utf8' });
mySettings = JSON.parse(buf);
tmpStr = (moduleName == null) ? this._common:moduleName;
if (utils.existField(mySettings, tmpStr)) {
if (utils.existField(mySettings[tmpStr], name)) {
return mySettings[tmpStr][name];
}
}
}
}
//--- 1-2. service의 동적 설정을 읽어서 반환 한다.
fileSetting = utils.getPath('files', 'settings', service, this._filename);
if (fs.existsSync(fileSetting)) {
let buf = fs.readFileSync(fileSetting, { encoding: 'utf8' });
mySettings = JSON.parse(buf);
tmpStr = (moduleName == null) ? this._common:moduleName;
if (utils.existField(mySettings, tmpStr)) {
if (utils.existField(mySettings[tmpStr], name)) {
return mySettings[tmpStr][name];
}
}
}
//--- 2. 정적 설정을 읽어서 반환 한다.
//--- 정적 설정은 반드시 모듈별 정적 설정 이다.
if (moduleName != null) {
return modules[moduleName].setting._getSetting(name, defaultValue);
}
//--- 3. defaultValue를 반환 한다.
return defaultValue;
} catch (e) {
try {
logger.error('include/settings.js : ' + e.name + ': ' + e.message);
logger.error(e.stack);
} catch (e1) { }
return defaultValue;
}
}
//--- 동적 설정을 저장 한다.
_setSetting(service, site=null, moduleName=null, name=null, value=null) {
let fileSetting = null;
let mySettings = {};
let tmpStr = null;
if (site == null) {
fileSetting = utils.getPath('files', 'settings', service);
if (fs.existsSync(fileSetting) == false) {
fs.mkdirSync(fileSetting);
}
fileSetting = utils.getPath('files', 'settings', service, this._filename);
if (fs.existsSync(fileSetting)) {
let buf = fs.readFileSync(fileSetting, { encoding: 'utf8' });
mySettings = JSON.parse(buf);
} else {
mySettings = {};
}
tmpStr = (moduleName == null) ? this._common:moduleName;
if (utils.existField(mySettings, tmpStr) == false) {
mySettings[tmpStr] = {};
}
mySettings[tmpStr][name] = value;
fs.writeFileSync(fileSetting, JSON.stringify(mySettings), { encoding: 'utf8' });
return;
}
fileSetting = utils.getPath('files', 'settings', service);
if (fs.existsSync(fileSetting) == false) {
fs.mkdirSync(fileSetting);
}
fileSetting = utils.getPath('files', 'settings', service, site);
if (fs.existsSync(fileSetting) == false) {
fs.mkdirSync(fileSetting);
}
fileSetting = utils.getPath('files', 'settings', service, site, this._filename);
if (fs.existsSync(fileSetting)) {
let buf = fs.readFileSync(fileSetting, { encoding: 'utf8' });
mySettings = JSON.parse(buf);
} else {
mySettings = {};
}
tmpStr = (moduleName == null) ? this._common:moduleName;
if (utils.existField(mySettings, tmpStr) == false) {
mySettings[tmpStr] = {};
}
mySettings[tmpStr][name] = value;
fs.writeFileSync(fileSetting, JSON.stringify(mySettings), { encoding: 'utf8' });
return;
}
_getSiteName(site) {
let tmpSiteName = site;
if ((tmpSiteName != null) && (tmpSiteName.startsWith('site_') == false)) {
tmpSiteName = 'site_' + tmpSiteName;
}
return tmpSiteName;
}
//--- site : site_${siteKey} 형식을 사용 한다.
getSetting(site=null, moduleName=null, name=null, defaultValue=null) {
return this._getSetting(config.service.name, this._getSiteName(site), moduleName, name, defaultValue);
}
getSetting2(site=null, moduleName=null, name1=null, name2=null, defaultValue=null) {
const mySettings = this.getSetting(site, moduleName, name1, null);
if ((mySettings != null) && (utils.existField(mySettings, name2))) {
return mySettings[name2];
}
return defaultValue;
}
setSetting(site=null, moduleName=null, name=null, value=null) {
this._setSetting(config.service.name, this._getSiteName(site), moduleName, name, value);
}
}
module.exports = Settings;
|