Skip to content

Commit

Permalink
fix: load egg router before midway container ready (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Mar 12, 2021
1 parent 460bb48 commit 4640674
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class AppBootHook {
}
}

this.app.use(this.app.router.middleware());
await this.app.webFramework.loadMidwayController();
}

Expand Down
13 changes: 13 additions & 0 deletions packages/web/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { safelyGet, safeRequire } from '@midwayjs/core';
import { join } from 'path';
import { existsSync, readFileSync } from 'fs';
import { createLoggers } from './logger';
import { EggRouter as Router } from '@eggjs/router';

const ROUTER = Symbol('EggCore#router');
const EGG_LOADER = Symbol.for('egg#loader');
const EGG_PATH = Symbol.for('egg#eggPath');
const LOGGERS = Symbol('EggApplication#loggers');
Expand Down Expand Up @@ -272,6 +274,17 @@ export const createEggApplication = () => {
}
return (this as any)[LOGGERS];
}

get router() {
if ((this as any)[ROUTER]) {
return (this as any)[ROUTER];
}
const router = ((this as any)[ROUTER] = new Router(
{ sensitive: true },
this
));
return router;
}
}

return EggApplication as any;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller } from 'egg';

export default class EggTestController extends Controller {
public test() {
this.ctx.body = {
data: '中间件运行结果:' + this.ctx.test,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Application } from 'egg';

export default (app: Application) => {
const { controller, router } = app;

router.get(
'/api/egg-test',
controller.eggTest.test,
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

export type DefaultConfig = PowerPartial<EggAppConfig>;

export default (appInfo: EggAppInfo) => {
const config = {} as DefaultConfig;

// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1615306124396_5113';

// add your config here
config.middleware = ['clientCheckerMiddleware'];

config.midwayFeature = {
// true 代表使用 midway logger
// false 或者为空代表使用 egg-logger
replaceEggLogger: true
}

return config;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const security = {
csrf: false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EggPlugin } from 'egg';
export default {
logrotator: false, // disable when use @midwayjs/logger
static: false,
} as EggPlugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { App, Configuration } from '@midwayjs/decorator';
import { ILifeCycle } from '@midwayjs/core';
import { Application } from 'egg';

@Configuration()
export class ContainerLifeCycle implements ILifeCycle {

@App()
app: Application;

async onReady() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Get, Inject, Provide } from '@midwayjs/decorator';
import { Context } from 'egg';

@Provide()
@Controller('/')
export class HomeController {

@Inject()
ctx: Context;

@Get('/')
async home() {
return {
data: '中间件运行结果:' + this.ctx.test,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @description User-Service parameters
*/
export interface IUserOptions {
uid: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext } from '../../../../../../src';
import { Context } from 'egg';

@Provide()
export class ClientCheckerMiddleware implements IWebMiddleware {
resolve() {
return async (ctx: Context, next: IMidwayWebNext) => {
console.log('运行了 ClientCheckerMiddleware');
ctx.test = '来自 ClientCheckerMiddleware 的值';
await next();
};
}
}
12 changes: 12 additions & 0 deletions packages/web/test/issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,17 @@ describe('/test/issue.test.ts', () => {
expect(result.text).toEqual('Hello Midwayjs!');
await closeApp(app);
});

it('test #894 issue load egg router after midway container ready', async () => {
const app = await creatApp('issue/base-app-egg-router');
let result = await createHttpRequest(app).get('/');
expect(result.status).toEqual(200);
expect(result.text).toMatch('来自 ClientCheckerMiddleware 的值');

result = await createHttpRequest(app).get('/api/egg-test');
expect(result.status).toEqual(200);
expect(result.text).toMatch('来自 ClientCheckerMiddleware 的值');
await closeApp(app);
});
});

0 comments on commit 4640674

Please sign in to comment.