Skip to content

Commit

Permalink
feat: add ratelimiting
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi-best committed Dec 3, 2024
1 parent c5194ff commit f678243
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
DATABASE_URL=
GITHUB_TOKEN=
PORT=8000
PORT=8000
VITE_API_URL="http://localhost:8000/v1"
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ jobs:
name: Deploy API
runs-on: ubuntu-latest
env:
NODE_ENV: 'production'
DATABASE_URL: ${{ secrets.DATABASE_URL }}
GITHUB_TOKEN: ${{ secrets.API_GITHUB_TOKEN }}
UPSTASH_REDIS_REST_URL: ${{ secrets.UPSTASH_REDIS_REST_URL }}
UPSTASH_REDIS_REST_TOKEN: ${{ secrets.UPSTASH_REDIS_REST_TOKEN }}

permissions:
id-token: write # Needed for auth with Deno Deploy
Expand Down
5 changes: 4 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
"fe:build": "deno run -A --node-modules-dir npm:vite build",
"fe:preview": "deno run -A --node-modules-dir npm:vite preview",
"fe:serve": "deno run --allow-net --allow-read vite_server.ts",
"be:dev": "deno run --allow-all --watch --env-file server.ts",
"be:dev": "deno run --allow-all --watch --env-file main.ts",
"be:scrape": "deno run --allow-all --env-file utils/scrape/index.ts",
"be:deploy": "deployctl deploy --prod --env-file=.env --project=naijastars-api --exclude=./node_modules --exclude=./dist --exclude=./src --exclude=./.vite main.ts"
},
"compilerOptions": {
"lib": ["ES2020", "DOM", "DOM.Iterable"]
},
"nodeModulesDir": "auto",
"imports": {
"@b-fuze/deno-dom": "jsr:@b-fuze/deno-dom@^0.1.48",
"@deno/vite-plugin": "npm:@deno/vite-plugin@^1.0.1",
"@hono-rate-limiter/cloudflare": "npm:@hono-rate-limiter/cloudflare@^0.2.1",
"@hono-rate-limiter/redis": "npm:@hono-rate-limiter/redis@^0.1.4",
"@neon/serverless": "jsr:@neon/serverless@^0.10.4",
"@ptm/mm-mark": "jsr:@ptm/mm-mark@^0.2.22",
"@std/dotenv": "jsr:@std/dotenv@^0.225.2",
"@sveltejs/vite-plugin-svelte": "npm:@sveltejs/vite-plugin-svelte@^5.0.1",
"@upstash/redis": "npm:@upstash/redis@^1.34.3",
"autoprefixer": "npm:autoprefixer@^10.4.20",
"bits-ui": "npm:bits-ui@^0.21.16",
"clsx": "npm:clsx@^2.1.1",
Expand Down
27 changes: 27 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import { Hono } from 'npm:hono';
import { cors } from 'npm:hono/cors';
import { cache } from 'npm:hono/cache';
import { rateLimiter } from 'npm:hono-rate-limiter';
import { Redis } from '@upstash/redis';
import { RedisStore } from '@hono-rate-limiter/redis';

import RepositoryHandler from './routes/repository.ts';

Expand All @@ -12,6 +15,25 @@ const PORT = Number(Deno.env.get('PORT')) || 8000;
const app = new Hono();

/** MIDDLEWARES */
if (Deno.env.get('NODE_ENV') === 'production') {
console.log('Rate limiting enabled');

const redis = new Redis({
url: Deno.env.get('UPSTASH_REDIS_REST_URL'),
token: Deno.env.get('UPSTASH_REDIS_REST_TOKEN'),
});

const limiter = rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 5, // Limit each IP to 50 requests per `window` (here, per 15 minutes).
standardHeaders: 'draft-6', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
keyGenerator: (c) => c.req.header('cf-connecting-ip') ?? '', // Method to generate custom identifiers for clients.
store: new RedisStore({ client: redis }),
});

app.use(limiter);
}

app.use('/v1/*', cors());

app.get(
Expand Down

0 comments on commit f678243

Please sign in to comment.