Skip to content

Commit

Permalink
feat: add generateMiddleware for express and faas
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Sep 15, 2020
1 parent 5c19644 commit bfcfc9a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
7 changes: 3 additions & 4 deletions packages/decorator/src/annotation/check.ts
@@ -1,10 +1,10 @@
import 'reflect-metadata';
import * as joi from 'joi';

export function Check(failValue?: any) {
return function (target, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const origin = descriptor.value;
descriptor.value = function (...args: any[]) {
const paramTypes = Reflect.getMetadata('design:paramtypes', target, propertyKey);
const paramTypes = Reflect.getMetadata('design:paramTypes', target, propertyKey);
for (let i = 0; i < paramTypes.length; i++) {
const item = paramTypes[i];
const rules = Reflect.getMetadata('rules', item.prototype);
Expand All @@ -15,8 +15,7 @@ export function Check(failValue?: any) {
}
}
}
const result = origin.call(this, ...arguments);
return result;
return origin.call(this, ...arguments);
};
};
}
3 changes: 3 additions & 0 deletions packages/decorator/src/common/constant.ts
@@ -1,3 +1,6 @@
// got all value with no property name
export const ALL_VALUE= 'common:all_value_key';

// common
export const PRIORITY_KEY = 'common:priority';
export const SCHEDULE_KEY = 'common:schedule';
Expand Down
42 changes: 26 additions & 16 deletions packages/faas/src/framework.ts
@@ -1,32 +1,32 @@
import {
IFaaSConfigurationOptions,
IFaaSApplication,
FaaSContext,
FaaSMiddleware,
IFaaSConfigurationOptions,
IMidwayFaaSApplication,
WebMiddleware
} from './interface';
import {
IMidwayApplication,
IMidwayBootstrapOptions,
BaseFramework,
getClassMetadata,
IMiddleware,
IMidwayApplication,
IMidwayBootstrapOptions,
listModule,
listPreloadModule,
MidwayFrameworkType,
MidwayProcessTypeEnum,
MidwayRequestContainer,
REQUEST_OBJ_CTX_KEY,
BaseFramework, MidwayFrameworkType,
} from '@midwayjs/core';

import { dirname, resolve } from 'path';
import {
FUNC_KEY,
LOGGER_KEY,
PLUGIN_KEY,
} from '@midwayjs/decorator';
import { FUNC_KEY, LOGGER_KEY, PLUGIN_KEY, } from '@midwayjs/decorator';
import SimpleLock from '@midwayjs/simple-lock';
import * as compose from 'koa-compose';
import { MidwayHooks } from './hooks';

const LOCK_KEY = '_faas_starter_start_key';

// const MIDWAY_FAAS_KEY = '__midway_faas__';

export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions> {
Expand All @@ -35,7 +35,7 @@ export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions
protected funMappingStore: Map<string, any> = new Map();
protected logger;
private lock = new SimpleLock();
private webApplication: IFaaSApplication;
private webApplication: IMidwayFaaSApplication;

protected async beforeDirectoryLoad(options: Partial<IMidwayBootstrapOptions>) {
this.logger = options.logger || console;
Expand Down Expand Up @@ -80,9 +80,9 @@ export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions
const handlerName = opts.funHandler
? // @Func(key), if key is set
// or @Func({ handler })
opts.funHandler
opts.funHandler
: // else use ClassName.mehtod as handler key
covertId(funModule.name, opts.key);
covertId(funModule.name, opts.key);
this.funMappingStore.set(handlerName, {
middleware: opts.middleware || [],
mod: funModule,
Expand All @@ -100,7 +100,8 @@ export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions
}, LOCK_KEY);
}

public async stop(): Promise<void> {}
public async stop(): Promise<void> {
}

public getApplication(): IMidwayApplication {
return this.webApplication;
Expand Down Expand Up @@ -155,6 +156,11 @@ export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions
};
}

public async generateMiddleware(middlewareId: string): Promise<FaaSMiddleware> {
const mwIns = await this.getApplicationContext().getAsync<WebMiddleware>(middlewareId);
return mwIns.resolve();
}

protected getContext(context) {
if (!context.env) {
context.env = this.getApplicationContext()
Expand Down Expand Up @@ -205,7 +211,7 @@ export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions
}
throw new Error(
`no handler setup on ${target.name}#${method ||
this.defaultHandlerMethod}`
this.defaultHandlerMethod}`
);
}

Expand Down Expand Up @@ -240,7 +246,7 @@ export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions
// this.initConfiguration('./configuration', __dirname);
}

private defineApplicationProperties(app): IFaaSApplication {
private defineApplicationProperties(app): IMidwayFaaSApplication {
return Object.assign(app, {
getBaseDir: () => {
return this.baseDir;
Expand Down Expand Up @@ -292,6 +298,10 @@ export class MidwayFaaSFramework extends BaseFramework<IFaaSConfigurationOptions
}
}
},

generateMiddleware: async (middlewareId: string) => {
return this.generateMiddleware(middlewareId);
}
});
}

Expand Down
21 changes: 16 additions & 5 deletions packages/faas/src/interface.ts
Expand Up @@ -2,14 +2,21 @@ import { MidwayRequestContainer, IMidwayApplication, IMidwayLogger } from '@midw
import { FaaSHTTPContext } from '@midwayjs/faas-typings';
import type { MidwayHooks } from './hooks';

export interface IFaaSApplication extends IMidwayApplication {
export type FaaSMiddleware = (() => (context: FaaSContext, next: () => Promise<any>) => any) | string;

export interface IMidwayFaaSApplication extends IMidwayApplication {
getInitializeContext();
use(
middleware: (() => (context: any, next: () => Promise<any>) => any) | string
);
use(middleware: FaaSMiddleware);
useMiddleware(mw: string[]);
generateMiddleware(middlewareId: string): Promise<FaaSMiddleware>;
}

/**
* @deprecated
* use IMidwayFaaSApplication instead
*/
export type IFaaSApplication = IMidwayFaaSApplication;

/**
* @deprecated
*/
Expand All @@ -30,6 +37,10 @@ export interface IFaaSConfigurationOptions {
middleware?: string[];
initializeContext?: object;
applicationAdapter?: {
getApplication(): IFaaSApplication;
getApplication(): IMidwayFaaSApplication;
};
}

export interface WebMiddleware {
resolve(): FaaSMiddleware;
}
15 changes: 11 additions & 4 deletions packages/web-express/src/framework.ts
Expand Up @@ -169,6 +169,11 @@ export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigur
}
}

public async generateMiddleware(middlewareId: string) {
const mwIns = await this.getApplicationContext().getAsync<WebMiddleware>(middlewareId);
return mwIns.resolve();
}

protected async preRegisterRouter(
target: any,
controllerId: string
Expand Down Expand Up @@ -312,10 +317,12 @@ export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigur
return MidwayProcessTypeEnum.APPLICATION;
},

generateController: (controllerMapping: string,
routeArgsInfo?: RouterParamValue[],
routerResponseData?: any []) => {
return this.generateController(controllerMapping, routeArgsInfo, routerResponseData);
generateController: (controllerMapping: string) => {
return this.generateController(controllerMapping);
},

generateMiddleware: async (middlewareId: string) => {
return this.generateMiddleware(middlewareId);
}
});
}
Expand Down

0 comments on commit bfcfc9a

Please sign in to comment.