diff --git a/packages/midway-component-kmore/src/config/config.default.ts b/packages/midway-component-kmore/src/config/config.default.ts index 7fe28a177..c87b8ef43 100644 --- a/packages/midway-component-kmore/src/config/config.default.ts +++ b/packages/midway-component-kmore/src/config/config.default.ts @@ -12,13 +12,13 @@ import { export const kmoreConfig: Config = { + enableDefaultRoute: false, dataSource: {}, default: { ...initDbConfig, }, } - export const kmoreMiddlewareConfig: Readonly> = { ...initialMiddlewareConfig, ignore: [], diff --git a/packages/midway-component-kmore/src/config/config.local.ts b/packages/midway-component-kmore/src/config/config.local.ts index 5e7cf8fc0..ddf0aae4b 100644 --- a/packages/midway-component-kmore/src/config/config.local.ts +++ b/packages/midway-component-kmore/src/config/config.local.ts @@ -2,6 +2,10 @@ import { initDbConfig } from '##/lib/config.js' import { Config } from '##/lib/index.js' +export const keys = 123456 +export const koa = { + port: 7001, +} export const kmoreConfig: Config = { enableDefaultRoute: true, dataSource: {}, diff --git a/packages/midway-component-kmore/src/config/config.unittest.ts b/packages/midway-component-kmore/src/config/config.unittest.ts index 5e7cf8fc0..4c2ac6be8 100644 --- a/packages/midway-component-kmore/src/config/config.unittest.ts +++ b/packages/midway-component-kmore/src/config/config.unittest.ts @@ -2,6 +2,10 @@ import { initDbConfig } from '##/lib/config.js' import { Config } from '##/lib/index.js' +export const keys = Date.now().toString() +export const koa = { + port: 7001, +} export const kmoreConfig: Config = { enableDefaultRoute: true, dataSource: {}, diff --git a/packages/midway-component-kmore/src/configuration.ts b/packages/midway-component-kmore/src/configuration.ts index 9bc714689..73414cc7d 100644 --- a/packages/midway-component-kmore/src/configuration.ts +++ b/packages/midway-component-kmore/src/configuration.ts @@ -2,15 +2,16 @@ import assert from 'node:assert' import { App, - Config, + Config as _Config, Configuration, - MidwayEnvironmentService, - MidwayInformationService, ILifeCycle, ILogger, Inject, Logger, MidwayDecoratorService, + MidwayEnvironmentService, + MidwayInformationService, + MidwayWebRouterService, } from '@midwayjs/core' import { CacheManager } from '@mwcp/cache' import { TraceInit } from '@mwcp/otel' @@ -20,11 +21,12 @@ import { RegisterDecoratorHandlerParam, registerDecoratorHandler, registerMiddleware, + deleteRouter, } from '@mwcp/share' import { sleep } from '@waiting/shared-core' -import * as DefulatConfig from './config/config.default.js' +import * as DefaultConfig from './config/config.default.js' import * as LocalConfig from './config/config.local.js' import * as UnittestConfig from './config/config.unittest.js' import { @@ -34,7 +36,7 @@ import { } from './decorator/decorator.helper.js' import { useComponents } from './imports.js' import { DbSourceManager } from './lib/db-source-manager.js' -import { ConfigKey, KmorePropagationConfig, KmoreSourceConfig } from './lib/index.js' +import { Config, ConfigKey, KmorePropagationConfig, KmoreSourceConfig } from './lib/index.js' import { KmoreMiddleware } from './middleware/index.middleware.js' @@ -42,7 +44,7 @@ import { KmoreMiddleware } from './middleware/index.middleware.js' namespace: ConfigKey.namespace, importConfigs: [ { - default: DefulatConfig, + default: DefaultConfig, local: LocalConfig, unittest: UnittestConfig, }, @@ -55,11 +57,14 @@ export class AutoConfiguration implements ILifeCycle { @Inject() protected readonly environmentService: MidwayEnvironmentService @Inject() protected readonly informationService: MidwayInformationService + @Inject() protected readonly webRouterService: MidwayWebRouterService + @Logger() protected readonly logger: ILogger - @Config() readonly kmoreSourceConfig: KmoreSourceConfig + @_Config(ConfigKey.config) readonly config: Config + @_Config() readonly kmoreSourceConfig: KmoreSourceConfig - @Config(ConfigKey.propagationConfig) protected readonly propagationConfig: KmorePropagationConfig + @_Config(ConfigKey.propagationConfig) protected readonly propagationConfig: KmorePropagationConfig @Inject() readonly dbSManager: DbSourceManager @@ -67,6 +72,12 @@ export class AutoConfiguration implements ILifeCycle { @Inject() cacheManager: CacheManager + async onConfigLoad(): Promise { + if (! this.config.enableDefaultRoute) { + await deleteRouter(`/_${ConfigKey.namespace}`, this.webRouterService) + } + } + @TraceInit({ namespace: ConfigKey.namespace }) async onReady(container: IMidwayContainer): Promise { void container diff --git a/packages/midway-component-kmore/src/decorator/decorator.helper.ts b/packages/midway-component-kmore/src/decorator/decorator.helper.ts index 94ecf0048..1cd98aeb9 100644 --- a/packages/midway-component-kmore/src/decorator/decorator.helper.ts +++ b/packages/midway-component-kmore/src/decorator/decorator.helper.ts @@ -14,7 +14,7 @@ import { import { sleep } from '@waiting/shared-core' import { PropagationType } from 'kmore' -import { initTransactionalOptons } from '##/lib/config.js' +import { initTransactionalOptions } from '##/lib/config.js' import { CallerKey, RegisterTrxPropagateOptions } from '##/lib/propagation/trx-status.base.js' import { genCallerKey } from '##/lib/propagation/trx-status.helper.js' import { TrxStatusService } from '##/lib/trx-status.service.js' @@ -88,7 +88,7 @@ export function genDecoratorExecutorOptions( const initArgs = { propagationType: PropagationType.REQUIRED, propagationOptions: { - ...initTransactionalOptons, + ...initTransactionalOptions, }, cacheOptions: false, } diff --git a/packages/midway-component-kmore/src/imports.ts b/packages/midway-component-kmore/src/imports.ts index 6855b0395..e59cee5cf 100644 --- a/packages/midway-component-kmore/src/imports.ts +++ b/packages/midway-component-kmore/src/imports.ts @@ -1,3 +1,4 @@ +import * as info from '@midwayjs/info' import * as koa from '@midwayjs/koa' import * as swagger from '@midwayjs/swagger' import * as cache from '@mwcp/cache' @@ -13,14 +14,10 @@ const CI = !! (process.env['MIDWAY_SERVER_ENV'] === 'unittest' export const useComponents: IComponentInfo[] = [otel] if (CI) { - if ( ! useComponents.includes(koa)) { - useComponents.push(koa) - useComponents.push(info) - useComponents.push(cache) - } - if ( ! useComponents.includes(swagger)) { - useComponents.push(swagger) - } + useComponents.push(koa) + useComponents.push(info) + useComponents.push(cache) + useComponents.push(swagger) } export interface IComponentInfo { diff --git a/packages/midway-component-kmore/src/lib/config.ts b/packages/midway-component-kmore/src/lib/config.ts index 788e2a77b..876f15d72 100644 --- a/packages/midway-component-kmore/src/lib/config.ts +++ b/packages/midway-component-kmore/src/lib/config.ts @@ -30,7 +30,7 @@ export const initDbConfig: DbConfig = { traceResponse: true, } -export const initTransactionalOptons: TransactionalOptions = { +export const initTransactionalOptions: TransactionalOptions = { /** * @default {@link RowLockLevel.ForShare} */ @@ -46,6 +46,6 @@ export const initPropagationConfig: PropagationConfig = { * @default PropagationType.REQUIRED, */ propagationType: PropagationType.REQUIRED, - ...initTransactionalOptons, + ...initTransactionalOptions, } diff --git a/packages/midway-component-kmore/src/lib/types.ts b/packages/midway-component-kmore/src/lib/types.ts index b62cdcb7c..27e66caf8 100644 --- a/packages/midway-component-kmore/src/lib/types.ts +++ b/packages/midway-component-kmore/src/lib/types.ts @@ -27,7 +27,11 @@ export enum Msg { export interface Config extends BaseConfig, KmoreSourceConfig { - // void 0 + + /** + * @default false + */ + enableDefaultRoute: boolean } export interface MiddlewareOptions { diff --git a/packages/midway-component-kmore/test/config.unittest.ts b/packages/midway-component-kmore/test/config.unittest.ts index 0258ef889..8505426f8 100644 --- a/packages/midway-component-kmore/test/config.unittest.ts +++ b/packages/midway-component-kmore/test/config.unittest.ts @@ -5,6 +5,7 @@ import { EventCallbacks } from 'kmore' import { genDbDict } from 'kmore-types' import { + Config, initialMiddlewareConfig, initMiddlewareOptions, MiddlewareConfig, @@ -17,6 +18,7 @@ import { import { Db } from '#@/test.model.js' + export const mwConfig: Readonly> = { ...initialMiddlewareConfig, ignore: [], // ! @@ -62,7 +64,8 @@ export const master: DbConfig = { eventCallbacks: eventCbs, traceInitConnection: true, } -export const kmoreConfig: KmoreSourceConfig<'master'> = { +export const kmoreConfig: Config<'master'> = { + enableDefaultRoute: true, dataSource: { master, }, diff --git a/packages/midway-component-kmore/test/root.hooks.ts b/packages/midway-component-kmore/test/root.hooks.ts deleted file mode 100644 index 70863ad20..000000000 --- a/packages/midway-component-kmore/test/root.hooks.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { ConfigKey } from '##/index.js' -import { mwConfig } from '#@/config.unittest.js' -import { testConfig } from '#@/root.config.js' - - -/** - * @see https://mochajs.org/#root-hook-plugins - * beforeAll: - * - In serial mode(Mocha’s default ), before all tests begin, once only - * - In parallel mode, run before all tests begin, for each file - * beforeEach: - * - In both modes, run before each test - */ -export const mochaHooks = async () => { - - return { - // beforeAll: async () => { - // const globalConfig = { - // keys: Math.random().toString(), - // [ConfigKey.middlewareConfig]: mwConfig, - // [ConfigKey.config]: kmoreConfig, - // } - // const opts = { - // imports: [WEB], - // globalConfig, - // } - // const app = await createApp(join(__dirname, 'fixtures', 'base-app'), opts) as Application - // app.addConfigObject(globalConfig) - // testConfig.app = app - // testConfig.httpRequest = createHttpRequest(app) - // const { url } = testConfig.httpRequest.get('/') - // testConfig.host = url - - // testConfig.container = app.getApplicationContext() - // // const svc = await testConfig.container.getAsync(TaskQueueService) - // // https://midwayjs.org/docs/testing - // }, - - - afterEach: async () => { - const { app } = testConfig - app.addConfigObject({ - [ConfigKey.middlewareConfig]: mwConfig, - }) - }, - - } - -} -