Loading...
Loading...
Compare original and translation side by side
undefinedundefinedundefinedundefinedundefinedundefined# Enable gzip compression
gzip on;
gzip_types application/json;
gzip_min_length 1000;
# User Service Routes
location /api/users {
limit_req zone=api_limit burst=20 nodelay;
# Authentication check
access_by_lua_block {
local token = ngx.var.http_authorization
if not token then
return ngx.HTTP_UNAUTHORIZED
end
}
proxy_pass http://user_service;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Request timeout
proxy_connect_timeout 10s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
# Product Service Routes
location /api/products {
limit_req zone=api_limit burst=50 nodelay;
proxy_pass http://product_service;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Caching
proxy_cache api_cache;
proxy_cache_valid 200 1m;
proxy_cache_key "$scheme$request_method$host$request_uri";
}
# Order Service Routes (requires auth)
location /api/orders {
limit_req zone=user_limit burst=10 nodelay;
auth_request /auth;
auth_request_set $auth_user $upstream_http_x_user_id;
proxy_pass http://order_service;
proxy_http_version 1.1;
proxy_set_header X-User-ID $auth_user;
}
# Health check endpoint
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
# Metrics endpoint
location /metrics {
stub_status on;
access_log off;
}# Enable gzip compression
gzip on;
gzip_types application/json;
gzip_min_length 1000;
# User Service Routes
location /api/users {
limit_req zone=api_limit burst=20 nodelay;
# Authentication check
access_by_lua_block {
local token = ngx.var.http_authorization
if not token then
return ngx.HTTP_UNAUTHORIZED
end
}
proxy_pass http://user_service;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Request timeout
proxy_connect_timeout 10s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
# Product Service Routes
location /api/products {
limit_req zone=api_limit burst=50 nodelay;
proxy_pass http://product_service;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Caching
proxy_cache api_cache;
proxy_cache_valid 200 1m;
proxy_cache_key "$scheme$request_method$host$request_uri";
}
# Order Service Routes (requires auth)
location /api/orders {
limit_req zone=user_limit burst=10 nodelay;
auth_request /auth;
auth_request_set $auth_user $upstream_http_x_user_id;
proxy_pass http://order_service;
proxy_http_version 1.1;
proxy_set_header X-User-ID $auth_user;
}
# Health check endpoint
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
# Metrics endpoint
location /metrics {
stub_status on;
access_log off;
}undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined/api/users/api/productsundefined/api/users/api/productsundefinedconst express = require('express');
const httpProxy = require('express-http-proxy');
const rateLimit = require('express-rate-limit');
const jwt = require('jsonwebtoken');
const app = express();
// Rate limiting
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 100
});
// JWT verification
const verifyJwt = (req, res, next) => {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'No token' });
try {
jwt.verify(token, process.env.JWT_SECRET);
next();
} catch (err) {
res.status(403).json({ error: 'Invalid token' });
}
};
// Request logging
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} ${req.method} ${req.path}`);
next();
});
app.use(limiter);
// User service proxy
app.use('/api/users', verifyJwt, httpProxy('http://user-service:3000', {
proxyReqPathResolver: (req) => `/api/users${req.url}`,
userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
proxyRes.headers['X-Gateway'] = 'true';
return proxyResData;
}
}));
// Product service proxy
app.use('/api/products', httpProxy('http://product-service:3001', {
proxyReqPathResolver: (req) => `/api/products${req.url}`
}));
// Health check
app.get('/health', (req, res) => res.json({ status: 'ok' }));
app.listen(8080, () => console.log('Gateway on port 8080'));const express = require('express');
const httpProxy = require('express-http-proxy');
const rateLimit = require('express-rate-limit');
const jwt = require('jsonwebtoken');
const app = express();
// Rate limiting
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 100
});
// JWT verification
const verifyJwt = (req, res, next) => {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'No token' });
try {
jwt.verify(token, process.env.JWT_SECRET);
next();
} catch (err) {
res.status(403).json({ error: 'Invalid token' });
}
};
// Request logging
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} ${req.method} ${req.path}`);
next();
});
app.use(limiter);
// User service proxy
app.use('/api/users', verifyJwt, httpProxy('http://user-service:3000', {
proxyReqPathResolver: (req) => `/api/users${req.url}`,
userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
proxyRes.headers['X-Gateway'] = 'true';
return proxyResData;
}
}));
// Product service proxy
app.use('/api/products', httpProxy('http://product-service:3001', {
proxyReqPathResolver: (req) => `/api/products${req.url}`
}));
// Health check
app.get('/health', (req, res) => res.json({ status: 'ok' }));
app.listen(8080, () => console.log('Gateway on port 8080'));// Prometheus metrics
const promClient = require('prom-client');
const httpRequestDuration = new promClient.Histogram({
name: 'gateway_http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'route', 'status_code']
});
const httpRequests = new promClient.Counter({
name: 'gateway_http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'route', 'status_code']
});
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const duration = (Date.now() - start) / 1000;
httpRequestDuration.labels(req.method, req.path, res.statusCode).observe(duration);
httpRequests.labels(req.method, req.path, res.statusCode).inc();
});
next();
});// Prometheus metrics
const promClient = require('prom-client');
const httpRequestDuration = new promClient.Histogram({
name: 'gateway_http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'route', 'status_code']
});
const httpRequests = new promClient.Counter({
name: 'gateway_http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'route', 'status_code']
});
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const duration = (Date.now() - start) / 1000;
httpRequestDuration.labels(req.method, req.path, res.statusCode).observe(duration);
httpRequests.labels(req.method, req.path, res.statusCode).inc();
});
next();
});