Spaces:
Configuration error
Configuration error
| const express = require('express'); | |
| const cors = require('cors'); | |
| const { db } = require('./firebase'); | |
| const app = express(); | |
| app.use(cors()); | |
| app.use(express.json()); | |
| const PORT = process.env.PORT || 7860; // HF Spaces default port | |
| // Health check | |
| app.get('/', (req, res) => { | |
| res.send('Neon Echo Game Server is Running'); | |
| }); | |
| // Submit Score Endpoint | |
| app.post('/api/score', async (req, res) => { | |
| try { | |
| const { score, name, photoURL, uid, token } = req.body; | |
| if (!uid || typeof score !== 'number') { | |
| return res.status(400).json({ error: 'Invalid data' }); | |
| } | |
| const appId = 'neon-echo-default'; | |
| // Write to Firestore securely via Admin SDK | |
| await db.collection('artifacts').doc(appId) | |
| .collection('public').doc('data') | |
| .collection('leaderboard').collection('scores') | |
| .add({ | |
| name: name || 'Anonymous', | |
| score: score, | |
| photoURL: photoURL || null, | |
| uid: uid, | |
| timestamp: new Date() // Server-side timestamp | |
| }); | |
| res.json({ success: true, message: 'Score saved' }); | |
| } catch (error) { | |
| console.error('Error saving score:', error); | |
| res.status(500).json({ error: 'Internal Server Error' }); | |
| } | |
| }); | |
| // Get Leaderboard Endpoint | |
| app.get('/api/leaderboard', async (req, res) => { | |
| try { | |
| const appId = 'neon-echo-default'; | |
| const snapshot = await db.collection('artifacts').doc(appId) | |
| .collection('public').doc('data') | |
| .collection('leaderboard').collection('scores') | |
| .orderBy('score', 'desc') | |
| .limit(50) | |
| .get(); | |
| const scores = []; | |
| snapshot.forEach(doc => scores.push(doc.data())); | |
| res.json(scores); | |
| } catch (error) { | |
| console.error(error); | |
| res.status(500).send('Error'); | |
| } | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`Server running on port ${PORT}`); | |
| }); | |