index.ts98 lines, 2.5 KB
| 1 | import express from "express"; |
| 2 | import cors from "cors"; |
| 3 | import helmet from "helmet"; |
| 4 | import dotenv from "dotenv"; |
| 5 | import proxyRoutes from "./routes/proxy"; |
| 6 | import { gatewayAuth } from "./middleware/auth"; |
| 7 | import { createErrorResponse } from "../../shared/utils"; |
| 8 | |
| 9 | //load environment variables |
| 10 | dotenv.config(); |
| 11 | |
| 12 | const app = express(); |
| 13 | const PORT = process.env.PORT || 8080; |
| 14 | |
| 15 | // trust proxy (important for reverse proxies) |
| 16 | app.set("trust proxy", 1); |
| 17 | |
| 18 | // setup middlewares |
| 19 | app.use( |
| 20 | helmet({ |
| 21 | crossOriginEmbedderPolicy: false, |
| 22 | }) |
| 23 | ); |
| 24 | app.use( |
| 25 | cors({ |
| 26 | origin: process.env.CORS_ORIGIN || "http://localhost:3000", |
| 27 | credentials: process.env.CORS_CREDENTIALS === "true", |
| 28 | methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], |
| 29 | allowedHeaders: [ |
| 30 | "Content-Type", |
| 31 | "Authorization", |
| 32 | "x-user-id", |
| 33 | "x-user-email", |
| 34 | ], |
| 35 | }) |
| 36 | ); |
| 37 | |
| 38 | // parse JSON bodies |
| 39 | app.use(express.json({ limit: "10mb" })); |
| 40 | app.use(express.urlencoded({ extended: true })); |
| 41 | |
| 42 | // auth middlware |
| 43 | app.use(gatewayAuth); |
| 44 | |
| 45 | // setup proxy routes |
| 46 | app.use(proxyRoutes); |
| 47 | |
| 48 | //Gloval error handler |
| 49 | app.use( |
| 50 | ( |
| 51 | error: any, |
| 52 | req: express.Request, |
| 53 | res: express.Response, |
| 54 | next: express.NextFunction |
| 55 | ) => { |
| 56 | console.log("Unhandled eeror:", error); |
| 57 | |
| 58 | if (!res.headersSent) { |
| 59 | res |
| 60 | .status(error.statusCode || 500) |
| 61 | .json(createErrorResponse(error.message || "Internal eror")); |
| 62 | } |
| 63 | } |
| 64 | ); |
| 65 | |
| 66 | // Start server |
| 67 | const server = app.listen(PORT, () => { |
| 68 | console.log(`🚀 API Gateway running on port ${PORT}`); |
| 69 | console.log(`📊 Environment: ${process.env.NODE_ENV}`); |
| 70 | console.log(`🔗 Gateway URL: http://localhost:${PORT}`); |
| 71 | console.log(`❤️ Health check: http://localhost:${PORT}/health`); |
| 72 | console.log(""); |
| 73 | console.log("📋 Available endpoints:"); |
| 74 | console.log(` Auth Service: http://localhost:${PORT}/api/auth/*`); |
| 75 | console.log(` User Service: http://localhost:${PORT}/api/users/*`); |
| 76 | console.log(` Notes Service: http://localhost:${PORT}/api/notes/*`); |
| 77 | console.log(` Tags Service: http://localhost:${PORT}/api/tags/*`); |
| 78 | console.log(""); |
| 79 | }); |
| 80 | |
| 81 | // Graceful shutdown |
| 82 | process.on("SIGINT", () => { |
| 83 | console.log("🚦 Shutting down API Gateway..."); |
| 84 | server.close(() => { |
| 85 | console.log("✅ API Gateway shut down gracefully."); |
| 86 | process.exit(0); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | process.on("SIGTERM", () => { |
| 91 | console.log("🚦 Shutting down API Gateway..."); |
| 92 | server.close(() => { |
| 93 | console.log("✅ API Gateway shut down gracefully."); |
| 94 | process.exit(0); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | export default app; |