Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix global middleware load order #594

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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