Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@fastify/helmet": "^12.0.0",
"@fastify/jwt": "^9.0.0",
"@fastify/multipart": "^9.0.0",
"@fastify/rate-limit": "^10.3.0",
"@fastify/static": "^8.0.0",
"@prisma/client": "^6.0.0",
"dotenv": "^16.4.0",
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jwt from '@fastify/jwt';
import cookie from '@fastify/cookie';
import multipart from '@fastify/multipart';
import fastifyStatic from '@fastify/static';
import rateLimit from '@fastify/rate-limit';
import path from 'path';
import { fileURLToPath } from 'url';

Expand Down Expand Up @@ -45,6 +46,10 @@ export async function buildApp() {

await app.register(cookie);
await app.register(multipart, { limits: { fileSize: 5 * 1024 * 1024 } }); // 5MB
await app.register(rateLimit, {
max: 100,
timeWindow: '1 minute',
});

// Static file serving for uploads
await app.register(fastifyStatic, {
Expand Down
36 changes: 32 additions & 4 deletions apps/backend/src/routes/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { generateQRBuffer, generateQRSvg } from '../utils/qr.js';
export async function publicRoutes(app: FastifyInstance) {
// ─── Public Profile ───

app.get('/:username', async (request: FastifyRequest<{ Params: { username: string } }>, reply: FastifyReply) => {
app.get('/:username', {
config: {
rateLimit: {
max: 100,
timeWindow: '1 minute'
}
}
}, async (request: FastifyRequest<{ Params: { username: string } }>, reply: FastifyReply) => {
const { username } = request.params;

const user = await app.prisma.user.findUnique({
Expand Down Expand Up @@ -71,7 +78,14 @@ export async function publicRoutes(app: FastifyInstance) {

// ─── Shared Card View (Direct) ───

app.get('/card/:cardId', async (request: FastifyRequest<{ Params: { cardId: string } }>, reply: FastifyReply) => {
app.get('/card/:cardId', {
config: {
rateLimit: {
max: 100,
timeWindow: '1 minute'
}
}
}, async (request: FastifyRequest<{ Params: { cardId: string } }>, reply: FastifyReply) => {
const { cardId } = request.params;

const card = await app.prisma.card.findUnique({
Expand Down Expand Up @@ -110,7 +124,14 @@ export async function publicRoutes(app: FastifyInstance) {

// ─── Public Card View ───

app.get('/:username/card/:cardId', async (request: FastifyRequest<{ Params: { username: string; cardId: string } }>, reply: FastifyReply) => {
app.get('/:username/card/:cardId', {
config: {
rateLimit: {
max: 100,
timeWindow: '1 minute'
}
}
}, async (request: FastifyRequest<{ Params: { username: string; cardId: string } }>, reply: FastifyReply) => {
const { username, cardId } = request.params;

const user = await app.prisma.user.findUnique({
Expand Down Expand Up @@ -182,7 +203,14 @@ export async function publicRoutes(app: FastifyInstance) {

// ─── QR Code Generation ───

app.get('/:username/qr', async (request: FastifyRequest<{
app.get('/:username/qr', {
config: {
rateLimit: {
max: 50, // Lower limit for QR generation as it's more resource intensive
timeWindow: '1 minute'
}
}
}, async (request: FastifyRequest<{
Params: { username: string };
Querystring: { format?: string; size?: string };
}>, reply: FastifyReply) => {
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

Loading