diff --git a/src/index.ts b/src/index.ts index e900c50..fa6cc4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); @@ -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( diff --git a/src/routers/v2/index.ts b/src/routers/v2/index.ts index a4d53ca..c22b3fb 100644 --- a/src/routers/v2/index.ts +++ b/src/routers/v2/index.ts @@ -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(); +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);