|
| 1 | +import dotenv from "dotenv"; |
| 2 | +import express from "express"; |
| 3 | +import type { Request, Response } from "express"; |
| 4 | +import * as trpcExpress from "@trpc/server/adapters/express"; |
| 5 | +import { appRouter } from "./routers/_app.js"; |
| 6 | +import { createContext } from "./context.js"; |
| 7 | +import prismaModule from "./prisma.js"; |
| 8 | +import cors from "cors"; |
| 9 | +import type { CorsOptions as CorsOptionsType } from "cors"; |
| 10 | +import rateLimit from "express-rate-limit"; |
| 11 | +import helmet from "helmet"; |
| 12 | +import ipBlocker from "./middleware/ipBlock.js"; |
| 13 | + |
| 14 | +dotenv.config(); |
| 15 | + |
| 16 | +const app = express(); |
| 17 | +const PORT = process.env.PORT || 4000; |
| 18 | +const CORS_ORIGINS = process.env.CORS_ORIGINS |
| 19 | + ? process.env.CORS_ORIGINS.split(",") |
| 20 | + : ["http://localhost:3000", "http://localhost:5000"]; |
| 21 | + |
| 22 | +// Security headers |
| 23 | +app.use(helmet()); |
| 24 | +app.use( |
| 25 | + helmet.contentSecurityPolicy({ |
| 26 | + directives: { |
| 27 | + defaultSrc: ["'self'"], |
| 28 | + scriptSrc: ["'self'", "'unsafe-inline'"], |
| 29 | + styleSrc: ["'self'", "'unsafe-inline'"], |
| 30 | + imgSrc: ["'self'", "data:", "https:"], |
| 31 | + }, |
| 32 | + }) |
| 33 | +); |
| 34 | + |
| 35 | +// Apply IP blocking middleware first |
| 36 | +app.use(ipBlocker.middleware); |
| 37 | + |
| 38 | +// Different rate limits for different endpoints |
| 39 | +const authLimiter = rateLimit({ |
| 40 | + windowMs: 15 * 60 * 1000, |
| 41 | + max: 5, |
| 42 | + message: "Too many login attempts, please try again later", |
| 43 | + standardHeaders: true, |
| 44 | + legacyHeaders: false, |
| 45 | +}); |
| 46 | + |
| 47 | +const apiLimiter = rateLimit({ |
| 48 | + windowMs: 15 * 60 * 1000, |
| 49 | + max: 30, |
| 50 | + message: "Too many requests from this IP", |
| 51 | + standardHeaders: true, |
| 52 | + legacyHeaders: false, |
| 53 | +}); |
| 54 | + |
| 55 | +// Request size limits |
| 56 | +app.use(express.json({ limit: "10kb" })); |
| 57 | +app.use(express.urlencoded({ limit: "10kb", extended: true })); |
| 58 | + |
| 59 | +// CORS configuration |
| 60 | +const corsOptions: CorsOptionsType = { |
| 61 | + origin: (origin, callback) => { |
| 62 | + if (!origin || CORS_ORIGINS.includes(origin)) { |
| 63 | + callback(null, origin); |
| 64 | + } else { |
| 65 | + callback(new Error("Not allowed by CORS")); |
| 66 | + } |
| 67 | + }, |
| 68 | + methods: ["GET", "POST"], |
| 69 | + allowedHeaders: ["Content-Type", "Authorization"], |
| 70 | + credentials: true, |
| 71 | + maxAge: 86400, // 24 hours |
| 72 | +}; |
| 73 | + |
| 74 | +app.use(cors(corsOptions)); |
| 75 | + |
| 76 | +// Blocked IPs endpoint (admin endpoint) |
| 77 | +app.get("/admin/blocked-ips", (req: Request, res: Response) => { |
| 78 | + const blockedIPs = ipBlocker.getBlockedIPs(); |
| 79 | + res.json({ |
| 80 | + blockedIPs: blockedIPs.map((ip) => ({ |
| 81 | + ...ip, |
| 82 | + blockedUntil: new Date(ip.blockedUntil).toISOString(), |
| 83 | + })), |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +// Test endpoint |
| 88 | +app.get("/test", apiLimiter, (req: Request, res: Response) => { |
| 89 | + res.status(200).json({ status: "ok", message: "Test endpoint is working" }); |
| 90 | +}); |
| 91 | + |
| 92 | +// Connect to database |
| 93 | +prismaModule.connectDB(); |
| 94 | + |
| 95 | +// Apply rate limiting to tRPC endpoints |
| 96 | +app.use("/trpc", apiLimiter); |
| 97 | + |
| 98 | +// tRPC middleware |
| 99 | +app.use( |
| 100 | + "/trpc", |
| 101 | + trpcExpress.createExpressMiddleware({ |
| 102 | + router: appRouter, |
| 103 | + createContext, |
| 104 | + }) |
| 105 | +); |
| 106 | + |
| 107 | +// Global error handling |
| 108 | +app.use((err: Error, req: Request, res: Response, next: Function) => { |
| 109 | + console.error(err.stack); |
| 110 | + res.status(500).json({ |
| 111 | + error: "Internal Server Error", |
| 112 | + message: process.env.NODE_ENV === "development" ? err.message : undefined, |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +app.listen(PORT, () => { |
| 117 | + console.log(`tRPC server running on http://localhost:${PORT}`); |
| 118 | +}); |
0 commit comments