Skip to content

Commit

Permalink
feat: add ratelimiter to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed Jul 21, 2024
1 parent 32ce946 commit 088ecf1
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 287 deletions.
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"debug": "^4.3.5",
"fastq": "^1.17.1",
"hono": "^4.5.1",
"hono-rate-limiter": "^0.4.0",
"jose": "^5.6.3",
"playwright-chromium": "^1.44.0",
"rss-parser": "^3.13.0",
Expand Down
14 changes: 14 additions & 0 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ import { Hono } from "hono"
import { logger } from "hono/logger"
import { format } from "@formkit/tempo"
import { prettyJSON } from "hono/pretty-json"
import { rateLimiter } from "hono-rate-limiter";
import { type HttpBindings, serve } from "@hono/node-server"

import { updateJob } from "./jobs/cron-update.js"
import bookmark from "./routes/bookmark/index.js"
import feed from "./routes/feed/index.js"
import root from "./routes/root.js"

const limiter = rateLimiter({
windowMs: 5 * 60 * 1000, // 5 minutes
limit: 5, // Limit each IP to 100 requests per `window`
standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
keyGenerator: (c) => {
const a = c.req.header('CF-Connecting-IP') || c.req.header('X-Forwarded-For') || c.req.header('origin') || '127.0.0.1' // Method to generate custom identifiers for clients.
console.log('clientIp', a)
return a
}
});

const app = new Hono<{ Bindings: HttpBindings }>({ strict: false }).basePath(
"/worker/v1",
)

app.use(logger())
app.use(prettyJSON())
app.use(limiter)

app.route("/bookmark", bookmark)
app.route("/feed", feed)
Expand Down
Loading

0 comments on commit 088ecf1

Please sign in to comment.