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

+ 2 - 0
apps/app/src/server/models/config.ts

@@ -9,6 +9,7 @@ import { getOrCreateModel } from '../util/mongoose-utils';
 
 
 export interface IConfig {
 export interface IConfig {
   _id: Types.ObjectId;
   _id: Types.ObjectId;
+  ns: string;
   key: string;
   key: string;
   value: string;
   value: string;
   createdAt: Date;
   createdAt: Date;
@@ -21,6 +22,7 @@ interface ModelMethods { any }
 
 
 
 
 const schema = new Schema<IConfig>({
 const schema = new Schema<IConfig>({
+  ns: { type: String },
   key: { type: String, required: true, unique: true },
   key: { type: String, required: true, unique: true },
   value: { type: String, required: true },
   value: { type: String, required: true },
 }, {
 }, {

+ 3 - 2
apps/app/src/server/routes/apiv3/slack-integration-settings.js

@@ -1,3 +1,4 @@
+import { ConfigSource } from '@growi/core/dist/interfaces';
 import { ErrorV3 } from '@growi/core/dist/models';
 import { ErrorV3 } from '@growi/core/dist/models';
 import {
 import {
   SlackbotType, REQUEST_TIMEOUT_FOR_GTOP,
   SlackbotType, REQUEST_TIMEOUT_FOR_GTOP,
@@ -176,8 +177,8 @@ module.exports = (crowi) => {
     // retrieve settings
     // retrieve settings
     const settings = {};
     const settings = {};
     if (currentBotType === SlackbotType.CUSTOM_WITHOUT_PROXY) {
     if (currentBotType === SlackbotType.CUSTOM_WITHOUT_PROXY) {
-      settings.slackSigningSecretEnvVars = configManager.getConfigFromEnvVars('crowi', 'slackbot:withoutProxy:signingSecret');
-      settings.slackBotTokenEnvVars = configManager.getConfigFromEnvVars('crowi', 'slackbot:withoutProxy:botToken');
+      settings.slackSigningSecretEnvVars = configManager.getConfig('slackbot:withoutProxy:signingSecret', ConfigSource.env);
+      settings.slackBotTokenEnvVars = configManager.getConfig('slackbot:withoutProxy:botToken', ConfigSource.env);
       settings.slackSigningSecret = configManager.getConfig('crowi', 'slackbot:withoutProxy:signingSecret');
       settings.slackSigningSecret = configManager.getConfig('crowi', 'slackbot:withoutProxy:signingSecret');
       settings.slackBotToken = configManager.getConfig('crowi', 'slackbot:withoutProxy:botToken');
       settings.slackBotToken = configManager.getConfig('crowi', 'slackbot:withoutProxy:botToken');
       settings.commandPermission = configManager.getConfig('crowi', 'slackbot:withoutProxy:commandPermission');
       settings.commandPermission = configManager.getConfig('crowi', 'slackbot:withoutProxy:commandPermission');

+ 28 - 24
apps/app/src/server/service/config-manager/config-manager.ts

@@ -1,6 +1,5 @@
-import type {
-  IConfigManager, ConfigSource, UpdateConfigOptions, RawConfigData,
-} from '@growi/core/dist/interfaces';
+import type { IConfigManager, UpdateConfigOptions, RawConfigData } from '@growi/core/dist/interfaces';
+import { ConfigSource } from '@growi/core/dist/interfaces';
 import { parseISO } from 'date-fns/parseISO';
 import { parseISO } from 'date-fns/parseISO';
 
 
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
@@ -10,8 +9,9 @@ import type { S2sMessagingService } from '../s2s-messaging/base';
 import type { S2sMessageHandlable } from '../s2s-messaging/handlable';
 import type { S2sMessageHandlable } from '../s2s-messaging/handlable';
 
 
 import type { ConfigKey, ConfigValues } from './config-definition';
 import type { ConfigKey, ConfigValues } from './config-definition';
-import { ENV_ONLY_GROUPS } from './config-definition';
+import { CONFIG_KEYS, ENV_ONLY_GROUPS } from './config-definition';
 import { ConfigLoader } from './config-loader';
 import { ConfigLoader } from './config-loader';
+import { configManager as configManagerLegacy } from './legacy/config-manager';
 
 
 
 
 const logger = loggerFactory('growi:service:ConfigManager');
 const logger = loggerFactory('growi:service:ConfigManager');
@@ -57,6 +57,9 @@ export class ConfigManager implements IConfigManagerForApp, S2sMessageHandlable
     else {
     else {
       this.envConfig = await this.configLoader.loadFromEnv();
       this.envConfig = await this.configLoader.loadFromEnv();
       this.dbConfig = await this.configLoader.loadFromDB();
       this.dbConfig = await this.configLoader.loadFromDB();
+
+      // Load legacy configs
+      await configManagerLegacy.loadConfigs();
     }
     }
 
 
     this.lastLoadedAt = new Date();
     this.lastLoadedAt = new Date();
@@ -68,20 +71,35 @@ export class ConfigManager implements IConfigManagerForApp, S2sMessageHandlable
    */
    */
   getConfig<K extends ConfigKey>(ns: string, key: K): ConfigValues[K];
   getConfig<K extends ConfigKey>(ns: string, key: K): ConfigValues[K];
 
 
-  getConfig<K extends ConfigKey>(key: K): ConfigValues[K];
+  getConfig<K extends ConfigKey>(key: K, source?: ConfigSource): ConfigValues[K];
 
 
-  getConfig<K extends ConfigKey>(...args: [key: K] | [ns: string, key: K]): ConfigValues[K] {
-    const key = (args.length === 2) ? args[1] : args[0];
+  getConfig<K extends ConfigKey>(...args: [key: K, source: ConfigSource | undefined] | [ns: string, key: K]): ConfigValues[K] {
+    const source = (args[1] === undefined || args[1] === ConfigSource.env || args[1] === ConfigSource.db) ? args[1] : undefined;
+    const key = (args[0] in CONFIG_KEYS ? args[0] : args[1]) as K;
+    const ns = ((args[1] === undefined || args[1] === ConfigSource.env || args[1] === ConfigSource.db) ? undefined : args[0]);
 
 
     if (!this.envConfig || !this.dbConfig) {
     if (!this.envConfig || !this.dbConfig) {
       throw new Error('Config is not loaded');
       throw new Error('Config is not loaded');
     }
     }
 
 
-    if (this.shouldUseEnvOnly(key)) {
-      return this.envConfig[key].value as ConfigValues[K];
+    const value = (() => {
+      if (source === ConfigSource.env) {
+        return this.envConfig[key]?.value;
+      }
+      if (source === ConfigSource.db) {
+        return this.dbConfig[key]?.value;
+      }
+      return this.shouldUseEnvOnly(key)
+        ? this.envConfig[key]?.value
+        : (this.dbConfig[key] ?? this.envConfig[key])?.value;
+    })() as ConfigValues[K];
+
+    const valueByLegacy = configManagerLegacy.getConfig(ns, key);
+    if (value !== valueByLegacy) {
+      logger.warn(`The value of the config key '${key}' is different between the new and legacy config managers: `, { value, valueByLegacy });
     }
     }
 
 
-    return (this.dbConfig[key] ?? this.envConfig[key])?.value as ConfigValues[K];
+    return value;
   }
   }
 
 
   private shouldUseEnvOnly(key: ConfigKey): boolean {
   private shouldUseEnvOnly(key: ConfigKey): boolean {
@@ -152,20 +170,6 @@ export class ConfigManager implements IConfigManagerForApp, S2sMessageHandlable
     }
     }
   }
   }
 
 
-  getRawConfigData(): {
-    env: RawConfigData<ConfigKey, ConfigValues>;
-    db: RawConfigData<ConfigKey, ConfigValues>;
-    } {
-    if (!this.envConfig || !this.dbConfig) {
-      throw new Error('Config is not loaded');
-    }
-
-    return {
-      env: this.envConfig,
-      db: this.dbConfig,
-    };
-  }
-
   getManagedEnvVars(showSecretValues = false): Record<string, string> {
   getManagedEnvVars(showSecretValues = false): Record<string, string> {
     if (!this.envConfig) {
     if (!this.envConfig) {
       throw new Error('Config is not loaded');
       throw new Error('Config is not loaded');

+ 12 - 11
packages/core/src/interfaces/config-manager.ts

@@ -1,8 +1,11 @@
 /**
 /**
  * Available configuration sources
  * Available configuration sources
  */
  */
-export const CONFIG_SOURCES = ['env', 'db'] as const;
-export type ConfigSource = typeof CONFIG_SOURCES[number];
+export const ConfigSource = {
+  env: 'env',
+  db: 'db',
+} as const;
+export type ConfigSource = typeof ConfigSource[keyof typeof ConfigSource];
 
 
 /**
 /**
  * Metadata for a configuration value
  * Metadata for a configuration value
@@ -56,7 +59,13 @@ export interface IConfigManager<K extends string, V extends Record<K, any>> {
   /**
   /**
    * Get a configuration value
    * Get a configuration value
    */
    */
-  getConfig<T extends K>(key: T): V[T];
+  getConfig<T extends K>(key: T, source?: ConfigSource): V[T];
+
+  /**
+   * @deprecated
+   * Get a configuration value
+   */
+  getConfig<T extends K>(ns: string, key: T): V[T];
 
 
   /**
   /**
    * Update a configuration value
    * Update a configuration value
@@ -73,14 +82,6 @@ export interface IConfigManager<K extends string, V extends Record<K, any>> {
    */
    */
   removeConfigs(keys: K[], options?: UpdateConfigOptions): Promise<void>;
   removeConfigs(keys: K[], options?: UpdateConfigOptions): Promise<void>;
 
 
-  /**
-   * Get raw configuration data for UI display
-   */
-  getRawConfigData(): {
-    env: RawConfigData<K, V[K]>;
-    db: RawConfigData<K, V[K]>;
-  };
-
   /**
   /**
    * Get environment variables managed with ConfigDefinitions
    * Get environment variables managed with ConfigDefinitions
    */
    */