This commit is contained in:
陶林 2023-09-12 17:21:15 +08:00
parent ad6cc0747b
commit b53ac87c76
16 changed files with 1714 additions and 486 deletions

4
.gitignore vendored
View File

@ -13,3 +13,7 @@ run/
*.un~
.tsbuildinfo
.tsbuildinfo.*
.s
.serverless
serverless.zip

View File

@ -1,29 +1,5 @@
# my_midway_project
### swagger
## QuickStart
<!-- add docs here for user -->
see [midway docs][midway] for more detail.
### Development
```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```shell
/swagger-ui/index.html#/
```
### Deploy
```bash
$ npm start
```
### npm scripts
- Use `npm run lint` to check code style.
- Use `npm test` to run unit test.
[midway]: https://midwayjs.org

View File

@ -1,29 +0,0 @@
# my_midway_project
## 快速入门
<!-- 在此次添加使用文档 -->
如需进一步了解,参见 [midway 文档][midway]。
### 本地开发
```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```
### 部署
```bash
$ npm start
```
### 内置指令
- 使用 `npm run lint` 来做代码风格检查。
- 使用 `npm test` 来执行单元测试。
[midway]: https://midwayjs.org

View File

@ -6,14 +6,17 @@
"dependencies": {
"@midwayjs/bootstrap": "^3.12.3",
"@midwayjs/core": "^3.12.3",
"@midwayjs/cross-domain": "^3.12.3",
"@midwayjs/decorator": "^3.12.3",
"@midwayjs/info": "^3.12.3",
"@midwayjs/koa": "^3.12.3",
"@midwayjs/logger": "^2.14.0",
"@midwayjs/validate": "^3.12.3"
"@midwayjs/swagger": "^3.12.4",
"@midwayjs/validate": "^3.12.3",
"@midwayjs/view-ejs": "^3.12.3"
},
"devDependencies": {
"@midwayjs/cli": "^2.0.0",
"@midwayjs/cli": "^2.1.1",
"@midwayjs/mock": "^3.12.3",
"@types/jest": "^29.2.0",
"@types/koa": "^2.13.4",
@ -21,6 +24,7 @@
"cross-env": "^6.0.0",
"jest": "^29.2.2",
"mwts": "^1.0.5",
"swagger-ui-dist": "^5.6.2",
"ts-jest": "^29.0.3",
"typescript": "~4.8.0"
},
@ -46,5 +50,9 @@
"url": ""
},
"author": "anonymous",
"license": "MIT"
"license": "MIT",
"optionalDependencies": {
"@midwayjs/cli-plugin-faas": "^2.1.2",
"@midwayjs/cli-plugin-generator": "^2.1.0"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,18 @@
import { MidwayConfig } from '@midwayjs/core';
export default {
// use for cookie sign key, should change to your own and keep security
keys: '1693555233471_3819',
keys: 'XkGaNxf2Q9b7zY2eDVVuNx',
koa: {
port: 7001,
port: 3000,
},
cors: {
origin: "*",
},
view: {
defaultExtension: '.ejs',
mapping: {
'.ejs': 'ejs',
},
},
ejs: {},
} as MidwayConfig;

View File

@ -0,0 +1,7 @@
import { MidwayConfig } from "@midwayjs/core";
export default {
koa: {
port: 3000
},
} as MidwayConfig

View File

@ -0,0 +1,7 @@
import { MidwayConfig } from "@midwayjs/core";
export default {
koa: {
port: 3000
},
} as MidwayConfig

View File

@ -6,15 +6,25 @@ import { join } from 'path';
// import { DefaultErrorFilter } from './filter/default.filter';
// import { NotFoundFilter } from './filter/notfound.filter';
import { ReportMiddleware } from './middleware/report.middleware';
import * as swagger from '@midwayjs/swagger';
import * as view from '@midwayjs/view-ejs';
import * as crossDomain from '@midwayjs/cross-domain';
@Configuration({
imports: [
koa,
validate,
view,
crossDomain,
{
component: info,
enabledEnvironment: ['local'],
},
{
component: swagger,
enabledEnvironment: ['local']
}
],
importConfigs: [join(__dirname, './config')],
})
@ -23,9 +33,6 @@ export class MainConfiguration {
app: koa.Application;
async onReady() {
// add middleware
this.app.useMiddleware([ReportMiddleware]);
// add filter
// this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
}
}

View File

@ -1,18 +0,0 @@
import { Inject, Controller, Get, Query } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
@Controller('/api')
export class APIController {
@Inject()
ctx: Context;
@Inject()
userService: UserService;
@Get('/get_user')
async getUser(@Query('uid') uid) {
const user = await this.userService.getUser({ uid });
return { success: true, message: 'OK', data: user };
}
}

View File

@ -1,9 +1,15 @@
import { Controller, Get } from '@midwayjs/core';
import { Controller, Get, Inject } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
@Controller('/')
export class HomeController {
@Inject()
ctx: Context;
@Get('/')
async home(): Promise<string> {
return 'Hello Midwayjs!';
async home() {
await this.ctx.render('index', {
data: 'world',
});
}
}

View File

@ -1,6 +1,7 @@
/**
* @description User-Service parameters
*/
export interface IUserOptions {
uid: number;
}
declare module '@midwayjs/core' {
interface Context {
}
}

View File

@ -1,14 +0,0 @@
import { Provide } from '@midwayjs/core';
import { IUserOptions } from '../interface';
@Provide()
export class UserService {
async getUser(options: IUserOptions) {
return {
uid: options.uid,
username: 'mockedName',
phone: '12345678901',
email: 'xxx.xxx@xxx.com',
};
}
}

View File

@ -1,20 +0,0 @@
import { createApp, close, createHttpRequest } from '@midwayjs/mock';
import { Framework } from '@midwayjs/koa';
describe('test/controller/home.test.ts', () => {
it('should POST /api/get_user', async () => {
// create app
const app = await createApp<Framework>();
// make request
const result = await createHttpRequest(app).get('/api/get_user').query({ uid: 123 });
// use expect by jest
expect(result.status).toBe(200);
expect(result.body.message).toBe('OK');
// close app
await close(app);
});
});

View File

@ -1,21 +0,0 @@
import { createApp, close, createHttpRequest } from '@midwayjs/mock';
import { Framework } from '@midwayjs/koa';
describe('test/controller/home.test.ts', () => {
it('should GET /', async () => {
// create app
const app = await createApp<Framework>();
// make request
const result = await createHttpRequest(app).get('/');
// use expect by jest
expect(result.status).toBe(200);
expect(result.text).toBe('Hello Midwayjs!');
// close app
await close(app);
});
});

12
view/index.ejs Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Midway</h1>
<a href="/swagger-ui/index.html#/" target="_blank">swagger</a>
</body>
</html>