Skip to content

Commit 040e027

Browse files
author
pedro
committed
feat:添加接口限频
1 parent 4d769be commit 040e027

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

app/api/cms/file.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
'use strict';
22

3-
const { LinRouter } = require('lin-mizar');
3+
const { LinRouter, NotFound } = require('lin-mizar');
4+
const { ratelimit } = require('lin-mizar/lin/limiter');
45
const { LocalUploader } = require('../../extensions/file/local-uploader');
6+
const redis = require('redis');
7+
8+
const client = redis.createClient();
59

610
const file = new LinRouter({
711
prefix: '/cms/file'
812
});
913

1014
file.post('/', async ctx => {
1115
const files = await ctx.multipart();
16+
if (files.length < 1) {
17+
throw new NotFound({ msg: '未找到符合条件的文件资源' });
18+
}
1219
const uploader = new LocalUploader('app/assets');
1320
await uploader.upload(files);
1421
ctx.success();
1522
});
1623

24+
file.get(
25+
'/',
26+
ratelimit({
27+
db: client,
28+
duration: 30 * 1000,
29+
max: 5,
30+
// throw: true,
31+
logging: true
32+
}),
33+
async ctx => {
34+
ctx.body = {
35+
msg: 'just a normal response.'
36+
};
37+
}
38+
);
39+
1740
module.exports = { file };

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"jest": "^24.3.1",
3434
"nodemon": "^1.18.10",
3535
"prettier": "1.16.4",
36+
"redis": "^2.8.0",
3637
"supertest": "^3.4.2"
3738
},
3839
"dependencies": {

0 commit comments

Comments
 (0)