-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment.js
More file actions
78 lines (65 loc) · 2.31 KB
/
deployment.js
File metadata and controls
78 lines (65 loc) · 2.31 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
const express = require('express');
const multer = require('multer');
const axios = require('axios');
const path = require('path');
const FormData = require('form-data');
const cors = require('cors');
const jwt = require('jsonwebtoken');
const jwksClient = require('jwks-rsa');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
// Multer setup for file uploads
const storage = multer.memoryStorage();
const upload = multer({ storage });
// Cognito details
const REGION = 'eu-north-1';
const USER_POOL_ID = 'eu-north-1_eBjV6MuY2';
const JWKS_URI = `https://cognito-idp.${REGION}.amazonaws.com/${USER_POOL_ID}/.well-known/jwks.json`;
// Set up jwks client for JWT verification
const client = jwksClient({
jwksUri: JWKS_URI,
});
function getKey(header, callback) {
client.getSigningKey(header.kid, (err, key) => {
const signingKey = key.getPublicKey();
callback(null, signingKey);
});
}
// Middleware to check JWT
function checkAuth(req, res, next) {
const authHeader = req.headers['authorization'];
if (!authHeader) {
return res.status(401).json({ error: 'No credentials sent!' });
}
const token = authHeader.split(' ')[1];
jwt.verify(token, getKey, { algorithms: ['RS256'] }, (err, decoded) => {
if (err) {
return res.status(403).json({ error: 'Invalid token' });
}
req.user = decoded;
next();
});
}
app.post('/deploy', checkAuth, upload.single('file'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
const formData = new FormData();
formData.append('deployment-name', 'Local BPMN Deployment');
formData.append('file', req.file.buffer, req.file.originalname);
const response = await axios.post('http://localhost:8080/engine-rest/deployment/create', formData, {
headers: formData.getHeaders(),
});
res.status(response.status).json(response.data);
} catch (error) {
console.error(error.response?.data || error.message);
res.status(500).json({ error: error.message });
}
});
module.exports = app;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});