-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
121 lines (102 loc) · 3.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Require packages
const express = require("express");
const cors = require("cors");
const helmet = require("helmet");
const morgan = require("morgan");
const { createProxyMiddleware } = require("http-proxy-middleware");
// Create an instance of Express app
const app = express();
// Middleware setup
app.use(cors()); // Enable CORS
app.use(helmet()); // Add security headers
app.use(morgan("combined")); // Log HTTP requests
app.disable("x-powered-by"); // Hide Express server information
// Define routes and corresponding microservices
const services = [
{
route: "/auth",
target: "https://your-deployed-service.herokuapp.com/auth",
},
{
route: "/users",
target: "https://your-deployed-service.herokuapp.com/users/",
},
{
route: "/chats",
target: "https://your-deployed-service.herokuapp.com/chats/",
},
{
route: "/payment",
target: "https://your-deployed-service.herokuapp.com/payment/",
},
// Add more services as needed
];
// Define rate limit constants
const rateLimit = 20; // Max requests per minute
const interval = 60 * 1000; // Time window in milliseconds (1 minute)
// Object to store request counts for each IP address
const requestCounts = {};
// Reset request count for each IP address every 'interval' milliseconds
setInterval(() => {
Object.keys(requestCounts).forEach((ip) => {
requestCounts[ip] = 0; // Reset request count for each IP address
});
}, interval);
// Middleware function for rate limiting and timeout handling
function rateLimitAndTimeout(req, res, next) {
const ip = req.ip; // Get client IP address
// Update request count for the current IP
requestCounts[ip] = (requestCounts[ip] || 0) + 1;
// Check if request count exceeds the rate limit
if (requestCounts[ip] > rateLimit) {
// Respond with a 429 Too Many Requests status code
return res.status(429).json({
code: 429,
status: "Error",
message: "Rate limit exceeded.",
data: null,
});
}
// Set timeout for each request (example: 10 seconds)
req.setTimeout(15000, () => {
// Handle timeout error
res.status(504).json({
code: 504,
status: "Error",
message: "Gateway timeout.",
data: null,
});
req.abort(); // Abort the request
});
next(); // Continue to the next middleware
}
// Apply the rate limit and timeout middleware to the proxy
app.use(rateLimitAndTimeout);
// Set up proxy middleware for each microservice
services.forEach(({ route, target }) => {
// Proxy options
const proxyOptions = {
target,
changeOrigin: true,
pathRewrite: {
[`^${route}`]: "",
},
};
// Apply rate limiting and timeout middleware before proxying
app.use(route, rateLimitAndTimeout, createProxyMiddleware(proxyOptions));
});
// Handler for route-not-found
app.use((_req, res) => {
res.status(404).json({
code: 404,
status: "Error",
message: "Route not found.",
data: null,
});
});
// Define port for Express server
const PORT = process.env.PORT || 5000;
// Start Express server
app.listen(PORT, () => {
console.log(`Gateway is running on port ${PORT}`);
});