Skip to content

Commit ab9986e

Browse files
committed
feat:redirect file
1 parent 7a86f23 commit ab9986e

4 files changed

Lines changed: 118 additions & 58 deletions

File tree

api-gateway/src/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import cookieParser from "cookie-parser";
33
import morgan from "morgan";
44
import { applySecurityMiddleware } from "./middleware/security";
55
import { routes } from "./routes";
6+
import { redirectDebugMiddleware } from "./redirect-debug";
67

78

89
const app:Application = express();
@@ -14,6 +15,7 @@ app.use(cookieParser())
1415

1516
applySecurityMiddleware(app);
1617

18+
app.use(redirectDebugMiddleware);
1719
routes(app);
1820

1921
export default app;

api-gateway/src/redirect-debug.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Request, Response } from "express";
2+
3+
export const redirectDebugMiddleware = (req: Request, res: Response, next: Function) => {
4+
// Only process this middleware for the debug route
5+
if (req.path !== '/debug/redirect-check') {
6+
return next();
7+
}
8+
9+
const requestInfo = {
10+
method: req.method,
11+
url: req.url,
12+
originalUrl: req.originalUrl,
13+
path: req.path,
14+
protocol: req.protocol,
15+
secure: req.secure,
16+
hostname: req.hostname,
17+
ip: req.ip,
18+
headers: req.headers,
19+
cookies: req.cookies,
20+
query: req.query,
21+
};
22+
23+
// Clean up sensitive information
24+
if (requestInfo.headers.authorization) {
25+
requestInfo.headers.authorization = '[REDACTED]';
26+
}
27+
if (requestInfo.headers.cookie) {
28+
requestInfo.headers.cookie = '[REDACTED]';
29+
}
30+
31+
res.status(200).json({
32+
message: "Redirect debugging information",
33+
requestInfo,
34+
serverInfo: {
35+
timestamp: new Date().toISOString(),
36+
nodeVersion: process.version,
37+
env: {
38+
NODE_ENV: process.env.NODE_ENV,
39+
}
40+
}
41+
});
42+
};

api-gateway/src/routes.ts

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Updated routes.ts file with improved logging and error handling
12
import { Application, Request, Response } from "express";
23
import proxy from "express-http-proxy";
34
import { Service } from "./config";
@@ -6,14 +7,14 @@ import { Service } from "./config";
67
const createMultipartProxy = (serviceUrl: string) => {
78
return proxy(serviceUrl, {
89
proxyReqPathResolver(req) {
9-
console.log(`Creating multipart proxy for ${serviceUrl}${req.url}`);
10+
console.log(`Multipart proxy: ${serviceUrl}${req.url}`);
1011
return "/multipart" + req.url;
1112
},
1213
parseReqBody: false,
1314
reqBodyEncoding: null,
1415
proxyErrorHandler: function(err, res, next) {
1516
console.error(`Multipart proxy error for ${serviceUrl}: ${err.message}`);
16-
res.status(500).json({ error: `Service temporarily unavailable: ${err.message}` });
17+
res.status(500).json({ error: `Service unavailable: ${err.message}` });
1718
}
1819
});
1920
};
@@ -22,66 +23,53 @@ const createMultipartProxy = (serviceUrl: string) => {
2223
const createRegularProxy = (serviceUrl: string) => {
2324
return proxy(serviceUrl, {
2425
proxyReqPathResolver(req) {
25-
console.log(`Proxying request to ${serviceUrl}${req.url}`);
26+
console.log(`Regular proxy: ${serviceUrl}${req.url}`);
2627
return req.url;
2728
},
2829
userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
2930
console.log(`Response from ${serviceUrl}${userReq.url}: Status ${proxyRes.statusCode}`);
30-
try {
31-
return proxyResData;
32-
} catch (e) {
33-
console.error('Error in response decorator:', e);
34-
return proxyResData;
35-
}
31+
// Don't modify headers that might cause redirects
32+
return proxyResData;
3633
},
3734
proxyErrorHandler: function(err, res, next) {
3835
console.error(`Proxy error for ${serviceUrl}: ${err.message}`);
39-
res.status(500).json({ error: `Service temporarily unavailable: ${err.message}` });
36+
res.status(500).json({ error: `Service unavailable: ${err.message}` });
4037
},
4138
timeout: 60000
4239
});
4340
};
4441

