-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (73 loc) · 2.79 KB
/
server.js
File metadata and controls
90 lines (73 loc) · 2.79 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
79
80
81
82
83
84
85
86
87
88
89
90
const express = require('express');
const path = require('path');
const axios = require('axios');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// Serve the HTML file at the root endpoint
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// API endpoint to get embed app URL
app.post('/api/embed-app-url', async (req, res) => {
try {
const { email, appId, sessionExpiry, patExpiry, tooljetServerUrl, tooljetAuthToken } = req.body;
// Validate required fields
if (!email || !appId) {
return res.status(400).json({
success: false,
error: 400,
message: 'Email and App ID are required'
});
}
console.log('Calling ToolJet embed API with:', { email, appId, sessionExpiry, patExpiry });
// Build request body with required and optional parameters
const requestBody = {
email,
appId
};
// Add optional parameters if provided
if (sessionExpiry !== undefined && sessionExpiry !== null && sessionExpiry !== '') {
requestBody.sessionExpiry = parseInt(sessionExpiry);
}
if (patExpiry !== undefined && patExpiry !== null && patExpiry !== '') {
requestBody.patExpiry = parseInt(patExpiry);
}
const url = `${tooljetServerUrl}/api/ext/users/personal-access-token` || process.env.TOOLJET_EMBED_APP_URL;
const response = await axios.post(url, requestBody, {
headers: {
'Authorization': `Basic ${tooljetAuthToken || process.env.TOOLJET_AUTH_TOKEN}`,
'Content-Type': 'application/json'
}
});
console.log('ToolJet API response:', response.data);
// Return only the redirectUrl to the frontend
res.json({
success: true,
redirectUrl: response.data.redirectUrl
});
} catch (error) {
console.error('Error calling ToolJet API:', error.response?.data || error.message);
res.status(500).json({
success: false,
error: error.response?.status || 500,
message: error.response?.data?.message || error.message || 'Failed to get embed URL'
});
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Server error:', err);
res.status(500).json({
success: false,
error: 500,
message: 'Internal server error'
});
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Access the app at: http://localhost:${PORT}`);
});