-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (82 loc) · 2.91 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
import express from "express";
import { logger } from "./middlewares/logger.js";
import compression from "compression";
import { sendRequestToServer } from "./utils/requestHandler.js";
import { cacheServerResponse, isCached } from "./utils/cache.js";
import cron from "node-cron";
import { SERVERS } from "./config.js";
import { rateLimit } from "express-rate-limit";
// Initialize available servers array and current server index for round-robin load balancing
let availableServers = [];
let currentServer = 0; // Server Index, ranges from 0 to 2
const app = express();
// Set up rate limiter middleware
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15-minute window
limit: 100, // Allow max 100 requests per 15 minutes
message: "You have reached your rate limit! Please try again after some time",
standardHeaders: "draft-6",
skipFailedRequests: true,
});
app.use(limiter);
app.use(compression()); // Enable GZIP compression for responses
app.use(express.json()); // Parse incoming JSON requests
app.use(logger);
// Main route handler for all incoming requests
app.use("*", async (request, response) => {
const url = request.originalUrl;
const method = request.method;
if (availableServers.length === 0) {
return response.status(503).send("No servers available");
}
// Select target server based on round-robin and update current server index
const targetURL = SERVERS[currentServer] + url;
currentServer = (currentServer + 1) % availableServers.length;
const { isResponseCached, responseBody } = isCached(method, url);
if (isResponseCached) {
response.setHeader("X-Cache", "HIT");
return response.status(200).send(responseBody);
}
// Attempt to send the request to the selected server and handle response
try {
const serverResponse = await sendRequestToServer(
targetURL,
method,
request.headers,
request.body
);
const responseBody = await serverResponse.json();
if (serverResponse.ok && method === "GET") {
cacheServerResponse(url, responseBody);
}
// Sends Response back to the client
return response.status(serverResponse.status).send(responseBody);
} catch (error) {
console.log(error);
return response.sendStatus(500);
}
});
// Function to ping servers and update the available servers list
async function pingServers() {
const servers = [];
for await (const server of SERVERS) {
try {
const response = await fetch(`${server}/health`);
if (response.ok) {
servers.push(server);
console.log(`Yes: ${server}`);
}
} catch (error) {
console.log(`No: ${server}`);
}
}
availableServers = servers;
}
// Schedule server health checks every 30 seconds to update available servers list
cron.schedule("*/30 * * * * *", async () => {
await pingServers();
});
app.listen(3000, async () => {
await pingServers();
console.log("Server is running on http://localhost:3000");
});