🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
configuration.ts 内容如下:
import { App, Configuration, Inject } from '@midwayjs/decorator';
import {
ILifeCycle,
IMidwayContainer,
MidwayDecoratorService,
} from '@midwayjs/core';
// eslint-disable-next-line node/no-extraneous-import
import { Application } from 'egg';
import { join } from 'path';
import as oss from '@midwayjs/oss';
import as redis from '@midwayjs/redis';
import as axios from '@midwayjs/axios';
import as validate from '@midwayjs/validate';
import as midwayAuto from '@ali/midway-auto';
// import as typegoose from '@midwayjs/typegoose';
import as swagger from '@midwayjs/swagger';
import as keycenter from '@ali/midway-keycenter';
import { MethodDecorators } from './decorator';
import as bull from '@midwayjs/bull';
import as bullBoard from '@midwayjs/bull-board';
import as orm from '@midwayjs/typeorm';
import as upload from '@midwayjs/upload';
import * as crossDomain from '@midwayjs/cross-domain';
import {
MidwayErrorFilter,
OtherErrorFilter,
ValidateErrorFilter,
} from './filter/error';
configuration.ts 内容如下: import { App, Configuration, Inject } from '@midwayjs/decorator'; import { ILifeCycle, IMidwayContainer, MidwayDecoratorService, } from '@midwayjs/core'; // eslint-disable-next-line node/no-extraneous-import import { Application } from 'egg'; import { join } from 'path'; import as oss from '@midwayjs/oss'; import as redis from '@midwayjs/redis'; import as axios from '@midwayjs/axios'; import as validate from '@midwayjs/validate'; import as midwayAuto from '@ali/midway-auto'; // import as typegoose from '@midwayjs/typegoose'; import as swagger from '@midwayjs/swagger'; import as keycenter from '@ali/midway-keycenter'; import { MethodDecorators } from './decorator'; import as bull from '@midwayjs/bull'; import as bullBoard from '@midwayjs/bull-board'; import as orm from '@midwayjs/typeorm'; import as upload from '@midwayjs/upload'; import * as crossDomain from '@midwayjs/cross-domain'; import { MidwayErrorFilter, OtherErrorFilter, ValidateErrorFilter, } from './filter/error';
@Configuration({ imports: [ upload, midwayAuto, oss, redis, // 导入 redis 组件 axios, // http请求 validate, // 参数校验 keycenter, // 加载 keycenter 组件 // typegoose, // 使用mongodb 数据库需要打开这个 bull, // 任务队列 bullBoard, // bull ui orm, // typeorm crossDomain, // 跨域 { component: swagger, // 加载 swagger 组件 enabledEnvironment: ['local'], }, ], importConfigs: [join(__dirname, './config')], }) export class ContainerLifeCycle implements ILifeCycle { @App() app: Application;
@Inject() decoratorService: MidwayDecoratorService;
/**
@param container */ async onConfigLoad(container: IMidwayContainer) { console.log('config env:' + this.app.getEnv());
const keycenterService = await container.getAsync(
keycenter.KeyCenterService
);
// 调用解密接口,获取语雀token
const result = await keycenterService.decrypt(
this.app.config.keycenter.yuqueCiphertText,
'auto-edd-autosdk_yuque_key'
);
// 调用解密接口,获取oss accessKeySecret const ossSK = await keycenterService.decrypt( this.app.config.keycenter.ossCiphertText, 'auto-edd-autosdk_common_key' );
// 兼容本地环境mysql, redis连接问题 let mysqlPassword = this.app.config.typeorm.dataSource.default.password;
let redisPassword = this.app.config.redis.client.password;
if (this.app.getEnv() != 'local') { // 调用解密接口,获取mysql password mysqlPassword = await keycenterService.decrypt( this.app.config.keycenter.mysqlCiphertText, 'auto-edd-autosdk_common_key' );
// 调用解密接口,获取redis password redisPassword = await keycenterService.decrypt( this.app.config.keycenter.redisCiphertText, 'auto-edd-autosdk_common_key' ); }
// console.log('mysql Password:' + mysqlPassword); // 拿到之后你可以做一些事情,直接返回,就可以合并到默认的 config 中。 return { keycenter: { yuqueToken: result, }, oss: { client: { accessKeySecret: ossSK, }, }, redis: { client: { password: redisPassword, }, }, typeorm: { dataSource: { default: { password: mysqlPassword, }, }, }, }; }
async onReady(): Promise {
// 注册中间件 useMiddleware
// 注册全局处理器 useFilter // 错误统一处理 this.app.useFilter([ MidwayErrorFilter, ValidateErrorFilter, OtherErrorFilter, ]);
// 注册方法修饰器 for (const { key, fn } of MethodDecorators) { this.decoratorService.registerMethodHandler(key, fn); } } }
单元测试例子: import { createApp, close, createHttpRequest } from "@midwayjs/mock"; import { Framework } from "@midwayjs/web"; import { Application } from "egg";
describe("test/controller/api.test.ts", () => { let app: Application;
beforeAll(async () => { // create app app = await createApp();
});
afterAll(async () => { await close(app); });
it("should POST /ws/midway-demo/get_user", async () => { // make request const result = await createHttpRequest(app) .post("/ws/midway-demo/get_user") .query({ uid: 123 });
}); });