MetiMiester commited on
Commit
c78c6a9
·
verified ·
1 Parent(s): 9ae42eb

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +32 -68
Dockerfile CHANGED
@@ -1,68 +1,32 @@
1
- // ESM server (Node 20+). Serves API + built React app from frontend/dist
2
-
3
- import 'dotenv/config';
4
- import express from 'express';
5
- import cors from 'cors';
6
- import helmet from 'helmet';
7
- import morgan from 'morgan';
8
- import mongoose from 'mongoose';
9
- import path from 'node:path';
10
- import { fileURLToPath } from 'node:url';
11
-
12
- // ---- paths / __dirname ----
13
- const __filename = fileURLToPath(import.meta.url);
14
- const __dirname = path.dirname(__filename);
15
-
16
- // ---- env & port ----
17
- const MONGODB_URI = process.env.MONGODB_URI;
18
- const PORT = Number(process.env.PORT) || 7860; // HF uses $PORT (7860)
19
-
20
- // ---- basic app ----
21
- const app = express();
22
- app.set('trust proxy', 1);
23
- app.use(helmet());
24
- app.use(cors());
25
- app.use(express.json({ limit: '1mb' }));
26
- app.use(morgan('tiny'));
27
-
28
- // ---- DB connect ----
29
- if (!MONGODB_URI) {
30
- console.error('❌ Missing MONGODB_URI environment variable.');
31
- process.exit(1);
32
- }
33
- mongoose.set('strictQuery', true);
34
- try {
35
- await mongoose.connect(MONGODB_URI);
36
- console.log('✅ MongoDB connected');
37
- } catch (err) {
38
- console.error('❌ MongoDB connection error:', err.message || err);
39
- process.exit(1);
40
- }
41
-
42
- // ---- routes ----
43
- // import your existing router (already created earlier)
44
- import productsRouter from './routes/products.routes.js';
45
-
46
- // Health check first (before static fallback)
47
- app.get('/health', (_req, res) => {
48
- const conn = mongoose.connection.readyState === 1 ? 'connected' : 'disconnected';
49
- res.json({ ok: conn === 'connected', db: conn });
50
- });
51
-
52
- // API
53
- app.use('/api/products', productsRouter);
54
-
55
- // ---- static frontend build ----
56
- const clientDir = path.join(__dirname, '../../frontend/dist');
57
- app.use(express.static(clientDir));
58
-
59
- // Fallback to index.html for SPA routes (but not for /api/*)
60
- app.get('*', (req, res) => {
61
- if (req.path.startsWith('/api/')) return res.status(404).json({ error: 'Not found' });
62
- res.sendFile(path.join(clientDir, 'index.html'));
63
- });
64
-
65
- // ---- start ----
66
- app.listen(PORT, '0.0.0.0', () => {
67
- console.log(`🚀 API running on http://0.0.0.0:${PORT}`);
68
- });
 
1
+ # ---------- build stage ----------
2
+ FROM node:20-slim AS builder
3
+ WORKDIR /app
4
+
5
+ # Install deps (non-strict to avoid lockfile mismatches)
6
+ COPY frontend/package*.json frontend/
7
+ COPY backend/package*.json backend/
8
+ RUN npm install --prefix frontend
9
+ RUN npm install --omit=dev --prefix backend
10
+
11
+ # Copy sources
12
+ COPY frontend ./frontend
13
+ COPY backend ./backend
14
+
15
+ # Build React app
16
+ RUN npm run build --prefix frontend
17
+
18
+ # ---------- runtime ----------
19
+ FROM node:20-slim
20
+ WORKDIR /app
21
+
22
+ # Bring backend + built frontend
23
+ COPY --from=builder /app/backend ./backend
24
+ COPY --from=builder /app/frontend/dist ./frontend/dist
25
+
26
+ ENV NODE_ENV=production
27
+ # Hugging Face sets $PORT (usually 7860). We bind to it explicitly.
28
+ ENV PORT=7860
29
+ EXPOSE 7860
30
+
31
+ # Start Express (serves /api + frontend/dist)
32
+ CMD ["node", "backend/src/server.js"]