-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (98 loc) · 3.33 KB
/
server.js
File metadata and controls
120 lines (98 loc) · 3.33 KB
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
120
require('dotenv').config();
const express = require('express');
const { Pool } = require('pg');
const cors = require('cors');
const rateLimit = require("express-rate-limit");
const redisClient = require('./redisClient');
const RedisStore = require('rate-limit-redis').default;
const app = express();
const PORT = process.env.PORT || 3000;
// PostgreSQL pool
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT
});
// Rate limiter for /news with redis
// This ensures rate limits work across multiple servers to scale later by Distributed Rate Limiting for Distributed Cache Pattern
const newsLimiter = rateLimit({
store: new RedisStore({
sendCommand: (...args) => redisClient.sendCommand(args),
}),
windowMs: 1 * 1000, //1 seconds
max: 10, //10 requests per window
standardHeaders: true,
legacyHeaders: false,
handler: (req, res) => {
res.status(429).json({ success: false, message: "Too many requests. Wait 30s." });
}
});
// Middleware
app.use(express.json());
app.use(cors());
app.use(express.static('public'));
// Apply to /news route
app.use("/news", newsLimiter);
// /news endpoint with filtering and pagination
app.get('/news', async (req, res) => {
try {
const { country = '', category = '', page = 1, pageSize = 20 } = req.query;
const pageNum = parseInt(page, 10) || 1;
const pageSizeNum = parseInt(pageSize, 10) || 20;
const cacheKey = `news${country}:${category}:${pageNum}:${pageSizeNum}`;
// Redis Check
const cachedData = await redisClient.get(cacheKey);
if(cachedData){
console.log(`Cache Hit: ${cacheKey}`);
return res.json(JSON.parse(cachedData));
}
console.log(`Cache Miss: ${cacheKey}`);
const offset = (pageNum - 1) * pageSizeNum;
// Build dynamic filters
const filters = [];
const values = [];
if (country) {
values.push(country);
filters.push(`source_country = $${values.length}`);
}
if (category) {
values.push(category);
filters.push(`news_type = $${values.length}`);
}
let query = 'SELECT * FROM news WHERE 1=1';
if (filters.length > 0) {
query += ' AND ' + filters.join(' AND ');
}
// Append ORDER BY and hardcode LIMIT/OFFSET for performance
query += ` ORDER BY published_at DESC LIMIT ${pageSizeNum} OFFSET ${offset}`;
const result = await pool.query(query, values);
// Count total rows for pagination (same filter logic)
let countQuery = 'SELECT COUNT(*) FROM news WHERE 1=1';
if (filters.length > 0) {
countQuery += ' AND ' + filters.join(' AND ');
}
const countResult = await pool.query(countQuery, values);
const totalCount = parseInt(countResult.rows[0].count, 10);
const totalPages = Math.ceil(totalCount / pageSizeNum);
const response = {
success: true,
page: pageNum,
pageSize: pageSizeNum,
totalCount,
totalPages,
data: result.rows
};
// cache the response page
await redisClient.set(cacheKey,JSON.stringify(response), {EX: 3600});
res.json(response);
} catch (err) {
console.error(err);
res.status(500).json({ success: false, error: err.message });
}
});
// Start server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});