Spaces:
Sleeping
Sleeping
Upload index.js
Browse files
index.js
CHANGED
|
@@ -1,136 +1 @@
|
|
| 1 |
-
|
| 2 |
-
const cors = require('cors');
|
| 3 |
-
const dotenv = require('dotenv');
|
| 4 |
-
const path = require('path');
|
| 5 |
-
|
| 6 |
-
// Import models
|
| 7 |
-
const News = require('./models/News');
|
| 8 |
-
const Dataset = require('./models/Dataset');
|
| 9 |
-
|
| 10 |
-
dotenv.config();
|
| 11 |
-
|
| 12 |
-
const app = express();
|
| 13 |
-
// Use port 8501 for Hugging Face Spaces, or 3001 for local development
|
| 14 |
-
const PORT = process.env.HF_SPACES ? 8501 : (process.env.PORT || 3001);
|
| 15 |
-
|
| 16 |
-
// Enable CORS for all origins in development, specific origins in production
|
| 17 |
-
if (process.env.NODE_ENV === 'development') {
|
| 18 |
-
app.use(cors());
|
| 19 |
-
} else {
|
| 20 |
-
// In production (like Hugging Face Spaces), be more specific about CORS
|
| 21 |
-
app.use(cors({
|
| 22 |
-
origin: function (origin, callback) {
|
| 23 |
-
// Allow requests with no origin (like mobile apps or curl requests)
|
| 24 |
-
if (!origin) return callback(null, true);
|
| 25 |
-
|
| 26 |
-
// Allow localhost in development
|
| 27 |
-
if (origin.startsWith('http://localhost') ||
|
| 28 |
-
origin.startsWith('https://localhost') ||
|
| 29 |
-
origin.includes('huggingface.co') ||
|
| 30 |
-
origin.includes('hf.space') ||
|
| 31 |
-
origin === 'http://localhost:80' ||
|
| 32 |
-
origin === 'http://localhost:5173') {
|
| 33 |
-
return callback(null, true);
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
// Allow same origin requests
|
| 37 |
-
if (origin === `http://localhost:${PORT}`) {
|
| 38 |
-
return callback(null, true);
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
callback(null, true); // Allow all in production for now
|
| 42 |
-
}
|
| 43 |
-
}));
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
app.use(express.json());
|
| 47 |
-
|
| 48 |
-
// Serve static files from the client build in production
|
| 49 |
-
if (process.env.NODE_ENV === 'production') {
|
| 50 |
-
app.use(express.static('/usr/share/nginx/html'));
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
// Health check endpoint
|
| 54 |
-
app.get('/health', (req, res) => {
|
| 55 |
-
res.status(200).json({
|
| 56 |
-
status: 'OK',
|
| 57 |
-
timestamp: new Date().toISOString(),
|
| 58 |
-
uptime: process.uptime()
|
| 59 |
-
});
|
| 60 |
-
});
|
| 61 |
-
|
| 62 |
-
app.get('/', (req, res) => {
|
| 63 |
-
if (process.env.NODE_ENV === 'production') {
|
| 64 |
-
// In production, serve the React app
|
| 65 |
-
res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
|
| 66 |
-
} else {
|
| 67 |
-
res.json({
|
| 68 |
-
message: 'BioNexus Hub API',
|
| 69 |
-
version: '1.0.0',
|
| 70 |
-
timestamp: new Date().toISOString()
|
| 71 |
-
});
|
| 72 |
-
}
|
| 73 |
-
});
|
| 74 |
-
|
| 75 |
-
app.get('/api/news', async (req, res) => {
|
| 76 |
-
try {
|
| 77 |
-
const news = await News.getAll();
|
| 78 |
-
res.json(news);
|
| 79 |
-
} catch (error) {
|
| 80 |
-
console.error('Error fetching news:', error);
|
| 81 |
-
res.status(500).json({ error: 'Failed to fetch news' });
|
| 82 |
-
}
|
| 83 |
-
});
|
| 84 |
-
|
| 85 |
-
app.get('/api/news/:id', async (req, res) => {
|
| 86 |
-
try {
|
| 87 |
-
const { id } = req.params;
|
| 88 |
-
const newsItem = await News.getById(id);
|
| 89 |
-
if (!newsItem) {
|
| 90 |
-
return res.status(404).json({ error: 'News item not found' });
|
| 91 |
-
}
|
| 92 |
-
res.json(newsItem);
|
| 93 |
-
} catch (error) {
|
| 94 |
-
console.error('Error fetching news item:', error);
|
| 95 |
-
res.status(500).json({ error: 'Failed to fetch news item' });
|
| 96 |
-
}
|
| 97 |
-
});
|
| 98 |
-
|
| 99 |
-
app.get('/api/datasets', async (req, res) => {
|
| 100 |
-
try {
|
| 101 |
-
const datasets = await Dataset.getAll();
|
| 102 |
-
res.json(datasets);
|
| 103 |
-
} catch (error) {
|
| 104 |
-
console.error('Error fetching datasets:', error);
|
| 105 |
-
res.status(500).json({ error: 'Failed to fetch datasets' });
|
| 106 |
-
}
|
| 107 |
-
});
|
| 108 |
-
|
| 109 |
-
app.get('/api/datasets/:id', async (req, res) => {
|
| 110 |
-
try {
|
| 111 |
-
const { id } = req.params;
|
| 112 |
-
const dataset = await Dataset.getById(id);
|
| 113 |
-
if (!dataset) {
|
| 114 |
-
return res.status(404).json({ error: 'Dataset not found' });
|
| 115 |
-
}
|
| 116 |
-
res.json(dataset);
|
| 117 |
-
} catch (error) {
|
| 118 |
-
console.error('Error fetching dataset:', error);
|
| 119 |
-
res.status(500).json({ error: 'Failed to fetch dataset' });
|
| 120 |
-
}
|
| 121 |
-
});
|
| 122 |
-
|
| 123 |
-
// In production, serve the React app for any non-API routes
|
| 124 |
-
if (process.env.NODE_ENV === 'production') {
|
| 125 |
-
app.get('*', (req, res) => {
|
| 126 |
-
if (!req.path.startsWith('/api/') && req.path !== '/health') {
|
| 127 |
-
res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
|
| 128 |
-
} else {
|
| 129 |
-
res.status(404).json({ error: 'API endpoint not found' });
|
| 130 |
-
}
|
| 131 |
-
});
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
app.listen(PORT, '0.0.0.0', () => {
|
| 135 |
-
console.log(`BioNexus Hub server running on port ${PORT}`);
|
| 136 |
-
});
|
|
|
|
| 1 |
+
console.log('Happy developing ✨')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|