-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-gateway.js
More file actions
139 lines (117 loc) · 3.12 KB
/
Copy pathapi-gateway.js
File metadata and controls
139 lines (117 loc) · 3.12 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Example: API Gateway with RailJS
* Demonstrates request/response pattern with multiple services
*/
import { Rail } from '../rail.js';
const rail = new Rail({ debug: true });
// API Gateway Module
const apiGatewayModule = {
name: 'api-gateway',
connect(rail) {
// Simulate HTTP request
rail.on('http.request', async (data) => {
const { method, path, body } = data;
console.log(`📥 ${method} ${path}`);
// Route to appropriate service
if (path.startsWith('/users')) {
rail.emit('service.users.request', { method, path, body });
} else if (path.startsWith('/products')) {
rail.emit('service.products.request', { method, path, body });
} else {
rail.emit('http.response', { status: 404, body: 'Not Found' });
}
}, 'api-gateway');
}
};
// User Service Module
const userServiceModule = {
name: 'user-service',
users: new Map([
[1, { id: 1, name: 'Alice', email: 'alice@example.com' }],
[2, { id: 2, name: 'Bob', email: 'bob@example.com' }]
]),
connect(rail) {
rail.on('service.users.request', (data) => {
const { method, path } = data;
if (method === 'GET' && path === '/users') {
rail.emit('http.response', {
status: 200,
body: Array.from(this.users.values())
});
} else if (method === 'GET' && path.match(/\/users\/\d+/)) {
const id = parseInt(path.split('/')[2]);
const user = this.users.get(id);
if (user) {
rail.emit('http.response', { status: 200, body: user });
} else {
rail.emit('http.response', { status: 404, body: 'User not found' });
}
}
}, 'user-service');
}
};
// Analytics Module
const analyticsModule = {
name: 'analytics',
requests: [],
connect(rail) {
rail.on('http.request', (data) => {
this.requests.push({
...data,
timestamp: new Date()
});
console.log(`📊 Total requests: ${this.requests.length}`);
}, 'analytics');
}
};
// Rate Limiter Module
const rateLimiterModule = {
name: 'rate-limiter',
limits: new Map(),
connect(rail) {
rail.on('http.request', (data) => {
const ip = data.ip || 'unknown';
const now = Date.now();
if (!this.limits.has(ip)) {
this.limits.set(ip, []);
}
const requests = this.limits.get(ip).filter(t => now - t < 60000);
requests.push(now);
this.limits.set(ip, requests);
if (requests.length > 100) {
console.warn(`⚠️ Rate limit exceeded for ${ip}`);
rail.emit('rate-limit.exceeded', { ip, requests: requests.length });
}
}, 'rate-limiter');
}
};
// Attach all modules
rail.attach(apiGatewayModule);
rail.attach(userServiceModule);
rail.attach(analyticsModule);
rail.attach(rateLimiterModule);
// Listen for responses
rail.on('http.response', (data) => {
console.log(`📤 ${data.status}:`, data.body);
}, 'response-logger');
// Simulate API requests
console.log('\n=== Simulating API Gateway ===\n');
rail.emit('http.request', {
method: 'GET',
path: '/users',
ip: '192.168.1.1'
});
setTimeout(() => {
rail.emit('http.request', {
method: 'GET',
path: '/users/1',
ip: '192.168.1.1'
});
}, 100);
setTimeout(() => {
rail.emit('http.request', {
method: 'GET',
path: '/users/999',
ip: '192.168.1.1'
});
}, 200);