Skip to content

Commit 8d61bb5

Browse files
committed
feat: impl Host decorator
1 parent 80b2a8c commit 8d61bb5

File tree

23 files changed

+386
-17
lines changed

23 files changed

+386
-17
lines changed

core/controller-decorator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from './src/decorator/Acl';
77
export * from './src/decorator/http/HTTPController';
88
export * from './src/decorator/http/HTTPMethod';
99
export * from './src/decorator/http/HTTPParam';
10+
export * from './src/decorator/http/Host';
1011
export * from './src/builder/ControllerMetaBuilderFactory';
1112
export * from './src/builder/ControllerMetaBuilder';
1213
export * from './src/util/ControllerMetadataUtil';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import ControllerInfoUtil from '../../util/ControllerInfoUtil';
2+
import { EggProtoImplClass } from '@eggjs/core-decorator';
3+
import MethodInfoUtil from '../../util/MethodInfoUtil';
4+
import assert from 'assert';
5+
6+
export function Host(host: string) {
7+
function classHost(constructor: EggProtoImplClass) {
8+
ControllerInfoUtil.addControllerHost(host, constructor);
9+
}
10+
11+
function methodHOst(target: any, propertyKey: PropertyKey) {
12+
assert(typeof propertyKey === 'string',
13+
`[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
14+
const controllerClazz = target.constructor as EggProtoImplClass;
15+
const methodName = propertyKey as string;
16+
17+
MethodInfoUtil.setMethodHost(host, controllerClazz, methodName);
18+
}
19+
20+
return function(target: any, propertyKey?: PropertyKey) {
21+
if (propertyKey === undefined) {
22+
classHost(target);
23+
} else {
24+
methodHOst(target, propertyKey);
25+
}
26+
};
27+
}

core/controller-decorator/src/impl/http/HTTPControllerMetaBuilder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ export class HTTPControllerMetaBuilder {
4343
const protoName = property!.name as string;
4444
const needAcl = ControllerInfoUtil.hasControllerAcl(this.clazz);
4545
const aclCode = ControllerInfoUtil.getControllerAcl(this.clazz);
46+
const host = ControllerInfoUtil.getControllerHost(this.clazz);
4647
const metadata = new HTTPControllerMeta(
47-
clazzName, protoName, controllerName, httpPath, httpMiddlewares, methods, needAcl, aclCode);
48+
clazzName, protoName, controllerName, httpPath, httpMiddlewares, methods, needAcl, aclCode, host);
4849
ControllerMetadataUtil.setControllerMetadata(this.clazz, metadata);
4950
for (const method of metadata.methods) {
5051
const realPath = metadata.getMethodRealPath(method);

core/controller-decorator/src/impl/http/HTTPControllerMethodMetaBuilder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ export class HTTPControllerMethodMetaBuilder {
105105
const middlewares = MethodInfoUtil.getMethodMiddlewares(this.clazz, this.methodName);
106106
const needAcl = MethodInfoUtil.hasMethodAcl(this.clazz, this.methodName);
107107
const aclCode = MethodInfoUtil.getMethodAcl(this.clazz, this.methodName);
108+
const host = MethodInfoUtil.getMethodHost(this.clazz, this.methodName);
108109
const realPath = parentPath
109110
? path.posix.join(parentPath, httpPath)
110111
: httpPath;
111112
const paramTypeMap = this.buildParamType(realPath);
112113
const priority = this.getPriority();
113-
return new HTTPMethodMeta(this.methodName, httpPath!, httpMethod!, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode);
114+
return new HTTPMethodMeta(
115+
this.methodName, httpPath!, httpMethod!, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode, host);
114116
}
115117
}

core/controller-decorator/src/model/HTTPControllerMeta.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
1414
public readonly methods: readonly HTTPMethodMeta[];
1515
public readonly needAcl: boolean;
1616
public readonly aclCode?: string;
17+
public readonly host?: string;
1718

1819
constructor(
1920
className: string,
@@ -24,6 +25,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
2425
methods: HTTPMethodMeta[],
2526
needAcl: boolean,
2627
aclCode: string | undefined,
28+
host: string | undefined,
2729
) {
2830
this.protoName = protoName;
2931
this.controllerName = controllerName;
@@ -33,6 +35,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
3335
this.methods = methods;
3436
this.needAcl = needAcl;
3537
this.aclCode = aclCode;
38+
this.host = host;
3639
}
3740

3841
getMethodRealPath(method: HTTPMethodMeta) {
@@ -42,6 +45,13 @@ export class HTTPControllerMeta implements ControllerMetadata {
4245
return method.path;
4346
}
4447

48+
getMethodHost(method: HTTPMethodMeta): string | undefined {
49+
if (this.host) {
50+
return this.host;
51+
}
52+
return method.host;
53+
}
54+
4555
getMethodName(method: HTTPMethodMeta) {
4656
return `${method.method} ${this.controllerName}.${method.name}`;
4757
}

core/controller-decorator/src/model/HTTPMethodMeta.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class HTTPMethodMeta implements MethodMeta {
7373
public readonly priority: number;
7474
public readonly needAcL: boolean;
7575
public readonly aclCode: string | undefined;
76+
public readonly host: string | undefined;
7677

7778
constructor(
7879
name: string,
@@ -84,6 +85,7 @@ export class HTTPMethodMeta implements MethodMeta {
8485
priority: number,
8586
needAcl: boolean,
8687
aclCode: string | undefined,
88+
host: string | undefined,
8789
) {
8890
this.name = name;
8991
this.path = path;
@@ -94,6 +96,7 @@ export class HTTPMethodMeta implements MethodMeta {
9496
this.priority = priority;
9597
this.needAcL = needAcl;
9698
this.aclCode = aclCode;
99+
this.host = host;
97100
}
98101
}
99102

core/controller-decorator/src/util/ControllerInfoUtil.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
33

44
export const CONTROLLER_TYPE = Symbol.for('EggPrototype#controllerType');
55
export const CONTROLLER_NAME = Symbol.for('EggPrototype#controllerName');
6+
export const CONTROLLER_HOST = Symbol.for('EggPrototype#controllerHost');
67
export const CONTROLLER_MIDDLEWARES = Symbol.for('EggPrototype#controller#middlewares');
78
export const CONTROLLER_ACL = Symbol.for('EggPrototype#controller#acl');
89

@@ -43,4 +44,12 @@ export default class ControllerInfoUtil {
4344
static getControllerAcl(clazz: EggProtoImplClass): string | undefined {
4445
return MetadataUtil.getMetaData(CONTROLLER_ACL, clazz);
4546
}
47+
48+
static addControllerHost(host: string, clazz: EggProtoImplClass) {
49+
MetadataUtil.defineMetaData(CONTROLLER_HOST, host, clazz);
50+
}
51+
52+
static getControllerHost(clazz: EggProtoImplClass): string | undefined {
53+
return MetadataUtil.getMetaData(CONTROLLER_HOST, clazz);
54+
}
4655
}

core/controller-decorator/src/util/MethodInfoUtil.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ControllerTypeLike, MiddlewareFunc } from '../model';
33
import { MapUtil } from '@eggjs/tegg-common-util';
44

55
const METHOD_CONTROLLER_TYPE_MAP = Symbol.for('EggPrototype#controller#mthods');
6+
const METHOD_CONTROLLER_HOST = Symbol.for('EggPrototype#controller#mthods#host');
67
const METHOD_CONTEXT_INDEX = Symbol.for('EggPrototype#controller#method#context');
78
const METHOD_MIDDLEWARES = Symbol.for('EggPrototype#method#middlewares');
89
const METHOD_ACL = Symbol.for('EggPrototype#method#acl');
@@ -58,4 +59,14 @@ export default class MethodInfoUtil {
5859
const methodAclMap: MethodAclMap | undefined = MetadataUtil.getMetaData(METHOD_ACL, clazz);
5960
return methodAclMap?.get(methodName);
6061
}
62+
63+
static setMethodHost(host: string, clazz: EggProtoImplClass, methodName: string) {
64+
const methodControllerMap: METHOD_MAP = MetadataUtil.initOwnMapMetaData(METHOD_CONTROLLER_HOST, clazz, new Map());
65+
methodControllerMap.set(methodName, host);
66+
}
67+
68+
static getMethodHost(clazz: EggProtoImplClass, methodName: string): string | undefined {
69+
const methodControllerMap: METHOD_MAP | undefined = MetadataUtil.getMetaData(METHOD_CONTROLLER_HOST, clazz);
70+
return methodControllerMap?.get(methodName);
71+
}
6172
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Host } from '../../src/decorator/http/Host';
2+
3+
@Host('foo.eggjs.com')
4+
export class HostController {
5+
async hello(): Promise<void> {
6+
return;
7+
}
8+
9+
@Host('bar.eggjs.com')
10+
async bar(): Promise<void> {
11+
return;
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import assert from 'assert';
2+
import { HostController } from '../fixtures/HostController';
3+
import ControllerInfoUtil from '../../src/util/ControllerInfoUtil';
4+
import MethodInfoUtil from '../../src/util/MethodInfoUtil';
5+
6+
describe('test/Host.test.ts', () => {
7+
it('controller Host work', () => {
8+
const controllerHost = ControllerInfoUtil.getControllerHost(HostController);
9+
assert(controllerHost === 'foo.eggjs.com');
10+
});
11+
12+
it('method Host work', () => {
13+
const methodHost = MethodInfoUtil.getMethodHost(HostController, 'bar');
14+
assert(methodHost === 'bar.eggjs.com');
15+
});
16+
});

0 commit comments

Comments
 (0)