-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (56 loc) · 2.24 KB
/
server.js
File metadata and controls
65 lines (56 loc) · 2.24 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
// 1. Import necessary tools
const express = require('express');
const fetch = require('node-fetch');
const cors = require('cors');
// 2. Setup the Express app
const app = express();
const port = 3000;
const API_DOMAIN = 'https://api.muapi.ai/api/v1';
// 3. Apply Middleware
app.use(cors());
app.use(express.json());
// 4. Securely get the new API key from an environment variable
// IMPORTANT: The key name has changed to match your script.
const apiKey = process.env.MUAPIAPP_API_KEY;
// 5. Endpoint for submitting the video generation task to the new API
app.post('/generate', async (req, res) => {
if (!apiKey) return res.status(500).json({ error: 'Server is missing MUAPIAPP_API_KEY.' });
// Get the new parameters from the frontend
const { prompt, resolution, aspect_ratio } = req.body;
if (!prompt) return res.status(400).json({ error: 'No prompt provided.' });
console.log(`Received new prompt: "${prompt}"`);
try {
const apiResponse = await fetch(`${API_DOMAIN}/openai-sora-2-text-to-video`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey // Use the new header key
},
body: JSON.stringify({ prompt, resolution, aspect_ratio })
});
const data = await apiResponse.json();
res.status(apiResponse.status).json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 6. Endpoint for polling the result from the new API
app.get('/task/:requestId', async (req, res) => {
if (!apiKey) return res.status(500).json({ error: 'Server is missing MUAPIAPP_API_KEY.' });
const { requestId } = req.params;
console.log(`Checking status for request_id: ${requestId}`);
try {
const apiResponse = await fetch(`${API_DOMAIN}/predictions/${requestId}/result`, {
method: 'GET',
headers: { 'x-api-key': apiKey }
});
const data = await apiResponse.json();
res.status(apiResponse.status).json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 7. Start the server
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});