Skip to content

Commit

Permalink
feat: add rabbitmq (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Sep 27, 2020
1 parent 789b237 commit 2c03eb4
Show file tree
Hide file tree
Showing 30 changed files with 925 additions and 80 deletions.
6 changes: 3 additions & 3 deletions packages/bootstrap/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export function isTypeScriptEnvironment() {

export class BootstrapStarter {
private appDir;
private bootstrapItems: IMidwayFramework<any>[] = [];
private bootstrapItems: IMidwayFramework<any, any>[] = [];
private globalOptions: Partial<IMidwayBootstrapOptions> = {};

public configure(options: Partial<IMidwayBootstrapOptions>) {
this.globalOptions = options;
return this;
}

public load(unit: IMidwayFramework<any>) {
public load(unit: IMidwayFramework<any, any>) {
this.bootstrapItems.push(unit);
return this;
}
Expand Down Expand Up @@ -75,7 +75,7 @@ export class Bootstrap {
* load midway framework unit
* @param unit
*/
static load(unit: IMidwayFramework<any>) {
static load(unit: IMidwayFramework<any, any>) {
this.getStarter().load(unit);
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bootstrap/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
IMidwayContainer, IConfigurationOptions, MidwayFrameworkType,
} from '@midwayjs/core';

class TestFrameworkUnit implements IMidwayFramework<IConfigurationOptions> {
class TestFrameworkUnit implements IMidwayFramework<any, IConfigurationOptions> {
configurationOptions: IConfigurationOptions;
options;
app;
Expand Down
48 changes: 44 additions & 4 deletions packages/core/src/baseFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import {
IMidwayContainer,
IMidwayFramework,
MidwayFrameworkType,
MidwayProcessTypeEnum,
} from './interface';
import { ContainerLoader } from './';
import { APPLICATION_KEY, CONFIG_KEY } from '@midwayjs/decorator';

export abstract class BaseFramework<T extends IConfigurationOptions>
implements IMidwayFramework<T> {
export abstract class BaseFramework<
APP extends IMidwayApplication,
T extends IConfigurationOptions
> implements IMidwayFramework<APP, T> {
protected isTsMode = true;
protected baseDir: string;
protected appDir: string;
protected containerLoader: ContainerLoader;
public configurationOptions: T;
public app: APP;

public configure(options: T): BaseFramework<T> {
public configure(options: T): BaseFramework<APP, T> {
this.configurationOptions = options;
return this;
}
Expand Down Expand Up @@ -62,6 +66,10 @@ export abstract class BaseFramework<T extends IConfigurationOptions>

await this.afterDirectoryLoad(options);

if (!this.app.getApplicationContext) {
this.app = this.defineApplicationProperties(this.app);
}

/**
* start to load configuration and lifeCycle
*/
Expand All @@ -87,7 +95,7 @@ export abstract class BaseFramework<T extends IConfigurationOptions>

public abstract getFrameworkType(): MidwayFrameworkType;

public abstract getApplication(): IMidwayApplication;
public abstract getApplication(): APP;

public abstract run(): Promise<void>;

Expand All @@ -96,6 +104,38 @@ export abstract class BaseFramework<T extends IConfigurationOptions>
await this.containerLoader.stop();
}

protected defineApplicationProperties(app: APP): APP {
return Object.assign(app, {
getBaseDir: () => {
return this.baseDir;
},

getAppDir: () => {
return this.appDir;
},

getEnv: () => {
return this.getApplicationContext()
.getEnvironmentService()
.getCurrentEnvironment();
},

getConfig: (key?: string) => {
return this.getApplicationContext()
.getConfigService()
.getConfiguration(key);
},

getFrameworkType: () => {
return this.getFrameworkType();
},

getProcessType: () => {
return MidwayProcessTypeEnum.APPLICATION;
},
});
}

protected async beforeStop(): Promise<void> {}

protected async beforeInitialize(
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,14 @@ export interface IMidwayBootstrapOptions {

export interface IConfigurationOptions {}

export interface IMidwayFramework<T extends IConfigurationOptions> {
export interface IMidwayFramework<APP extends IMidwayApplication, T extends IConfigurationOptions> {
app: APP;
configurationOptions: T;
configure(options: T): IMidwayFramework<T>;
configure(options: T): IMidwayFramework<APP, T>;
initialize(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
run(): Promise<void>;
stop(): Promise<void>;
getApplication(): IMidwayApplication;
getApplication(): APP;
getApplicationContext(): IMidwayContainer;
getConfiguration(key?: string): any;
getCurrentEnvironment(): string;
Expand All @@ -309,8 +310,9 @@ export enum MidwayFrameworkType {
WEB_KOA = '@midwayjs/web_koa',
WEB_EXPRESS = '@midwayjs/web_express',
FAAS = '@midwayjs/faas',
RPC_HSF = '',
RPC_GRPC = '',
MS_HSF = '',
MS_GRPC = '',
MS_RABBITMQ = '@midwayjs/rabbitmq',
WS_IO = '@midwayjs/socketio',
WSS = '',
CUSTOM = ''
Expand Down
4 changes: 4 additions & 0 deletions packages/decorator/src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export const HSF_KEY = 'rpc:hsf';
export const RPC_GRPC_KEY = 'rpc:grpc';
export const RPC_DUBBO_KEY = 'rpc:dubbo';

// microservice
export const MS_CONSUMER_KEY = 'ms:consumer';
export const MS_PRODUCER_KEY = 'ms:producer';

// framework
export const CONFIG_KEY = 'config';
export const PLUGIN_KEY = 'plugin';
Expand Down
9 changes: 6 additions & 3 deletions packages/decorator/src/common/decoratorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ export function throwErrorForTest(key: decoratorKey, e: Error) {
}

/**
* save method data to class
* this method has deprecated and use savePropertyDataToClass instead
*
* @deprecated
* @param decoratorNameKey
* @param data
Expand All @@ -449,7 +450,8 @@ export function saveMethodDataToClass(
}

/**
* attach method data to class
* this method has deprecated and use attachPropertyDataToClass instead
*
* @deprecated
* @param decoratorNameKey
* @param data
Expand All @@ -471,7 +473,8 @@ export function attachMethodDataToClass(
}

/**
* get method data from class
* this method has deprecated and use getPropertyDataFromClass instead
*
* @deprecated
* @param decoratorNameKey
* @param target
Expand Down
2 changes: 2 additions & 0 deletions packages/decorator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export * from './framework/plugin';
export * from './framework/app';
export * from './ws/webSocketController';
export * from './ws/webSocketEvent';
export * from './microservice/consumer';
export * from './microservice/rabbitmqConsumer';
15 changes: 15 additions & 0 deletions packages/decorator/src/microservice/consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ScopeEnum, saveClassMetadata, saveModule, MS_CONSUMER_KEY } from '../';
import { Scope } from '../annotation';

export enum MSListenerType {
RABBITMQ = 'rabbitmq',
MTTQ = 'mttq',
}

export function Consumer(type: MSListenerType): ClassDecorator {
return (target: any) => {
saveModule(MS_CONSUMER_KEY, target);
saveClassMetadata(MS_CONSUMER_KEY, type, target);
Scope(ScopeEnum.Request)(target);
};
}
32 changes: 32 additions & 0 deletions packages/decorator/src/microservice/rabbitmqConsumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MS_CONSUMER_KEY, attachPropertyDataToClass } from '../';

export interface RabbitMQListenerOptions {
propertyKey?: string;
queueName?: string;
exchange?: string;
exclusive?: boolean;
durable?: boolean;
maxPriority?: number;
prefetch?: number;
keys?: {[keyName: string]: string};
routingKey?: string;
consumeOptions?: {
consumerTag?: string;
noLocal?: boolean;
noAck?: boolean;
exclusive?: boolean;
priority?: number;
arguments?: any;
}
}

export function RabbitMQListener(
queueName: string,
options: RabbitMQListenerOptions = {}
): MethodDecorator {
return (target: any, propertyKey: string) => {
options.queueName = queueName;
options.propertyKey = propertyKey;
attachPropertyDataToClass(MS_CONSUMER_KEY, options, target, propertyKey);
};
}
32 changes: 15 additions & 17 deletions packages/faas/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
BaseFramework,
getClassMetadata,
IMiddleware,
IMidwayApplication,
IMidwayBootstrapOptions,
listModule,
listPreloadModule,
Expand All @@ -30,21 +29,22 @@ const LOCK_KEY = '_faas_starter_start_key';
// const MIDWAY_FAAS_KEY = '__midway_faas__';

export class MidwayFaaSFramework extends BaseFramework<
IMidwayFaaSApplication,
IFaaSConfigurationOptions
> {
protected defaultHandlerMethod = 'handler';
private globalMiddleware: string[];
protected funMappingStore: Map<string, any> = new Map();
protected logger;
private lock = new SimpleLock();
private webApplication: IMidwayFaaSApplication;
public app: IMidwayFaaSApplication;

protected async beforeDirectoryLoad(
options: Partial<IMidwayBootstrapOptions>
) {
this.logger = options.logger || console;
this.globalMiddleware = this.configurationOptions.middleware || [];
this.webApplication = this.defineApplicationProperties(
this.app = this.defineApplicationProperties(
this.configurationOptions.applicationAdapter?.getApplication() || {}
);

Expand All @@ -58,16 +58,16 @@ export class MidwayFaaSFramework extends BaseFramework<
public async run() {
return this.lock.sureOnce(async () => {
// attach global middleware from user config
if (this.webApplication?.use) {
const middlewares = this.webApplication.getConfig('middleware') || [];
await this.webApplication.useMiddleware(middlewares);
if (this.app?.use) {
const middlewares = this.app.getConfig('middleware') || [];
await this.app.useMiddleware(middlewares);
this.globalMiddleware = this.globalMiddleware.concat(
this.webApplication['middleware']
this.app['middleware']
);
}

// set app keys
this.webApplication['keys'] = this.webApplication.getConfig('keys') || '';
this.app['keys'] = this.app.getConfig('keys') || '';

// store all function entry
const funModules = listModule(FUNC_KEY);
Expand Down Expand Up @@ -104,10 +104,8 @@ export class MidwayFaaSFramework extends BaseFramework<
}, LOCK_KEY);
}

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

public getApplication(): IMidwayApplication {
return this.webApplication;
public getApplication() {
return this.app;
}

public getFrameworkType(): MidwayFrameworkType {
Expand Down Expand Up @@ -184,7 +182,7 @@ export class MidwayFaaSFramework extends BaseFramework<
);
}
if (!context.hooks) {
context.hooks = new MidwayHooks(context, this.webApplication);
context.hooks = new MidwayHooks(context, this.app);
}
return context;
}
Expand Down Expand Up @@ -254,7 +252,7 @@ export class MidwayFaaSFramework extends BaseFramework<
// this.initConfiguration('./configuration', __dirname);
}

private defineApplicationProperties(app): IMidwayFaaSApplication {
protected defineApplicationProperties(app): IMidwayFaaSApplication {
return Object.assign(app, {
getBaseDir: () => {
return this.baseDir;
Expand Down Expand Up @@ -302,7 +300,7 @@ export class MidwayFaaSFramework extends BaseFramework<
if (middlewares.length) {
const newMiddlewares = await this.loadMiddleware(middlewares);
for (const mw of newMiddlewares) {
this.webApplication.use(mw);
this.app.use(mw);
}
}
},
Expand All @@ -315,13 +313,13 @@ export class MidwayFaaSFramework extends BaseFramework<

private registerDecorator() {
this.containerLoader.registerHook(PLUGIN_KEY, (key, target) => {
return target[REQUEST_OBJ_CTX_KEY]?.[key] || this.webApplication[key];
return target[REQUEST_OBJ_CTX_KEY]?.[key] || this.app[key];
});

this.containerLoader.registerHook(LOGGER_KEY, (key, target) => {
return (
target[REQUEST_OBJ_CTX_KEY]?.['logger'] ||
this.webApplication.getLogger()
this.app.getLogger()
);
});
}
Expand Down
Loading

0 comments on commit 2c03eb4

Please sign in to comment.