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 15, 2022
1 parent 0378f16 commit 805f6e8
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 48 deletions.
10 changes: 7 additions & 3 deletions core/controller-decorator/src/decorator/http/Host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import assert from 'assert';
import { HostType } from '../../model';

export function Host(host: HostType) {

function parseHost(): string[] {
return Array.isArray(host) ? host : [ host ];
}

function classHost(constructor: EggProtoImplClass) {
ControllerInfoUtil.addControllerHost(host, constructor);
ControllerInfoUtil.addControllerHosts(parseHost(), 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);
MethodInfoUtil.setMethodHosts(parseHost(), controllerClazz, methodName);
}

return function(target: any, propertyKey?: PropertyKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class HTTPControllerMetaBuilder {
const protoName = property!.name as string;
const needAcl = ControllerInfoUtil.hasControllerAcl(this.clazz);
const aclCode = ControllerInfoUtil.getControllerAcl(this.clazz);
const host = ControllerInfoUtil.getControllerHost(this.clazz);
const host = ControllerInfoUtil.getControllerHosts(this.clazz);
const metadata = new HTTPControllerMeta(
clazzName, protoName, controllerName, httpPath, httpMiddlewares, methods, needAcl, aclCode, host);
ControllerMetadataUtil.setControllerMetadata(this.clazz, metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ export class HTTPControllerMethodMetaBuilder {
const middlewares = MethodInfoUtil.getMethodMiddlewares(this.clazz, this.methodName);
const needAcl = MethodInfoUtil.hasMethodAcl(this.clazz, this.methodName);
const aclCode = MethodInfoUtil.getMethodAcl(this.clazz, this.methodName);
const host = MethodInfoUtil.getMethodHost(this.clazz, this.methodName);
const hosts = MethodInfoUtil.getMethodHosts(this.clazz, this.methodName);
const realPath = parentPath
? path.posix.join(parentPath, httpPath)
: httpPath;
const paramTypeMap = this.buildParamType(realPath);
const priority = this.getPriority();
return new HTTPMethodMeta(
this.methodName, httpPath!, httpMethod!, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode, host);
this.methodName, httpPath!, httpMethod!, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode, hosts);
}
}
16 changes: 8 additions & 8 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 hosts?: 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,
hosts: string[] | undefined,
) {
this.protoName = protoName;
this.controllerName = controllerName;
Expand All @@ -35,7 +35,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
this.methods = methods;
this.needAcl = needAcl;
this.aclCode = aclCode;
this.host = host;
this.host = hosts;
}

getMethodRealPath(method: HTTPMethodMeta) {
Expand All @@ -45,11 +45,11 @@ export class HTTPControllerMeta implements ControllerMetadata {
return method.path;
}

getMethodHost(method: HTTPMethodMeta): HostType | undefined {
if (this.host) {
return this.host;
getMethodHosts(method: HTTPMethodMeta): string[] | undefined {
if (this.hosts) {
return this.hosts;
}
return method.host;
return method.hosts;
}

getMethodName(method: HTTPMethodMeta) {
Expand Down
8 changes: 4 additions & 4 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 hosts: 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,
hosts: string[] | undefined,
) {
this.name = name;
this.path = path;
Expand All @@ -96,7 +96,7 @@ export class HTTPMethodMeta implements MethodMeta {
this.priority = priority;
this.needAcL = needAcl;
this.aclCode = aclCode;
this.host = host;
this.hosts = hosts;
}
}

Expand Down
8 changes: 4 additions & 4 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) {
MetadataUtil.defineMetaData(CONTROLLER_HOST, host, clazz);
static addControllerHosts(hosts: string[], clazz: EggProtoImplClass) {
MetadataUtil.defineMetaData(CONTROLLER_HOST, hosts, clazz);
}

static getControllerHost(clazz: EggProtoImplClass): string | undefined {
static getControllerHosts(clazz: EggProtoImplClass): string[] | undefined {
return MetadataUtil.getMetaData(CONTROLLER_HOST, clazz);
}
}
12 changes: 6 additions & 6 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 setMethodHosts(hosts: string[], clazz: EggProtoImplClass, methodName: string) {
const methodControllerMap: METHOD_MAP = MetadataUtil.initOwnMapMetaData(METHOD_CONTROLLER_HOST, clazz, new Map());
methodControllerMap.set(methodName, host);
methodControllerMap.set(methodName, hosts);
}

static getMethodHost(clazz: EggProtoImplClass, methodName: string): HostType | undefined {
static getMethodHosts(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;
}
}
8 changes: 4 additions & 4 deletions core/controller-decorator/test/http/Host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ 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');
const controllerHost = ControllerInfoUtil.getControllerHosts(HostController);
assert(controllerHost![0] === 'foo.eggjs.com');
});

it('method Host work', () => {
const methodHost = MethodInfoUtil.getMethodHost(HostController, 'bar');
assert(methodHost === 'bar.eggjs.com');
const methodHost = MethodInfoUtil.getMethodHosts(HostController, 'bar');
assert(methodHost![0] === 'bar.eggjs.com');
});
});
22 changes: 6 additions & 16 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,22 +49,15 @@ export class HTTPMethodRegister {
this.eggContainerFactory = eggContainerFactory;
}

private hostMatch(host: HostType | undefined, 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;
const methodArgsLength = argsLength + (hasContext ? 1 : 0);
const self = this;
return async function(ctx: Context, next: Next) {
// if hosts is not empty and host is not matched, not execute
if (host && !self.hostMatch(host, ctx.host)) {
if (host && host !== ctx.host) {
return await next();
}
// HTTP decorator core implement
Expand Down Expand Up @@ -126,9 +118,8 @@ export class HTTPMethodRegister {

// 2. check duplicate with host tegg controller
let hostRouter;
const host = this.controllerMeta.getMethodHost(this.methodMeta);
const hostList = Array.isArray(host) ? host : [ host ];
hostList.forEach(h => {
const hosts = this.controllerMeta.getMethodHosts(this.methodMeta) || [];
hosts.forEach(h => {
if (h) {
hostRouter = this.checkRouters.get(h);
if (!hostRouter) {
Expand Down Expand Up @@ -170,9 +161,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 hosts = this.controllerMeta.getMethodHosts(this.methodMeta) || [ undefined ];
hosts.forEach(h => {
const handler = this.createHandler(this.methodMeta, h);
Reflect.apply(routerFunc, this.router,
[ methodName, methodRealPath, ...methodMiddlewares, handler ]);
Expand Down

0 comments on commit 805f6e8

Please sign in to comment.