-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (79 loc) · 2.71 KB
/
Copy pathserver.js
File metadata and controls
100 lines (79 loc) · 2.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import express from 'express'
import compression from 'compression'
import path from 'path'
import { fileURLToPath } from 'url'
import admin from 'firebase-admin'
import { findComplementaryFoods } from './proteinScoring.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const app = express()
const PORT = process.env.PORT || 8080
// Initialize Firebase Admin SDK
// Check if we're in development (emulator) or production
const isDevelopment = process.env.NODE_ENV !== 'production'
if (isDevelopment) {
// Use emulator in development
process.env.FIRESTORE_EMULATOR_HOST = '127.0.0.1:8086'
console.log('🔧 Using Firestore Emulator (127.0.0.1:8086)')
}
admin.initializeApp({
projectId: 'prfctprotein-com',
})
const db = admin.firestore()
// Enable gzip compression
app.use(compression())
// Parse JSON bodies
app.use(express.json())
// API Routes
// Get all foods (names only for dropdown)
app.get('/api/foods', async (req, res) => {
try {
const snapshot = await db.collection('foods').orderBy('name').get()
const foods = snapshot.docs.map((doc) => ({
id: doc.id,
name: doc.data().name,
}))
res.json(foods)
} catch (error) {
console.error('Error fetching foods:', error)
res.status(500).json({ error: 'Failed to fetch foods' })
}
})
// Get a specific food by ID (with complementary food suggestions)
app.get('/api/foods/:id', async (req, res) => {
try {
const { id } = req.params
const doc = await db.collection('foods').doc(id).get()
if (!doc.exists) {
return res.status(404).json({ error: 'Food not found' })
}
const data = doc.data()
// Get all foods for complementary calculations
const allFoodsSnapshot = await db.collection('foods').get()
const allFoods = allFoodsSnapshot.docs.map((doc) => doc.data())
// Calculate complementary foods (top 50)
const complementaryFoods = findComplementaryFoods(data, allFoods, 50)
// Return food data with complementary suggestions
const response = {
name: data.name,
scientificName: data.scientificName || null,
sourceUrl: data.sourceUrl,
aminoAcids: data.aminoAcids,
nutritionalData: data.nutritionalData || null,
complementaryFoods,
}
res.json(response)
} catch (error) {
console.error('Error fetching food:', error)
res.status(500).json({ error: 'Failed to fetch food' })
}
})
// Serve static files from the dist directory
app.use(express.static(path.join(__dirname, 'dist')))
// Handle SPA routing - send all requests to index.html
app.use((req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'))
})
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
})