mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
init
This commit is contained in:
31
workers/rate-limiter/index.ts
Normal file
31
workers/rate-limiter/index.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
interface Env {
|
||||
TEXT2IMAGE_RATE_LIMITER: any;
|
||||
}
|
||||
|
||||
export default {
|
||||
async fetch(request, env): Promise<Response> {
|
||||
const { pathname } = new URL(request.url);
|
||||
|
||||
const { success } = await env.TEXT2IMAGE_RATE_LIMITER.limit({
|
||||
key: pathname,
|
||||
});
|
||||
if (!success) {
|
||||
const svg = `
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 150" width="500" height="150">
|
||||
<rect width="100%" height="100%" fill="#f8d7da" />
|
||||
<text x="50%" y="50%" text-anchor="middle" dominant-baseline="middle" font-family="Arial, sans-serif" font-size="24" fill="#721c24">
|
||||
Sorry! Rate limit exceeded, try again in a couple minutes.
|
||||
</text>
|
||||
</svg>
|
||||
`;
|
||||
return new Response(svg, {
|
||||
status: 429,
|
||||
headers: {
|
||||
"Content-Type": "image/svg+xml",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(`Success!`);
|
||||
},
|
||||
} satisfies ExportedHandler<Env>;
|
22
workers/rate-limiter/wrangler-rate-limiter.toml
Normal file
22
workers/rate-limiter/wrangler-rate-limiter.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
name = "rate-limiter"
|
||||
main = "index.ts"
|
||||
compatibility_date = "2024-12-20"
|
||||
node_compat = true
|
||||
dev.port = 3003
|
||||
|
||||
routes = [
|
||||
{ pattern = "text2image.seemueller.io/generate*", zone_name = "seemueller.io" },
|
||||
]
|
||||
|
||||
# The rate limiting API is in open beta.
|
||||
[[unsafe.bindings]]
|
||||
name = "TEXT2IMAGE_RATE_LIMITER"
|
||||
type = "ratelimit"
|
||||
# An identifier you define, that is unique to your Cloudflare account.
|
||||
# Must be an integer.
|
||||
namespace_id = "1001"
|
||||
|
||||
# Limit: the number of requests allowed within a given period in a single
|
||||
# Cloudflare location
|
||||
# Period: the duration of the period, in seconds. Must be either 10 or 60
|
||||
simple = { limit = 26, period = 101}
|
Reference in New Issue
Block a user