generated from VeeamCommunity/veeamcommunity-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (103 loc) · 3.89 KB
/
server.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// server.js
const express = require('express');
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const { Command } = require('commander');
const app = express();
app.use(express.json());
// Initialize Commander for CLI arguments
const program = new Command();
program
.option('-m, --mock', 'Run server in mock mode')
.parse(process.argv);
const options = program.opts();
const isMockMode = options.mock || process.env.MOCK === 'true';
if (isMockMode) {
console.log('Running in MOCK mode. Authentication and data fetching are mocked.');
} else {
console.log('Running in REAL mode. Authentication and data fetching are active.');
}
// Disable SSL certificate validation (for development only)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
// Define a POST route to handle logging
app.post('/logs', (req, res) => {
const { level, message, timestamp } = req.body;
console.log(`[${timestamp}] [${level.toUpperCase()}] ${message}`);
res.status(200).send('Log received');
});
// Endpoint to authenticate and fetch data
app.post('/fetch-data', async (req, res) => {
if (isMockMode) {
// Mock response
const mockToken = 'mocked_bearer_token';
// Read sample data from vms.json
const vmsPath = path.join(__dirname, 'public', 'data', 'vms.json');
let vmsData;
try {
const data = fs.readFileSync(vmsPath, 'utf-8');
vmsData = JSON.parse(data);
} catch (err) {
console.error('Error reading mock VMs data:', err);
return res.status(500).json({ error: 'Failed to load mock data' });
}
// Optionally, write to the file if needed
// fs.writeFileSync(vmsPath, JSON.stringify(vmsData));
// Respond with mock token
return res.json({ success: true, token: mockToken });
}
// REAL mode: Actual authentication and data fetching
const { serverUrl, username, password } = req.body;
try {
// Authenticate with VeeamONE API
const tokenResponse = await fetch(`${serverUrl}/api/token`, {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}&grant_type=password&refresh_token=`
});
if (!tokenResponse.ok) {
throw new Error(`Authentication failed: ${tokenResponse.statusText}`);
}
const tokenData = await tokenResponse.json();
const bearerToken = tokenData.access_token;
// Function to fetch data and save to a file
async function fetchDataAndSave(endpoint, filename) {
const response = await fetch(`${serverUrl}/api/v2.2${endpoint}`, {
method: 'GET',
headers: {
'accept': 'application/json',
'Authorization': `Bearer ${bearerToken}`
}
});
if (!response.ok) {
throw new Error(`Failed to fetch ${endpoint}: ${response.statusText}`);
}
const data = await response.json();
fs.writeFileSync(path.join(__dirname, 'public', 'data', filename), JSON.stringify(data, null, 2));
}
// Ensure the data directory exists
const dataDir = path.join(__dirname, 'public', 'data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
// Fetch data for each workload type
await Promise.all([
fetchDataAndSave('/protectedData/virtualMachines?Offset=0&Limit=1000', 'vms.json'),
// Add more endpoints if needed
]);
res.json({ success: true, token: bearerToken });
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: error.message });
}
});
// Serve static files from the public directory
app.use(express.static('public'));
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running in ${isMockMode ? 'MOCK' : 'REAL'} mode on http://localhost:${PORT}`);
});