Spaces:
Running
Running
| /** | |
| * 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; | |