Skip to content

Commit

Permalink
chore: put caching before ratelimiting
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi-best committed Dec 5, 2024
1 parent 8bb8da1 commit 70f0578
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ const PORT = Number(Deno.env.get('PORT')) || 8000;
const app = new Hono();

/** MIDDLEWARES */

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

app.get(
'/v1/*',
cache({
cacheName: 'nsapi',
cacheControl: 'max-age=900',
wait: true,
})
);

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

Expand All @@ -27,7 +39,7 @@ if (Deno.env.get('NODE_ENV') === 'production') {

const limiter = rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 5, // Limit each IP to 50 requests per `window` (here, per 15 minutes).
limit: 100, // 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 }),
Expand All @@ -36,17 +48,6 @@ if (Deno.env.get('NODE_ENV') === 'production') {
app.use(limiter);
}

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

app.get(
'/v1/*',
cache({
cacheName: 'nsapi',
cacheControl: 'max-age=900',
wait: true,
})
);

/** ROUTES */
// Root route
app.get('/', (c) => {
Expand Down

0 comments on commit 70f0578

Please sign in to comment.