forked from Chanzhaoyu/chatgpt-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 请求速率限制 * perf: 优化代码 --------- Co-authored-by: ChenZhaoYu <790348264@qq.com>
- Loading branch information
Showing
7 changed files
with
61 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { rateLimit } from 'express-rate-limit' | ||
import { isNotEmptyString } from '../utils/is' | ||
|
||
const MAX_REQUEST_PER_HOUR = process.env.MAX_REQUEST_PER_HOUR | ||
|
||
const maxCount = (isNotEmptyString(MAX_REQUEST_PER_HOUR) && !isNaN(Number(MAX_REQUEST_PER_HOUR))) | ||
? parseInt(MAX_REQUEST_PER_HOUR) | ||
: 0 // 0 means unlimited | ||
|
||
const limiter = rateLimit({ | ||
windowMs: 60 * 60 * 1000, // Maximum number of accesses within an hour | ||
max: maxCount, | ||
statusCode: 200, // 200 means success,but the message is 'Too many request from this IP in 1 hour' | ||
message: async (req, res) => { | ||
res.send({ status: 'Fail', message: 'Too many request from this IP in 1 hour', data: null }) | ||
}, | ||
}) | ||
|
||
export { limiter } |