Yuki Takei 1 год назад
Родитель
Сommit
5f54db4d3c

+ 15 - 16
apps/app/src/server/service/config-manager/legacy/config-loader.ts

@@ -894,11 +894,13 @@ export default class ConfigLoader {
     const configFromDB: any = await this.loadFromDB();
     const configFromEnvVars: any = this.loadFromEnvVars();
 
-    // merge defaults per ns
+    // deperecate 'ns' and unified to 'crowi' -- 2024.12.10 Yuki Takei
     const mergedConfigFromDB = {
-      crowi: Object.assign(defaultCrowiConfigs, configFromDB.crowi),
-      markdown: Object.assign(defaultMarkdownConfigs, configFromDB.markdown),
-      notification: Object.assign(defaultNotificationConfigs, configFromDB.notification),
+      crowi: Object.assign(
+        Object.assign(defaultCrowiConfigs, configFromDB.crowi),
+        Object.assign(defaultMarkdownConfigs, configFromDB.markdown),
+        Object.assign(defaultNotificationConfigs, configFromDB.notification),
+      ),
     };
 
     // In getConfig API, only null is used as a value to indicate that a config is not set.
@@ -921,14 +923,13 @@ export default class ConfigLoader {
   }
 
   async loadFromDB(): Promise<any> {
-    const config = {};
+    const config = {
+      crowi: {},
+    };
     const docs = await Config.find().exec();
 
     for (const doc of docs) {
-      if (!config[doc.ns]) {
-        config[doc.ns] = {};
-      }
-      config[doc.ns][doc.key] = doc.value ? JSON.parse(doc.value) : null;
+      config.crowi[doc.key] = doc.value ? JSON.parse(doc.value) : null;
     }
 
     logger.debug('ConfigLoader#loadFromDB', config);
@@ -937,19 +938,17 @@ export default class ConfigLoader {
   }
 
   loadFromEnvVars(): any {
-    const config = {};
+    const config = {
+      crowi: {},
+    };
     for (const ENV_VAR_NAME of Object.keys(ENV_VAR_NAME_TO_CONFIG_INFO)) {
       const configInfo = ENV_VAR_NAME_TO_CONFIG_INFO[ENV_VAR_NAME];
-      if (config[configInfo.ns] === undefined) {
-        config[configInfo.ns] = {};
-      }
-
       if (process.env[ENV_VAR_NAME] === undefined) {
-        config[configInfo.ns][configInfo.key] = configInfo.default;
+        config.crowi[configInfo.key] = configInfo.default;
       }
       else {
         const parser = parserDictionary[configInfo.type];
-        config[configInfo.ns][configInfo.key] = parser.parse(process.env[ENV_VAR_NAME] as string);
+        config.crowi[configInfo.key] = parser.parse(process.env[ENV_VAR_NAME] as string);
       }
     }
 

+ 16 - 16
apps/app/src/server/service/config-manager/legacy/config-manager.ts

@@ -237,21 +237,21 @@ class ConfigManagerImpl implements ConfigManager, S2sMessageHandlable {
    * return whether the specified namespace/key should be retrieved only from env vars
    */
   private shouldSearchedFromEnvVarsOnly(namespace, key) {
-    return (namespace === 'crowi' && (
+    return (
       // siteUrl
       (
         KEYS_FOR_APP_SITE_URL_USES_ONLY_ENV_OPTION.includes(key)
-        && this.defaultSearch('crowi', 'env:useOnlyEnvVars:app:siteUrl')
+        && this.searchOnlyFromEnvVarConfigs('crowi', 'env:useOnlyEnvVars:app:siteUrl')
       )
       // local strategy
       || (
         KEYS_FOR_LOCAL_STRATEGY_USE_ONLY_ENV_OPTION.includes(key)
-        && this.defaultSearch('crowi', 'env:useOnlyEnvVars:security:passport-local')
+        && this.searchOnlyFromEnvVarConfigs('crowi', 'env:useOnlyEnvVars:security:passport-local')
       )
       // saml strategy
       || (
         KEYS_FOR_SAML_USE_ONLY_ENV_OPTION.includes(key)
-        && this.defaultSearch('crowi', 'env:useOnlyEnvVars:security:passport-saml')
+        && this.searchOnlyFromEnvVarConfigs('crowi', 'env:useOnlyEnvVars:security:passport-saml')
       )
       // file upload option
       || (
@@ -268,7 +268,7 @@ class ConfigManagerImpl implements ConfigManager, S2sMessageHandlable {
         KEYS_FOR_AZURE_USE_ONLY_ENV_OPTION.includes(key)
         && this.searchOnlyFromEnvVarConfigs('crowi', 'env:useOnlyEnvVars:azure')
       )
-    ));
+    );
   }
 
   /*
@@ -289,25 +289,25 @@ class ConfigManagerImpl implements ConfigManager, S2sMessageHandlable {
     // only exists in db
     if (this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
       logger.debug(`${namespace}.${key} only exists in db`);
-      return this.configObject.fromDB[namespace][key];
+      return this.configObject.fromDB.crowi[key];
     }
 
     // only exists env vars
     if (!this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
       logger.debug(`${namespace}.${key} only exists in env vars`);
-      return this.configObject.fromEnvVars[namespace][key];
+      return this.configObject.fromEnvVars.crowi[key];
     }
 
     // exists both in db and in env vars [db > env var]
     if (this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
-      if (this.configObject.fromDB[namespace][key] !== null) {
+      if (this.configObject.fromDB.crowi[key] !== null) {
         logger.debug(`${namespace}.${key} exists both in db and in env vars. loaded from db`);
-        return this.configObject.fromDB[namespace][key];
+        return this.configObject.fromDB.crowi[key];
       }
       /* eslint-disable-next-line no-else-return */
       else {
         logger.debug(`${namespace}.${key} exists both in db and in env vars. loaded from env vars`);
-        return this.configObject.fromEnvVars[namespace][key];
+        return this.configObject.fromEnvVars.crowi[key];
       }
     }
   }
@@ -320,7 +320,7 @@ class ConfigManagerImpl implements ConfigManager, S2sMessageHandlable {
       return undefined;
     }
 
-    return this.configObject.fromDB[namespace][key];
+    return this.configObject.fromDB.crowi[key];
   }
 
   /**
@@ -331,29 +331,29 @@ class ConfigManagerImpl implements ConfigManager, S2sMessageHandlable {
       return undefined;
     }
 
-    return this.configObject.fromEnvVars[namespace][key];
+    return this.configObject.fromEnvVars.crowi[key];
   }
 
   /**
    * check whether a specified config exists in configs loaded from the database
    */
   private configExistsInDB(namespace, key) {
-    if (this.configObject.fromDB[namespace] === undefined) {
+    if (this.configObject.fromDB.crowi === undefined) {
       return false;
     }
 
-    return this.configObject.fromDB[namespace][key] !== undefined;
+    return this.configObject.fromDB.crowi[key] !== undefined;
   }
 
   /**
    * check whether a specified config exists in configs loaded from the environment variables
    */
   private configExistsInEnvVars(namespace, key) {
-    if (this.configObject.fromEnvVars[namespace] === undefined) {
+    if (this.configObject.fromEnvVars.crowi === undefined) {
       return false;
     }
 
-    return this.configObject.fromEnvVars[namespace][key] !== undefined;
+    return this.configObject.fromEnvVars.crowi[key] !== undefined;
   }
 
   private convertInsertValue(value) {