Skip to content

Commit

Permalink
fix: fix global middleware load order (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Aug 11, 2020
1 parent 016e430 commit 137dd03
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
12 changes: 2 additions & 10 deletions packages/faas/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Configuration, App, Config } from '@midwayjs/decorator';
import { Configuration, App } from '@midwayjs/decorator';
import { ILifeCycle } from '@midwayjs/core';
import { IFaaSApplication } from './interface';

Expand All @@ -9,13 +9,5 @@ export class FaaSContainerConfiguration implements ILifeCycle {
@App()
app: IFaaSApplication;

@Config('middleware')
middleware: string[];

async onReady() {
// add middleware from user config
if (this.app?.use && this.middleware?.length) {
await this.app.useMiddleware(this.middleware);
}
}
async onReady() {}
}
15 changes: 10 additions & 5 deletions packages/faas/src/starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,15 @@ export class FaaSStarter implements IFaaSStarter {
this.registerDecorator();
await this.loader.refresh();

// merge app middleware to global
if (this.webApplication?.['middleware']?.length) {
// attach global middleawre from user config
if (this.webApplication?.use) {
const middlewares = this.webApplication.getConfig('middleware') || [];
await this.webApplication.useMiddleware(middlewares);
this.globalMiddleware = this.globalMiddleware.concat(
this.webApplication['middleware']
);
}

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

Expand Down Expand Up @@ -349,9 +352,11 @@ export class FaaSStarter implements IFaaSStarter {
},

useMiddleware: async middlewares => {
const newMiddlewares = await this.loadMiddleware(middlewares);
for (const mw of newMiddlewares) {
this.webApplication.use(mw);
if (middlewares.length) {
const newMiddlewares = await this.loadMiddleware(middlewares);
for (const mw of newMiddlewares) {
this.webApplication.use(mw);
}
}
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ import { Configuration } from '@midwayjs/decorator';
@Configuration({
importConfigs: ['./config.default'],
})
export class AutoConfiguraion {}
export class AutoConfiguraion {
async onReady(container) {
container.registerObject('adb', { data: '123' });
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Provide } from '@midwayjs/decorator';
import { Provide, Inject } from '@midwayjs/decorator';
import * as assert from 'assert';

@Provide()
export class TestMiddleware {
@Inject()
adb: any;

resolve() {
return async (ctx, next) => {
assert(this.adb);
assert(ctx.logger);
ctx.requestId = 555;
await next();
Expand Down

0 comments on commit 137dd03

Please sign in to comment.