utsushiiro 7 лет назад
Родитель
Сommit
55a13e90b3
2 измененных файлов с 30 добавлено и 10 удалено
  1. 2 5
      src/server/crowi/index.js
  2. 28 5
      src/server/service/config-manager.js

+ 2 - 5
src/server/crowi/index.js

@@ -209,12 +209,9 @@ Crowi.prototype.setupAppConfig = function() {
 };
 };
 
 
 Crowi.prototype.setupConfigManager = async function() {
 Crowi.prototype.setupConfigManager = async function() {
-  const ConfigLoader = require('../service/config-loader');
-  const configLoader = new ConfigLoader(this.model('Config'));
-  const config = await configLoader.load();
-
   const ConfigManager = require('../service/config-manager');
   const ConfigManager = require('../service/config-manager');
-  this.configManager = new ConfigManager(config);
+  this.configManager = new ConfigManager(this.model('Config'));
+  return await this.configManager.loadConfigs();
 };
 };
 
 
 Crowi.prototype.setupModels = function() {
 Crowi.prototype.setupModels = function() {

+ 28 - 5
src/server/service/config-manager.js

@@ -1,7 +1,16 @@
+const ConfigLoader = require('../service/config-loader')
+  , debug = require('debug')('growi:models:config');
+
 class ConfigManager {
 class ConfigManager {
 
 
-  constructor(configObject) {
-    this.configObject = configObject;
+  constructor(configModel) {
+    this.configModel = configModel;
+    this.configLoader = new ConfigLoader(this.configModel);
+    this.configObject = null;
+  }
+
+  async loadConfigs() {
+    this.configObject = await this.configLoader.load();
   }
   }
 
 
   getConfig(namespace, key) {
   getConfig(namespace, key) {
@@ -9,13 +18,27 @@ class ConfigManager {
   }
   }
 
 
   defaultSearch(namespace, key) {
   defaultSearch(namespace, key) {
-    if (this.configObject['fromDB'][namespace][key] !== undefined) {
-      return this.configObject['fromDB'][namespace][key];
+    if (this.configObject.fromDB[namespace][key] !== undefined) {
+      return this.configObject.fromDB[namespace][key];
     }
     }
     else {
     else {
-      return this.configObject['fromEnvVars'][namespace][key];
+      return this.configObject.fromEnvVars[namespace][key];
     }
     }
   }
   }
+
+  async updateConfigs(configs) {
+    for (const config of configs) {
+      this.configModel.findOneAndUpdate(
+        { ns: config.ns, key: config.key },
+        { ns: config.ns, key: config.key, value: JSON.stringify(config.value) },
+        { upsert: true, },
+        function(err, config) {
+          debug('Config.findAndUpdate', err, config);
+        });
+    }
+
+    await this.loadConfigs();
+  }
 }
 }
 
 
 module.exports = ConfigManager;
 module.exports = ConfigManager;