-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.cjs
71 lines (61 loc) · 1.94 KB
/
middleware.cjs
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
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const dotenv = require('dotenv');
const path = require('path');
dotenv.config();
const app = express();
const distPath = path.join(__dirname, 'dist');
const defaultJiraBaseUrl = process.env.VITE_JIRA_BASE_URL || 'https://your-jira-instance.atlassian.net';
const PORT = process.env.VITE_MIDDLEWARE_PORT || 5000;
let jiraBaseUrl = defaultJiraBaseUrl;
if (!jiraBaseUrl) {
console.error('Error: JIRA Base URL is undefined. Please check your configuration.');
process.exit(1);
}
// Serve React build files
app.use(express.static(distPath));
// Update JIRA Base URL dynamically
app.post('/update-config', express.json(), (req, res) => {
const { jiraBaseUrl: newUrl } = req.body;
if (newUrl) {
jiraBaseUrl = newUrl;
console.log(`Updated JIRA Base URL to: ${jiraBaseUrl}`);
res.status(200).json({ message: 'Configuration updated successfully!' });
} else {
res.status(400).json({ error: 'Invalid JIRA Base URL' });
}
});
function userDefinedRouter(req) {
return jiraBaseUrl;
}
// Proxy middleware for API calls
app.use(
'/api/jira',
(req, res, next) => {
if (!jiraBaseUrl) {
return res.status(500).json({ error: 'JIRA Base URL is not set' });
}
next();
},
createProxyMiddleware({
target: jiraBaseUrl,
router: userDefinedRouter,
secure: false,
changeOrigin: true,
followRedirects: true,
headers: {
'X-Atlassian-Token': 'no-check',
origin: `http://localhost:${PORT}`,
'User-Agent': 'test'
},
pathRewrite: { '^/api/jira': '' },
})
);
// Serve index.html for other routes
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
// Start server
app.listen(PORT, () => {
console.log(`Middleware running on http://localhost:${PORT}`);
});