Merge pull request #25 from JBB0807/deployment-prep
Proxy service performance improvement
This commit is contained in:
commit
2ef21157fd
2 changed files with 27 additions and 40 deletions
|
|
@ -60,29 +60,6 @@ auth.get("/current_user", (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// router.get("/google/login", (req, res) => {
|
|
||||||
// if (req.user) {
|
|
||||||
// console.log(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`);
|
|
||||||
// axios
|
|
||||||
// .post(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`, {
|
|
||||||
// user: req.user,
|
|
||||||
// })
|
|
||||||
// .then((response) => {
|
|
||||||
// req.user.userId = response.data.user.userid;
|
|
||||||
// console.log("User ID:", response.data.user.userid);
|
|
||||||
// req.user.role = "instructor";
|
|
||||||
// console.log("User registration response:", response.data);
|
|
||||||
// res.redirect(process.env.LOGIN_REDIRECT_URL);
|
|
||||||
// })
|
|
||||||
// .catch((error) => {
|
|
||||||
// console.error("Error registering user:", error.message);
|
|
||||||
// res.status(500).json({ error: true, message: "User login failed" });
|
|
||||||
// });
|
|
||||||
// } else {
|
|
||||||
// res.status(403).json({ error: true, message: "Not Authorized" });
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
auth.get("/login/failed", (req, res) => {
|
auth.get("/login/failed", (req, res) => {
|
||||||
res.status(401).json({
|
res.status(401).json({
|
||||||
error: true,
|
error: true,
|
||||||
|
|
|
||||||
|
|
@ -4,29 +4,39 @@ const { createProxyMiddleware } = require('http-proxy-middleware');
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 8080;
|
const port = 8080;
|
||||||
|
|
||||||
// Middleware to handle dynamic IPv6 proxying
|
// Cache proxy instances per target to avoid recreating each time
|
||||||
|
const proxyCache = new Map();
|
||||||
|
|
||||||
|
// Basic IPv6 validation pattern (can be improved or replaced with a library like 'ipaddr.js')
|
||||||
|
const isValidIPv6 = (ip) => /^[0-9a-fA-F:]+$/.test(ip);
|
||||||
|
|
||||||
app.use('/:ipv6', (req, res, next) => {
|
app.use('/:ipv6', (req, res, next) => {
|
||||||
const ipv6 = req.params.ipv6;
|
const ipv6 = req.params.ipv6;
|
||||||
|
|
||||||
// Validate or sanitize the IPv6 if needed
|
if (!isValidIPv6(ipv6)) {
|
||||||
|
return res.status(400).send('Invalid IPv6 address');
|
||||||
|
}
|
||||||
|
|
||||||
const targetUrl = `http://[${ipv6}]:8000`;
|
const targetUrl = `http://[${ipv6}]:8000`;
|
||||||
console.log(`Proxying request to: ${targetUrl}`);
|
console.log(`Proxying to: ${targetUrl}`);
|
||||||
|
|
||||||
// Create and attach the proxy middleware *once per request*
|
// Reuse proxy middleware for the same target
|
||||||
const proxy = createProxyMiddleware({
|
if (!proxyCache.has(targetUrl)) {
|
||||||
target: targetUrl,
|
proxyCache.set(targetUrl, createProxyMiddleware({
|
||||||
changeOrigin: true,
|
target: targetUrl,
|
||||||
logLevel: 'debug',
|
changeOrigin: true,
|
||||||
// pathRewrite: {
|
logLevel: 'debug',
|
||||||
// [`^/${ipv6}`]: '/', // Send to root of the target
|
pathRewrite: (path, req) => path.replace(`/${ipv6}`, '/'),
|
||||||
// },
|
onError(err, req, res) {
|
||||||
onError(err, req, res) {
|
console.error(`Proxy error for ${targetUrl}:`, err.message);
|
||||||
console.error('Proxy error:', err.message);
|
if (!res.headersSent) {
|
||||||
res.status(502).send('Bad Gateway: Failed to connect to target');
|
res.status(502).send('Bad Gateway: Failed to connect to target');
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
return proxy(req, res, next);
|
return proxyCache.get(targetUrl)(req, res, next);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, '0.0.0.0', () => {
|
app.listen(port, '0.0.0.0', () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue