mirror of
https://github.com/fmhy/edit.git
synced 2025-07-30 07:42:18 +10:00
feat(api): ratelimiting
This commit is contained in:
parent
26213d9f91
commit
1148023b1a
10 changed files with 139 additions and 150 deletions
17
api/middleware/ratelimit.ts
Normal file
17
api/middleware/ratelimit.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
export default defineEventHandler(async (event) => {
|
||||
const { cloudflare } = event.context
|
||||
|
||||
// FIXME: THIS IS NOT RECOMMENDED. BUT I WILL USE IT FOR NOW
|
||||
// Not recommended: many users may share a single IP, especially on mobile networks
|
||||
// or when using privacy-enabling proxies
|
||||
const ipAddress = getHeader(event, 'CF-Connecting-IP') ?? ''
|
||||
|
||||
const { success } = await // KILL YOURSELF
|
||||
(cloudflare.env as unknown as Env).RATE_LIMITER.limit({
|
||||
key: ipAddress
|
||||
})
|
||||
|
||||
if (!success) {
|
||||
throw createError('Failure – global rate limit exceeded')
|
||||
}
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue