karthikeya1212 commited on
Commit
c6864a7
ยท
verified ยท
1 Parent(s): d45b274

Update app/server.js

Browse files
Files changed (1) hide show
  1. app/server.js +29 -8
app/server.js CHANGED
@@ -43,7 +43,6 @@
43
  // });
44
 
45
  // module.exports = router;
46
-
47
  const express = require('express');
48
  const multer = require('multer');
49
  const cors = require('cors');
@@ -58,11 +57,18 @@ const app = express();
58
  const router = express.Router();
59
  const upload = multer();
60
 
 
 
 
61
  app.use(cors());
62
 
63
  // ---------------- Submit new shadow task ----------------
64
  router.post('/add-shadow', upload.single('file'), async (req, res) => {
65
- if (!req.file) return res.status(400).json({ error: 'No image uploaded' });
 
 
 
 
66
 
67
  const options = {
68
  type: req.body.type || 'cast',
@@ -73,8 +79,11 @@ router.post('/add-shadow', upload.single('file'), async (req, res) => {
73
  lightY: req.body.lightY ? parseFloat(req.body.lightY) : undefined,
74
  };
75
 
 
 
76
  try {
77
  const taskId = await addTask(req.file.buffer, options);
 
78
  res.json({ task_id: taskId });
79
  } catch (err) {
80
  console.error('โŒ Error adding shadow task:', err);
@@ -84,22 +93,32 @@ router.post('/add-shadow', upload.single('file'), async (req, res) => {
84
 
85
  // ---------------- Server load / status ----------------
86
  router.get('/status', (req, res) => {
 
 
87
  res.json({
88
  status: 'ok',
89
- queue_length: getQueueLength(),
90
  });
91
  });
92
 
93
  // ---------------- Poll for result ----------------
94
  router.get('/result/:id', (req, res) => {
95
- const status = getTaskStatus(req.params.id);
 
 
 
 
96
 
97
  if (status === 'pending') return res.sendStatus(202);
98
  if (status === 'failed') return res.status(500).json({ error: 'Shadow generation failed' });
99
 
100
- const result = getTaskResult(req.params.id);
101
- if (!result) return res.status(404).json({ error: 'Result not found' });
 
 
 
102
 
 
103
  res.set('Content-Type', 'image/png');
104
  res.send(result);
105
  });
@@ -108,7 +127,9 @@ app.use('/api/shadow', router);
108
 
109
  // ---------------- Start server ----------------
110
  const PORT = process.env.PORT || 5000;
111
- const HOST = '0.0.0.0'; // Bind to all network interfaces for HF Spaces
112
- app.listen(PORT, HOST, () => console.log(`๐Ÿš€ Shadow backend running on port ${PORT}`));
 
 
113
 
114
  module.exports = app;
 
43
  // });
44
 
45
  // module.exports = router;
 
46
  const express = require('express');
47
  const multer = require('multer');
48
  const cors = require('cors');
 
57
  const router = express.Router();
58
  const upload = multer();
59
 
60
+ // Debug: Server initialization
61
+ console.log('๐Ÿ”น Initializing Shadow Backend...');
62
+
63
  app.use(cors());
64
 
65
  // ---------------- Submit new shadow task ----------------
66
  router.post('/add-shadow', upload.single('file'), async (req, res) => {
67
+ console.log('๐Ÿ”น /add-shadow called');
68
+ if (!req.file) {
69
+ console.warn('โš ๏ธ No file uploaded');
70
+ return res.status(400).json({ error: 'No image uploaded' });
71
+ }
72
 
73
  const options = {
74
  type: req.body.type || 'cast',
 
79
  lightY: req.body.lightY ? parseFloat(req.body.lightY) : undefined,
80
  };
81
 
82
+ console.log('๐Ÿ”น Shadow options:', options);
83
+
84
  try {
85
  const taskId = await addTask(req.file.buffer, options);
86
+ console.log(`โœ… Task added with ID: ${taskId}`);
87
  res.json({ task_id: taskId });
88
  } catch (err) {
89
  console.error('โŒ Error adding shadow task:', err);
 
93
 
94
  // ---------------- Server load / status ----------------
95
  router.get('/status', (req, res) => {
96
+ const queueLength = getQueueLength();
97
+ console.log('๐Ÿ”น /status called, queue_length:', queueLength);
98
  res.json({
99
  status: 'ok',
100
+ queue_length: queueLength,
101
  });
102
  });
103
 
104
  // ---------------- Poll for result ----------------
105
  router.get('/result/:id', (req, res) => {
106
+ const taskId = req.params.id;
107
+ console.log('๐Ÿ”น /result called for task:', taskId);
108
+
109
+ const status = getTaskStatus(taskId);
110
+ console.log('๐Ÿ”น Task status:', status);
111
 
112
  if (status === 'pending') return res.sendStatus(202);
113
  if (status === 'failed') return res.status(500).json({ error: 'Shadow generation failed' });
114
 
115
+ const result = getTaskResult(taskId);
116
+ if (!result) {
117
+ console.warn('โš ๏ธ Result not found for task:', taskId);
118
+ return res.status(404).json({ error: 'Result not found' });
119
+ }
120
 
121
+ console.log('๐Ÿ”น Returning result for task:', taskId);
122
  res.set('Content-Type', 'image/png');
123
  res.send(result);
124
  });
 
127
 
128
  // ---------------- Start server ----------------
129
  const PORT = process.env.PORT || 5000;
130
+ const HOST = '0.0.0.0';
131
+
132
+ console.log('๐Ÿ”น Starting server...');
133
+ app.listen(PORT, HOST, () => console.log(`๐Ÿš€ Shadow backend running on ${HOST}:${PORT}`));
134
 
135
  module.exports = app;