Skip to content

Commit

Permalink
feat: different rate-limits based on the location
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCassiere committed Jun 24, 2024
1 parent e513979 commit 1e6db82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ const limiter = rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100,
standardHeaders: "draft-6",
keyGenerator: (c) => c.req.header("x-app-service-id") ?? c.req.header("x-forwarded-for") ?? "",
keyGenerator: (c) => c.req.header("CF-Connecting-IP") ?? c.req.header("x-forwarded-for") ?? "",
});
app.use(limiter);

app.use("*", async (c, next) => {
c.set("service", null);
Expand All @@ -46,6 +45,8 @@ app.use("*", async (c, next) => {
app.use("/api/", timeout(5000));

app.route("/api/v2", v2Router);

app.use(limiter);
app.route("/docs", docsRouter);

app.get(
Expand Down
17 changes: 15 additions & 2 deletions src/routers/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { Hono } from "hono";
import { rateLimiter } from "hono-rate-limiter";

import type { ServerContext } from "@/types/hono";

import servicesRouter from "./services";
import logsRouter from "./logging";

import type { ServerContext } from "@/types/hono";

const app = new Hono<ServerContext>();

const limiter = rateLimiter({
windowMs: 5 * 60 * 1000, // 5 minutes
limit: 100,
standardHeaders: "draft-6",
keyGenerator: (c) => {
return (
c.req.header("x-app-service-id") ?? c.req.header("CF-Connecting-IP") ?? c.req.header("x-forwarded-for") ?? ""
);
},
});
app.use(limiter);

app.route("/service", servicesRouter);
app.route("/log", logsRouter);

Expand Down

0 comments on commit 1e6db82

Please sign in to comment.