Spaces:
Sleeping
Sleeping
Update app/server.js
Browse files- app/server.js +13 -35
app/server.js
CHANGED
|
@@ -43,6 +43,8 @@
|
|
| 43 |
// });
|
| 44 |
|
| 45 |
// module.exports = router;
|
|
|
|
|
|
|
| 46 |
const express = require('express');
|
| 47 |
const multer = require('multer');
|
| 48 |
const {
|
|
@@ -56,30 +58,25 @@ const app = express();
|
|
| 56 |
const router = express.Router();
|
| 57 |
const upload = multer();
|
| 58 |
|
| 59 |
-
// Debug: Server initialization
|
| 60 |
console.log('๐น Initializing Shadow Backend...');
|
| 61 |
|
| 62 |
// ---------------- CORS Middleware ----------------
|
| 63 |
app.use((req, res, next) => {
|
| 64 |
-
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 65 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
| 66 |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
| 67 |
|
| 68 |
-
|
| 69 |
-
if (req.method === 'OPTIONS') {
|
| 70 |
-
return res.sendStatus(200);
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
next();
|
| 74 |
});
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
// ---------------- Submit new shadow task ----------------
|
| 77 |
router.post('/add-shadow', upload.single('file'), async (req, res) => {
|
| 78 |
console.log('๐น /add-shadow called');
|
| 79 |
-
if (!req.file) {
|
| 80 |
-
console.warn('โ ๏ธ No file uploaded');
|
| 81 |
-
return res.status(400).json({ error: 'No image uploaded' });
|
| 82 |
-
}
|
| 83 |
|
| 84 |
const options = {
|
| 85 |
type: req.body.type || 'cast',
|
|
@@ -90,46 +87,31 @@ router.post('/add-shadow', upload.single('file'), async (req, res) => {
|
|
| 90 |
lightY: req.body.lightY ? parseFloat(req.body.lightY) : undefined,
|
| 91 |
};
|
| 92 |
|
| 93 |
-
console.log('๐น Shadow options:', options);
|
| 94 |
-
|
| 95 |
try {
|
| 96 |
const taskId = await addTask(req.file.buffer, options);
|
| 97 |
-
console.log(`โ
Task added with ID: ${taskId}`);
|
| 98 |
res.json({ task_id: taskId });
|
| 99 |
} catch (err) {
|
| 100 |
-
console.error(
|
| 101 |
res.status(500).json({ error: 'Server busy, try again' });
|
| 102 |
}
|
| 103 |
});
|
| 104 |
|
| 105 |
-
// ---------------- Server
|
| 106 |
router.get('/status', (req, res) => {
|
| 107 |
-
|
| 108 |
-
console.log('๐น /status called, queue_length:', queueLength);
|
| 109 |
-
res.json({
|
| 110 |
-
status: 'ok',
|
| 111 |
-
queue_length: queueLength,
|
| 112 |
-
});
|
| 113 |
});
|
| 114 |
|
| 115 |
// ---------------- Poll for result ----------------
|
| 116 |
router.get('/result/:id', (req, res) => {
|
| 117 |
const taskId = req.params.id;
|
| 118 |
-
console.log('๐น /result called for task:', taskId);
|
| 119 |
-
|
| 120 |
const status = getTaskStatus(taskId);
|
| 121 |
-
console.log('๐น Task status:', status);
|
| 122 |
|
| 123 |
if (status === 'pending') return res.sendStatus(202);
|
| 124 |
if (status === 'failed') return res.status(500).json({ error: 'Shadow generation failed' });
|
| 125 |
|
| 126 |
const result = getTaskResult(taskId);
|
| 127 |
-
if (!result) {
|
| 128 |
-
console.warn('โ ๏ธ Result not found for task:', taskId);
|
| 129 |
-
return res.status(404).json({ error: 'Result not found' });
|
| 130 |
-
}
|
| 131 |
|
| 132 |
-
console.log('๐น Returning result for task:', taskId);
|
| 133 |
res.set('Content-Type', 'image/png');
|
| 134 |
res.send(result);
|
| 135 |
});
|
|
@@ -137,10 +119,6 @@ router.get('/result/:id', (req, res) => {
|
|
| 137 |
app.use('/api/shadow', router);
|
| 138 |
|
| 139 |
// ---------------- Start server ----------------
|
| 140 |
-
const PORT = process.env.PORT
|
| 141 |
const HOST = '0.0.0.0';
|
| 142 |
-
|
| 143 |
-
console.log('๐น Starting server...');
|
| 144 |
app.listen(PORT, HOST, () => console.log(`๐ Shadow backend running on ${HOST}:${PORT}`));
|
| 145 |
-
|
| 146 |
-
module.exports = app;
|
|
|
|
| 43 |
// });
|
| 44 |
|
| 45 |
// module.exports = router;
|
| 46 |
+
|
| 47 |
+
|
| 48 |
const express = require('express');
|
| 49 |
const multer = require('multer');
|
| 50 |
const {
|
|
|
|
| 58 |
const router = express.Router();
|
| 59 |
const upload = multer();
|
| 60 |
|
|
|
|
| 61 |
console.log('๐น Initializing Shadow Backend...');
|
| 62 |
|
| 63 |
// ---------------- CORS Middleware ----------------
|
| 64 |
app.use((req, res, next) => {
|
| 65 |
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 66 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
| 67 |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
| 68 |
|
| 69 |
+
if (req.method === 'OPTIONS') return res.sendStatus(200);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
next();
|
| 71 |
});
|
| 72 |
|
| 73 |
+
// ---------------- Root route ----------------
|
| 74 |
+
app.get('/', (req, res) => res.send('Shadow Backend is running โ
'));
|
| 75 |
+
|
| 76 |
// ---------------- Submit new shadow task ----------------
|
| 77 |
router.post('/add-shadow', upload.single('file'), async (req, res) => {
|
| 78 |
console.log('๐น /add-shadow called');
|
| 79 |
+
if (!req.file) return res.status(400).json({ error: 'No image uploaded' });
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
const options = {
|
| 82 |
type: req.body.type || 'cast',
|
|
|
|
| 87 |
lightY: req.body.lightY ? parseFloat(req.body.lightY) : undefined,
|
| 88 |
};
|
| 89 |
|
|
|
|
|
|
|
| 90 |
try {
|
| 91 |
const taskId = await addTask(req.file.buffer, options);
|
|
|
|
| 92 |
res.json({ task_id: taskId });
|
| 93 |
} catch (err) {
|
| 94 |
+
console.error(err);
|
| 95 |
res.status(500).json({ error: 'Server busy, try again' });
|
| 96 |
}
|
| 97 |
});
|
| 98 |
|
| 99 |
+
// ---------------- Server status ----------------
|
| 100 |
router.get('/status', (req, res) => {
|
| 101 |
+
res.json({ status: 'ok', queue_length: getQueueLength() });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
});
|
| 103 |
|
| 104 |
// ---------------- Poll for result ----------------
|
| 105 |
router.get('/result/:id', (req, res) => {
|
| 106 |
const taskId = req.params.id;
|
|
|
|
|
|
|
| 107 |
const status = getTaskStatus(taskId);
|
|
|
|
| 108 |
|
| 109 |
if (status === 'pending') return res.sendStatus(202);
|
| 110 |
if (status === 'failed') return res.status(500).json({ error: 'Shadow generation failed' });
|
| 111 |
|
| 112 |
const result = getTaskResult(taskId);
|
| 113 |
+
if (!result) return res.status(404).json({ error: 'Result not found' });
|
|
|
|
|
|
|
|
|
|
| 114 |
|
|
|
|
| 115 |
res.set('Content-Type', 'image/png');
|
| 116 |
res.send(result);
|
| 117 |
});
|
|
|
|
| 119 |
app.use('/api/shadow', router);
|
| 120 |
|
| 121 |
// ---------------- Start server ----------------
|
| 122 |
+
const PORT = process.env.PORT || 7860;
|
| 123 |
const HOST = '0.0.0.0';
|
|
|
|
|
|
|
| 124 |
app.listen(PORT, HOST, () => console.log(`๐ Shadow backend running on ${HOST}:${PORT}`));
|
|
|
|
|
|