-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.js
More file actions
110 lines (95 loc) · 3.16 KB
/
proxy.js
File metadata and controls
110 lines (95 loc) · 3.16 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
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const compression = require('compression');
const app = express();
const PORT = process.env.PROXY_PORT || 4005;
// Cache for API responses (simple in-memory cache)
const cache = new Map();
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
// Middleware
app.use(compression());
app.use(cors({
origin: process.env.NODE_ENV === 'production'
? ['https://your-domain.com']
: ['http://localhost:5173', 'http://localhost:3000'],
credentials: true
}));
// Helper function to get cached data
const getCachedData = (key) => {
const cached = cache.get(key);
if (cached && Date.now() - cached.timestamp < CACHE_DURATION) {
return cached.data;
}
cache.delete(key);
return null;
};
// Helper function to set cache
const setCachedData = (key, data) => {
cache.set(key, { data, timestamp: Date.now() });
};
app.get('/api/mlbb-meta', async (req, res) => {
const cacheKey = 'mlbb-meta';
try {
// Check cache first
const cachedData = getCachedData(cacheKey);
if (cachedData) {
return res.json(cachedData);
}
const url = 'https://mapi.mobilelegends.com/legends/area?dateType=week&area=all&module=all&moduleType=all&language=en';
const response = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Referer': 'https://m.mobilelegends.com/rank',
'Origin': 'https://m.mobilelegends.com',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9'
},
timeout: 10000 // 10 second timeout
});
const data = response.data.data;
setCachedData(cacheKey, data);
res.json(data);
} catch (err) {
console.error('MLBB Meta API Error:', err.message);
res.status(500).json({
error: 'Failed to fetch MLBB meta data',
message: err.message,
timestamp: new Date().toISOString()
});
}
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'ok',
service: 'MLBB Meta Proxy',
timestamp: new Date().toISOString(),
cache_size: cache.size
});
});
// Clear cache endpoint (for development)
app.post('/api/clear-cache', (req, res) => {
cache.clear();
res.json({ message: 'Cache cleared successfully' });
});
app.listen(PORT, () => {
console.log(`
╔════════════════════════════════════════════╗
║ MLBB Meta Proxy is running! 🚀 ║
║ http://localhost:${PORT}/api/mlbb-meta ║
║ Cache Duration: ${CACHE_DURATION/1000/60} minutes ║
╚════════════════════════════════════════════╝
`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down gracefully...');
cache.clear();
process.exit(0);
});
process.on('SIGINT', () => {
console.log('Received SIGINT, shutting down gracefully...');
cache.clear();
process.exit(0);
});