Skip to content

Commit

Permalink
refactor: use hostList internal
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Dec 14, 2022
1 parent 0378f16 commit 0e1e863
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
7 changes: 4 additions & 3 deletions core/controller-decorator/src/decorator/http/Host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { HostType } from '../../model';

export function Host(host: HostType) {
function classHost(constructor: EggProtoImplClass) {
ControllerInfoUtil.addControllerHost(host, constructor);
const hostList = Array.isArray(host) ? host : [ host ];
ControllerInfoUtil.addControllerHost(hostList, constructor);
}

function methodHost(target: any, propertyKey: PropertyKey) {
assert(typeof propertyKey === 'string',
`[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
const controllerClazz = target.constructor as EggProtoImplClass;
const methodName = propertyKey as string;

MethodInfoUtil.setMethodHost(host, controllerClazz, methodName);
const hostList = Array.isArray(host) ? host : [ host ];
MethodInfoUtil.setMethodHost(hostList, controllerClazz, methodName);
}

return function(target: any, propertyKey?: PropertyKey) {
Expand Down
8 changes: 4 additions & 4 deletions core/controller-decorator/src/model/HTTPControllerMeta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { ControllerMetadata } from './ControllerMetadata';
import { ControllerType, HostType, MiddlewareFunc } from './types';
import { ControllerType, MiddlewareFunc } from './types';
import { HTTPMethodMeta } from './HTTPMethodMeta';
import { EggPrototypeName } from '@eggjs/core-decorator';

Expand All @@ -14,7 +14,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
public readonly methods: readonly HTTPMethodMeta[];
public readonly needAcl: boolean;
public readonly aclCode?: string;
public readonly host?: HostType;
public readonly host?: string[];

constructor(
className: string,
Expand All @@ -25,7 +25,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
methods: HTTPMethodMeta[],
needAcl: boolean,
aclCode: string | undefined,
host: HostType | undefined,
host: string[] | undefined,
) {
this.protoName = protoName;
this.controllerName = controllerName;
Expand All @@ -45,7 +45,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
return method.path;
}

getMethodHost(method: HTTPMethodMeta): HostType | undefined {
getMethodHost(method: HTTPMethodMeta): string[] | undefined {
if (this.host) {
return this.host;
}
Expand Down
6 changes: 3 additions & 3 deletions core/controller-decorator/src/model/HTTPMethodMeta.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import pathToRegexp from 'path-to-regexp';
import { MethodMeta } from './MethodMeta';
import { HostType, HTTPMethodEnum, HTTPParamType, MiddlewareFunc } from './types';
import { HTTPMethodEnum, HTTPParamType, MiddlewareFunc } from './types';

export abstract class ParamMeta {
type: HTTPParamType;
Expand Down Expand Up @@ -73,7 +73,7 @@ export class HTTPMethodMeta implements MethodMeta {
public readonly priority: number;
public readonly needAcL: boolean;
public readonly aclCode: string | undefined;
public readonly host: HostType | undefined;
public readonly host: string[] | undefined;

constructor(
name: string,
Expand All @@ -85,7 +85,7 @@ export class HTTPMethodMeta implements MethodMeta {
priority: number,
needAcl: boolean,
aclCode: string | undefined,
host: HostType | undefined,
host: string[] | undefined,
) {
this.name = name;
this.path = path;
Expand Down
6 changes: 3 additions & 3 deletions core/controller-decorator/src/util/ControllerInfoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ControllerTypeLike, HostType, MiddlewareFunc } from '../model';
import { ControllerTypeLike, MiddlewareFunc } from '../model';
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';

export const CONTROLLER_TYPE = Symbol.for('EggPrototype#controllerType');
Expand Down Expand Up @@ -45,11 +45,11 @@ export default class ControllerInfoUtil {
return MetadataUtil.getMetaData(CONTROLLER_ACL, clazz);
}

static addControllerHost(host: HostType, clazz: EggProtoImplClass) {
static addControllerHost(host: string[], clazz: EggProtoImplClass) {
MetadataUtil.defineMetaData(CONTROLLER_HOST, host, clazz);
}

static getControllerHost(clazz: EggProtoImplClass): string | undefined {
static getControllerHost(clazz: EggProtoImplClass): string[] | undefined {
return MetadataUtil.getMetaData(CONTROLLER_HOST, clazz);
}
}
10 changes: 5 additions & 5 deletions core/controller-decorator/src/util/MethodInfoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
import { ControllerTypeLike, HostType, MiddlewareFunc } from '../model';
import { ControllerTypeLike, MiddlewareFunc } from '../model';
import { MapUtil } from '@eggjs/tegg-common-util';

const METHOD_CONTROLLER_TYPE_MAP = Symbol.for('EggPrototype#controller#mthods');
Expand All @@ -8,7 +8,7 @@ const METHOD_CONTEXT_INDEX = Symbol.for('EggPrototype#controller#method#context'
const METHOD_MIDDLEWARES = Symbol.for('EggPrototype#method#middlewares');
const METHOD_ACL = Symbol.for('EggPrototype#method#acl');

type METHOD_MAP = Map<string, ControllerTypeLike | HostType>;
type METHOD_MAP = Map<string, ControllerTypeLike | string[]>;
type MethodContextIndexMap = Map<string, number>;
type MethodMiddlewareMap = Map<string, MiddlewareFunc[]>;
type MethodAclMap = Map<string, string | undefined>;
Expand Down Expand Up @@ -60,13 +60,13 @@ export default class MethodInfoUtil {
return methodAclMap?.get(methodName);
}

static setMethodHost(host: HostType, clazz: EggProtoImplClass, methodName: string) {
static setMethodHost(host: string[], clazz: EggProtoImplClass, methodName: string) {
const methodControllerMap: METHOD_MAP = MetadataUtil.initOwnMapMetaData(METHOD_CONTROLLER_HOST, clazz, new Map());
methodControllerMap.set(methodName, host);
}

static getMethodHost(clazz: EggProtoImplClass, methodName: string): HostType | undefined {
static getMethodHost(clazz: EggProtoImplClass, methodName: string): string[] | undefined {
const methodControllerMap: METHOD_MAP | undefined = MetadataUtil.getMetaData(METHOD_CONTROLLER_HOST, clazz);
return methodControllerMap?.get(methodName) as HostType | undefined;
return methodControllerMap?.get(methodName) as string[] | undefined;
}
}
4 changes: 2 additions & 2 deletions core/controller-decorator/test/http/Host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import MethodInfoUtil from '../../src/util/MethodInfoUtil';
describe('test/Host.test.ts', () => {
it('controller Host work', () => {
const controllerHost = ControllerInfoUtil.getControllerHost(HostController);
assert(controllerHost === 'foo.eggjs.com');
assert(controllerHost![0] === 'foo.eggjs.com');
});

it('method Host work', () => {
const methodHost = MethodInfoUtil.getMethodHost(HostController, 'bar');
assert(methodHost === 'bar.eggjs.com');
assert(methodHost![0] === 'bar.eggjs.com');
});
});
10 changes: 4 additions & 6 deletions plugin/controller/lib/impl/http/HTTPMethodRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import KoaRouter from 'koa-router';
import { Context } from 'egg';
import {
EggContext,
HostType,
HTTPControllerMeta,
HTTPMethodMeta,
HTTPParamType,
Expand Down Expand Up @@ -50,14 +49,14 @@ export class HTTPMethodRegister {
this.eggContainerFactory = eggContainerFactory;
}

private hostMatch(host: HostType | undefined, target: string) {
private hostMatch(host: string[], target: string) {
if (Array.isArray(host)) {
return host.includes(target);
}
return host === target;
}

private createHandler(methodMeta: HTTPMethodMeta, host: HostType | undefined) {
private createHandler(methodMeta: HTTPMethodMeta, host: string[] | undefined) {
const argsLength = methodMeta.paramMap.size;
const hasContext = methodMeta.contextParamIndex !== undefined;
const contextIndex = methodMeta.contextParamIndex;
Expand Down Expand Up @@ -170,9 +169,8 @@ export class HTTPMethodRegister {
if (aclMiddleware) {
methodMiddlewares.push(aclMiddleware);
}
const host = this.controllerMeta.getMethodHost(this.methodMeta);
const hostList = Array.isArray(host) ? host : [ host ];
hostList.forEach(h => {
const host = this.controllerMeta.getMethodHost(this.methodMeta) || [ undefined ];
host.forEach(h => {
const handler = this.createHandler(this.methodMeta, h);
Reflect.apply(routerFunc, this.router,
[ methodName, methodRealPath, ...methodMiddlewares, handler ]);
Expand Down

0 comments on commit 0e1e863

Please sign in to comment.