diff --git a/packages/core/telemetry/src/telemetry.ts b/packages/core/telemetry/src/telemetry.ts index c3d1f4eaf..4322b6e1f 100644 --- a/packages/core/telemetry/src/telemetry.ts +++ b/packages/core/telemetry/src/telemetry.ts @@ -10,7 +10,7 @@ import { telemetryOptions } from './config'; import { Metric, MetricOptions } from './metric'; import { Trace, TraceOptions } from './trace'; -export { telemetryOptions } from './config'; +export { telemetryOptions }; export interface TelemetryOptions { serviceName?: string; diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/README.md b/packages/plugins/@tachybase/plugin-multi-app-manager/README.md index 910ff8081..e23a38e73 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/README.md +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/README.md @@ -11,3 +11,25 @@ yarn pm enable multi-app-manager ``` ## 使用方法 + +注意目前仅支持部分在应用启动时传入的环境变量,具体列表如下: + +```ini +APP_KEY=test-key +API_BASE_PATH=/api/ +DB_DIALECT=sqlite +DB_STORAGE=storage/db/tachybase.sqlite +DB_TABLE_PREFIX= +DB_HOST=localhost +DB_PORT=5432 +DB_DATABASE=postgres +DB_USER=tachybase +DB_PASSWORD=tachybase +DB_UNDERSCORED=false +DB_TIMEZONE=+08:00 +DB_TABLE_PREFIX= +DB_SCHEMA= +CACHE_DEFAULT_STORE=memory +CACHE_MEMORY_MAX=2000 +``` + diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/README.zh-CN.md b/packages/plugins/@tachybase/plugin-multi-app-manager/README.zh-CN.md index c220fc200..39caaabea 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/README.zh-CN.md +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/README.zh-CN.md @@ -11,3 +11,25 @@ yarn pm enable multi-app-manager ``` ## 使用方法 + +注意目前仅支持部分在应用启动时传入的环境变量,具体列表如下: + +```ini +APP_KEY=test-key +API_BASE_PATH=/api/ +DB_DIALECT=sqlite +DB_STORAGE=storage/db/tachybase.sqlite +DB_TABLE_PREFIX= +DB_HOST=localhost +DB_PORT=5432 +DB_DATABASE=postgres +DB_USER=tachybase +DB_PASSWORD=tachybase +DB_UNDERSCORED=false +DB_TIMEZONE=+08:00 +DB_TABLE_PREFIX= +DB_SCHEMA= +CACHE_DEFAULT_STORE=memory +CACHE_MEMORY_MAX=2000 +``` + diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts b/packages/plugins/@tachybase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts index 9f73240a8..b4dd18c48 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts @@ -168,6 +168,12 @@ export const formSchema: ISchema = { 'x-decorator': 'FormItem', 'x-disabled': '{{ !createOnly }}', }, + 'options.startEnvs': { + type: 'string', + title: i18nText('Start environment variables'), + 'x-component': 'Input.TextArea', + 'x-decorator': 'FormItem', + }, pinned: { 'x-component': 'CollectionField', 'x-decorator': 'FormItem', diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/en-US.json b/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/en-US.json index a47f0347b..1c7fd5471 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/en-US.json +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/en-US.json @@ -20,5 +20,6 @@ "Start": "Start", "Stop": "Stop", "Refresh": "Refresh", - "Template is in use": "Template is in use" + "Template is in use": "Template is in use", + "Start environment variables": "Start environment variables" } diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/zh-CN.json b/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/zh-CN.json index 179d95a5e..0c0612562 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/zh-CN.json +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/src/locale/zh-CN.json @@ -20,5 +20,6 @@ "Start": "启动", "Stop": "停止", "Refresh": "刷新", - "Template is in use": "模板正在使用" + "Template is in use": "模板正在使用", + "Start environment variables": "启动环境变量" } diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/actions/apps.ts b/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/actions/apps.ts index 545f30d44..4f8ee5f47 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/actions/apps.ts +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/actions/apps.ts @@ -41,6 +41,19 @@ export async function create(ctx: Context, next: Next) { const params = ctx.action.params; const tmpl = params.values?.tmpl; if (tmpl) { + const startEnvs = params.values?.options?.startEnvs; + if (startEnvs) { + const dbDialect = startEnvs.split('\n').find((line) => line.startsWith('DB_DIALECT=')); + if (dbDialect) { + const dbType = dbDialect.split('=')[1].trim(); + if (dbType !== 'postgres') { + ctx.throw( + 400, + ctx.t('This database does not support to create application using template', { ns: NAMESPACE }), + ); + } + } + } const dbType = ctx.db.options.dialect; if (dbType !== 'postgres') { ctx.throw(400, ctx.t('This database does not support to create application using template', { ns: NAMESPACE })); diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/app-start-env.ts b/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/app-start-env.ts new file mode 100644 index 000000000..4da7ed975 --- /dev/null +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/app-start-env.ts @@ -0,0 +1,91 @@ +import { Application } from '@tachybase/server'; + +export default function mergeApplicationStartEnvs(appName: String, mainApp: Application, options: any, startEnvs: any) { + if (typeof startEnvs === 'string') { + const envs = startEnvs.trim().split('\n'); + startEnvs = {}; + for (const env of envs) { + const [key, value] = env.split('='); + startEnvs[key] = value; + } + } + if (startEnvs && Object.keys(startEnvs).length !== 0) { + // TODO: 因为环境变量和对应的配置项无法直接平铺,所以需要逐个匹配,以后或许有更好的解决方案 + if (startEnvs.APP_KEY) { + options = { + ...options, + authManager: { + jwt: { + secret: startEnvs.APP_KEY, + }, + }, + }; + delete startEnvs.APP_KEY; + } + if (startEnvs.API_BASE_PATH) { + options.resourcer.prefix = startEnvs.API_BASE_PATH; + delete startEnvs.API_BASE_PATH; + } + if (startEnvs.DB_DIALECT) { + options.database.dialect = startEnvs.DB_DIALECT; + delete startEnvs.DB_DIALECT; + } + if (startEnvs.DB_STORAGE) { + options.database.storage = startEnvs.DB_STORAGE; + delete startEnvs.DB_STORAGE; + } + if (startEnvs.DB_TABLE_PREFIX) { + options.database.tablePrefix = startEnvs.DB_TABLE_PREFIX; + delete startEnvs.DB_TABLE_PREFIX; + } + if (startEnvs.DB_HOST) { + options.database.host = startEnvs.DB_HOST; + delete startEnvs.DB_HOST; + } + if (startEnvs.DB_PORT) { + options.database.port = parseInt(startEnvs.DB_PORT); + delete startEnvs.DB_PORT; + } + if (startEnvs.DB_DATABASE) { + options.database.database = startEnvs.DB_DATABASE; + delete startEnvs.DB_DATABASE; + } + if (startEnvs.DB_USER) { + options.database.username = startEnvs.DB_USER; + delete startEnvs.DB_USER; + } + if (startEnvs.DB_PASSWORD) { + options.database.password = startEnvs.DB_PASSWORD; + delete startEnvs.DB_PASSWORD; + } + if (startEnvs.DB_UNDERSCORED) { + options.database.underscored = startEnvs.DB_UNDERSCORED === 'true'; + delete startEnvs.DB_UNDERSCORED; + } + if (startEnvs.DB_TIMEZONE) { + options.database.timezone = startEnvs.DB_TIMEZONE; + delete startEnvs.DB_TIMEZONE; + } + if (startEnvs.DB_TABLE_PREFIX) { + options.database.tablePrefix = startEnvs.DB_TABLE_PREFIX; + delete startEnvs.DB_TABLE_PREFIX; + } + if (startEnvs.DB_SCHEMA) { + options.database.schema = startEnvs.DB_SCHEMA; + delete startEnvs.DB_SCHEMA; + } + if (startEnvs.CACHE_DEFAULT_STORE) { + options.cacheManager.defaultStore = startEnvs.CACHE_DEFAULT_STORE; + delete startEnvs.CACHE_DEFAULT_STORE; + } + if (startEnvs.CACHE_MEMORY_MAX) { + options.cacheManager.stores.memory.max = parseInt(startEnvs.CACHE_MEMORY_MAX); + delete startEnvs.CACHE_MEMORY_MAX; + } + + if (Object.keys(startEnvs).length !== 0) { + mainApp.logger.warn(`Application ${appName} has unsupported startEnvs: ${JSON.stringify(startEnvs)}`); + } + } + return options; +} diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/models/application.ts b/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/models/application.ts index 14a71a200..9d8671335 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/models/application.ts +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/src/server/models/application.ts @@ -2,6 +2,7 @@ import { Model, Transactionable } from '@tachybase/database'; import { Application } from '@tachybase/server'; import { merge } from '@tachybase/utils'; +import mergeApplicationStartEnvs from '../app-start-env'; import { AppOptionsFactory } from '../server'; export interface registerAppOptions extends Transactionable { @@ -15,8 +16,10 @@ export class ApplicationModel extends Model { const preset = this.get('preset') as string; const tmpl = this.get('tmpl') as string; const appModelOptions = (this.get('options') as any) || {}; + const startEnvs = appModelOptions.startEnvs || {}; - const appOptions = options.appOptionsFactory(appName, mainApp, preset); + let appOptions = options.appOptionsFactory(appName, mainApp, preset); + appOptions = mergeApplicationStartEnvs(appName, mainApp, appOptions, startEnvs); const subAppOptions = { ...(merge(appOptions, appModelOptions) as object), diff --git a/packages/plugins/@tachybase/plugin-multi-app-manager/src/swagger/index.ts b/packages/plugins/@tachybase/plugin-multi-app-manager/src/swagger/index.ts index 425226364..611eff74e 100644 --- a/packages/plugins/@tachybase/plugin-multi-app-manager/src/swagger/index.ts +++ b/packages/plugins/@tachybase/plugin-multi-app-manager/src/swagger/index.ts @@ -176,8 +176,18 @@ export default { example: true, description: '应用是否默认跟随主应用启动', }, + startEnvs: { + type: 'string', + example: 'APP_KEY=test-key\nAPI_BASE_PATH=/api/', + description: '应用启动时的环境变量', + }, }, }, + tmpl: { + type: 'string', + example: 'default', + description: '模板应用的 ID', + }, createdAt: { type: 'string', format: 'date-time',