4542
export const routes = (app: Application) => {
46-
// Add a root health check endpoint
47-
app.get("/", (req: Request, res: Response) => {
48-
res.status(200).json({ status: "API Gateway is running", services: Object.keys(Service) });
43+
// Add more debugging for incoming requests
44+
app.use((req, res, next) => {
45+
console.log(`API Gateway received: ${req.method} ${req.url}`);
46+
console.log(`Headers: ${JSON.stringify(req.headers, null, 2)}`);
47+
next();
4948
});
50-
49+
5150
app.get("/health", (req: Request, res: Response) => {
52-
res.status(200).json({ status: "API Gateway is running" });
51+
res.status(200).json({ status: "api-gateway is running" });
5352
});
5453

5554
// Use multipart proxy only for file upload routes
5655
app.use("/auth/multipart", createMultipartProxy(Service.AUTH_SERVICE_URL));
5756
// Use regular proxy for other auth routes
5857
app.use("/auth", createRegularProxy(Service.AUTH_SERVICE_URL));
59-
58+
6059
app.use("/notification", createRegularProxy(Service.NOTIFICATION_SERVICE_URL));
61-
60+
6261
// Use multipart proxy only for file upload routes
6362
app.use("/course/multipart", createMultipartProxy(Service.COURSE_SERVICE_URL));
6463
// Use regular proxy for other course routes
6564
app.use("/course", createRegularProxy(Service.COURSE_SERVICE_URL));
66-
65+
6766
app.use("/payment", createRegularProxy(Service.PAYMENT_SERVICE_URL));
68-
67+
6968
app.use("/chat", createRegularProxy(Service.CHAT_SERVICE_URL));
7069

71-
// Improve 404 handling with more information
70+
// Catch-all route handler
7271
app.use("*", (req: Request, res: Response) => {
7372
console.log(`404 Not Found: ${req.method} ${req.originalUrl}`);
74-
res.status(404).json({
75-
error: "Route not found",
76-
path: req.originalUrl,
77-
availableRoutes: [
78-
"/health",
79-
"/auth/*",
80-
"/notification/*",
81-
"/course/*",
82-
"/payment/*",
83-
"/chat/*"
84-
]
85-
});
73+
res.status(404).json({ error: "route not found" });
8674
});
8775
};

manifest/ingress-controller.yaml

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,59 @@ kind: Ingress
33
metadata:
44
name: vlearning-ingress
55
annotations:
6-
nginx.ingress.kubernetes.io/enable-cors: "true"
7-
nginx.ingress.kubernetes.io/cors-allow-origin: "https://v-learning-client-5r8j.vercel.app"
8-
nginx.ingress.kubernetes.io/cors-allow-methods: "GET, PUT, POST, DELETE, PATCH, OPTIONS"
9-
nginx.ingress.kubernetes.io/cors-allow-headers: "Content-Type, Authorization, Cookie, Range, Accept"
10-
nginx.ingress.kubernetes.io/cors-expose-headers: "Content-Disposition, Content-Range, Accept-Ranges, Content-Length, Content-Type"
11-
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
12-
nginx.ingress.kubernetes.io/proxy-body-size: "50m" # Adjust if you need to handle large files
13-
nginx.ingress.kubernetes.io/proxy-connect-timeout: "60"
14-
nginx.ingress.kubernetes.io/proxy-read-timeout: "60"
15-
nginx.ingress.kubernetes.io/proxy-send-timeout: "60"
16-
cert-manager.io/cluster-issuer: letsencrypt-prod
6+
kubernetes.io/ingress.class: "nginx"
7+
nginx.ingress.kubernetes.io/ssl-redirect: "false" # Disable automatic SSL redirect
8+
nginx.ingress.kubernetes.io/rewrite-target: /$2 # Rewrite path to remove prefix
9+
nginx.ingress.kubernetes.io/use-regex: "true" # Enable regex in paths
1710
spec:
18-
ingressClassName: webapprouting.kubernetes.azure.com
19-
tls:
20-
- hosts:
21-
- welearning.online
22-
secretName: welearning-tls
2311
rules:
24-
- host: welearning.online
25-
http:
26-
paths:
27-
- path: /
28-
pathType: Prefix
29-
backend:
30-
service:
31-
name: vlearning-api-gateway
32-
port:
33-
number: 3000
12+
- host: welearning.online
13+
http:
14+
paths:
15+
- path: /auth(/|$)(.*)
16+
pathType: Prefix
17+
backend:
18+
service:
19+
name: vlearning-api-gateway
20+
port:
21+
number: 3000
22+
- path: /course(/|$)(.*)
23+
pathType: Prefix
24+
backend:
25+
service:
26+
name: vlearning-api-gateway
27+
port:
28+
number: 3000
29+
- path: /notification(/|$)(.*)
30+
pathType: Prefix
31+
backend:
32+
service:
33+
name: vlearning-api-gateway
34+
port:
35+
number: 3000
36+
- path: /payment(/|$)(.*)
37+
pathType: Prefix
38+
backend:
39+
service:
40+
name: vlearning-api-gateway
41+
port:
42+
number: 3000
43+
- path: /chat(/|$)(.*)
44+
pathType: Prefix
45+
backend:
46+
service:
47+
name: vlearning-api-gateway
48+
port:
49+
number: 3000
50+
- path: /(.*)
51+
pathType: Prefix
52+
backend:
53+
service:
54+
name: vlearning-frontend
55+
port:
56+
number: 80
57+
# If you're using HTTPS, add TLS configuration here
58+
# tls:
59+
# - hosts:
60+
# - welearning.online
61+
# secretName: welearning-tls

0 commit comments

Comments
 (0)