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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π Deploy Backend-Only to Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
This guide explains how to deploy **only the NestJS backend** from your GitHub monorepo to Hugging Face Spaces.
|
| 6 |
+
|
| 7 |
+
## π What Gets Deployed
|
| 8 |
+
|
| 9 |
+
- β
**Backend** (NestJS + BullMQ + Redis)
|
| 10 |
+
- β **Frontend** (stays in GitHub only)
|
| 11 |
+
|
| 12 |
+
## π― Deployment Steps
|
| 13 |
+
|
| 14 |
+
### Step 1: Prepare Your Repository
|
| 15 |
+
|
| 16 |
+
Your GitHub repo already has the full monorepo. We'll tell HF Spaces to use only the backend.
|
| 17 |
+
|
| 18 |
+
**Two options:**
|
| 19 |
+
|
| 20 |
+
#### **Option A: Rename Dockerfile** (Easiest)
|
| 21 |
+
```powershell
|
| 22 |
+
# In c:\demo
|
| 23 |
+
# Temporarily rename the full-stack Dockerfile
|
| 24 |
+
Move-Item Dockerfile Dockerfile.fullstack
|
| 25 |
+
|
| 26 |
+
# Rename the backend-only Dockerfile to Dockerfile
|
| 27 |
+
Move-Item Dockerfile.backend Dockerfile
|
| 28 |
+
|
| 29 |
+
# Commit and push
|
| 30 |
+
git add .
|
| 31 |
+
git commit -m "Use backend-only Dockerfile for HF Spaces deployment"
|
| 32 |
+
git push origin main
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
#### **Option B: Keep Multiple Dockerfiles** (Flexible)
|
| 36 |
+
Keep `Dockerfile.backend` and configure HF Spaces to use it (explained below).
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
### Step 2: Create Hugging Face Space
|
| 41 |
+
|
| 42 |
+
1. Go to https://huggingface.co/new-space
|
| 43 |
+
|
| 44 |
+
2. Fill in the details:
|
| 45 |
+
```
|
| 46 |
+
Space name: wagerkit-backend (or your choice)
|
| 47 |
+
License: MIT
|
| 48 |
+
SDK: Docker β οΈ Must select Docker!
|
| 49 |
+
Hardware: CPU Basic (free)
|
| 50 |
+
Visibility: Public
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
3. Click **Create Space**
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
### Step 3: Connect to GitHub
|
| 58 |
+
|
| 59 |
+
You have two methods to get your code to HF Spaces:
|
| 60 |
+
|
| 61 |
+
#### **Method 1: Direct Push** (Simple)
|
| 62 |
+
|
| 63 |
+
```powershell
|
| 64 |
+
# Add HF Space as remote
|
| 65 |
+
git remote add space https://huggingface.co/spaces/YOUR-USERNAME/wagerkit-backend
|
| 66 |
+
|
| 67 |
+
# Push your code
|
| 68 |
+
git push space main
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
#### **Method 2: GitHub Sync** (Automatic Updates)
|
| 72 |
+
|
| 73 |
+
1. In your HF Space, click **Settings** β **Repository**
|
| 74 |
+
2. Click **Link to GitHub**
|
| 75 |
+
3. Authorize Hugging Face to access your GitHub
|
| 76 |
+
4. Select your repository: `YOUR-GITHUB-USERNAME/demo` (or whatever you named it)
|
| 77 |
+
5. **Branch:** `main`
|
| 78 |
+
6. Click **Link Repository**
|
| 79 |
+
|
| 80 |
+
Now every push to GitHub main branch auto-deploys to HF Spaces! π
|
| 81 |
+
|
| 82 |
+
---
|
| 83 |
+
|
| 84 |
+
### Step 4: Configure Dockerfile Path (If Using Option B)
|
| 85 |
+
|
| 86 |
+
If you kept `Dockerfile.backend` instead of renaming:
|
| 87 |
+
|
| 88 |
+
1. In your HF Space, go to **Settings**
|
| 89 |
+
2. Find **Docker** section
|
| 90 |
+
3. Set **Dockerfile path**: `Dockerfile.backend`
|
| 91 |
+
4. Click **Save**
|
| 92 |
+
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
### Step 5: Add Environment Variables (Optional)
|
| 96 |
+
|
| 97 |
+
If you want to change the DOME API key or add secrets:
|
| 98 |
+
|
| 99 |
+
1. In HF Space settings, go to **Variables and secrets**
|
| 100 |
+
2. Add secrets:
|
| 101 |
+
```
|
| 102 |
+
Name: DOME_API_KEY
|
| 103 |
+
Value: your-actual-api-key-here
|
| 104 |
+
```
|
| 105 |
+
3. Update backend code to read from environment:
|
| 106 |
+
```typescript
|
| 107 |
+
// backend/src/config/configuration.ts or .env
|
| 108 |
+
DOME_API_KEY=process.env.DOME_API_KEY || 'default-key'
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
---
|
| 112 |
+
|
| 113 |
+
### Step 6: Wait for Build
|
| 114 |
+
|
| 115 |
+
- Build starts automatically after push
|
| 116 |
+
- **Build time:** ~5-10 minutes
|
| 117 |
+
- Watch logs in the **Logs** tab
|
| 118 |
+
|
| 119 |
+
**Build stages you'll see:**
|
| 120 |
+
```
|
| 121 |
+
[1/4] Building backend dependencies...
|
| 122 |
+
[2/4] Copying source files...
|
| 123 |
+
[3/4] Building NestJS application...
|
| 124 |
+
[4/4] Creating runtime image...
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
---
|
| 128 |
+
|
| 129 |
+
### Step 7: Access Your Backend API
|
| 130 |
+
|
| 131 |
+
Once deployed (status shows "Running" with green dot):
|
| 132 |
+
|
| 133 |
+
**Your API will be at:**
|
| 134 |
+
```
|
| 135 |
+
https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
**Example endpoints:**
|
| 139 |
+
```
|
| 140 |
+
GET https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets
|
| 141 |
+
GET https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets/us_election_2024_winner
|
| 142 |
+
GET https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets/jobs/status
|
| 143 |
+
POST https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets/us_election_2024_winner/refresh
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
---
|
| 147 |
+
|
| 148 |
+
## π§ͺ Testing Your Deployment
|
| 149 |
+
|
| 150 |
+
### Test in Browser
|
| 151 |
+
|
| 152 |
+
Visit: `https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets`
|
| 153 |
+
|
| 154 |
+
You should see JSON with 3 markets.
|
| 155 |
+
|
| 156 |
+
### Test with PowerShell
|
| 157 |
+
|
| 158 |
+
```powershell
|
| 159 |
+
# Replace with your actual Space URL
|
| 160 |
+
$url = "https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets"
|
| 161 |
+
|
| 162 |
+
# Test markets endpoint
|
| 163 |
+
Invoke-RestMethod -Uri $url | ConvertTo-Json
|
| 164 |
+
|
| 165 |
+
# Test specific market
|
| 166 |
+
Invoke-RestMethod -Uri "$url/us_election_2024_winner" | ConvertTo-Json
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
### Test with cURL
|
| 170 |
+
|
| 171 |
+
```bash
|
| 172 |
+
curl https://YOUR-USERNAME-wagerkit-backend.hf.space/api/markets
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
---
|
| 176 |
+
|
| 177 |
+
## π§ Important Configuration
|
| 178 |
+
|
| 179 |
+
### Port Configuration
|
| 180 |
+
|
| 181 |
+
The `Dockerfile.backend` is configured for HF Spaces:
|
| 182 |
+
- **External port:** 7860 (HF Spaces requirement)
|
| 183 |
+
- **Redis port:** 6379 (internal only)
|
| 184 |
+
- **Backend serves on:** 7860
|
| 185 |
+
|
| 186 |
+
### Redis Configuration
|
| 187 |
+
|
| 188 |
+
Redis is embedded in the container:
|
| 189 |
+
- **No external Redis needed** β
|
| 190 |
+
- Starts automatically with the backend
|
| 191 |
+
- Data is **not persistent** (resets on restart)
|
| 192 |
+
|
| 193 |
+
---
|
| 194 |
+
|
| 195 |
+
## π Troubleshooting
|
| 196 |
+
|
| 197 |
+
### Build Fails
|
| 198 |
+
|
| 199 |
+
**Check logs:**
|
| 200 |
+
1. Go to your Space β **Logs** tab
|
| 201 |
+
2. Look for red error messages
|
| 202 |
+
|
| 203 |
+
**Common issues:**
|
| 204 |
+
|
| 205 |
+
| Error | Solution |
|
| 206 |
+
|-------|----------|
|
| 207 |
+
| `Cannot find module` | Check `backend/package.json` includes all dependencies |
|
| 208 |
+
| `COPY failed` | Ensure `backend/` folder exists in repo |
|
| 209 |
+
| `npm ci failed` | Delete `package-lock.json`, regenerate with `npm install` |
|
| 210 |
+
| `Build timeout` | Free tier has 1GB RAM limit - try optimizing dependencies |
|
| 211 |
+
|
| 212 |
+
### App Not Starting
|
| 213 |
+
|
| 214 |
+
**Check runtime logs:**
|
| 215 |
+
1. Space β **Logs** tab (after build completes)
|
| 216 |
+
2. Look for startup messages
|
| 217 |
+
|
| 218 |
+
**Common issues:**
|
| 219 |
+
|
| 220 |
+
| Issue | Solution |
|
| 221 |
+
|-------|----------|
|
| 222 |
+
| Redis not starting | Check startup script logs, ensure Redis installed |
|
| 223 |
+
| Port binding error | Dockerfile must use PORT=7860 |
|
| 224 |
+
| Application crash | Check environment variables, missing .env values |
|
| 225 |
+
|
| 226 |
+
### API Returns Empty Data
|
| 227 |
+
|
| 228 |
+
**Check market processing:**
|
| 229 |
+
```powershell
|
| 230 |
+
# Check job status
|
| 231 |
+
Invoke-RestMethod -Uri "https://YOUR-URL.hf.space/api/markets/jobs/status"
|
| 232 |
+
```
|
| 233 |
+
|
| 234 |
+
**If jobs are stuck:**
|
| 235 |
+
- Redis might not be running
|
| 236 |
+
- Check logs for BullMQ errors
|
| 237 |
+
- Try refreshing a market manually:
|
| 238 |
+
```powershell
|
| 239 |
+
Invoke-RestMethod -Method POST -Uri "https://YOUR-URL.hf.space/api/markets/us_election_2024_winner/refresh"
|
| 240 |
+
```
|
| 241 |
+
|
| 242 |
+
### Slow Response Times
|
| 243 |
+
|
| 244 |
+
Free tier CPU Basic has limited resources (2 vCPU, 16GB storage).
|
| 245 |
+
|
| 246 |
+
**Solutions:**
|
| 247 |
+
- β
Reduce number of markets processed simultaneously
|
| 248 |
+
- β
Increase cache duration
|
| 249 |
+
- β
Upgrade to CPU Medium ($5/month)
|
| 250 |
+
|
| 251 |
+
---
|
| 252 |
+
|
| 253 |
+
## π Monitoring
|
| 254 |
+
|
| 255 |
+
### Check Application Health
|
| 256 |
+
|
| 257 |
+
HF Spaces shows status dot:
|
| 258 |
+
- π’ **Green (Running):** All good
|
| 259 |
+
- π‘ **Yellow (Building):** Deployment in progress
|
| 260 |
+
- π΄ **Red (Error):** Application crashed
|
| 261 |
+
|
| 262 |
+
### Check Background Jobs
|
| 263 |
+
|
| 264 |
+
```powershell
|
| 265 |
+
$status = Invoke-RestMethod -Uri "https://YOUR-URL.hf.space/api/markets/jobs/status"
|
| 266 |
+
$status | Format-List
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
Expected output:
|
| 270 |
+
```
|
| 271 |
+
queuedJobs : 0
|
| 272 |
+
activeJobs : 0
|
| 273 |
+
completedJobs : 3
|
| 274 |
+
failedJobs : 0
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
---
|
| 278 |
+
|
| 279 |
+
## π Updating Your Deployment
|
| 280 |
+
|
| 281 |
+
### If Using Direct Push:
|
| 282 |
+
```powershell
|
| 283 |
+
# Make changes to backend code
|
| 284 |
+
# Commit changes
|
| 285 |
+
git add .
|
| 286 |
+
git commit -m "Update backend logic"
|
| 287 |
+
|
| 288 |
+
# Push to HF Space (triggers rebuild)
|
| 289 |
+
git push space main
|
| 290 |
+
```
|
| 291 |
+
|
| 292 |
+
### If Using GitHub Sync:
|
| 293 |
+
```powershell
|
| 294 |
+
# Just push to GitHub
|
| 295 |
+
git push origin main
|
| 296 |
+
# HF Space auto-deploys! π
|
| 297 |
+
```
|
| 298 |
+
|
| 299 |
+
Rebuild takes ~5-10 minutes.
|
| 300 |
+
|
| 301 |
+
---
|
| 302 |
+
|
| 303 |
+
## π¨ Optional: Custom Domain
|
| 304 |
+
|
| 305 |
+
HF Spaces Pro allows custom domains:
|
| 306 |
+
|
| 307 |
+
1. Upgrade to HF Pro
|
| 308 |
+
2. Go to Space settings β **Domain**
|
| 309 |
+
3. Add your domain (e.g., `api.wagerkit.com`)
|
| 310 |
+
4. Update DNS records as shown
|
| 311 |
+
|
| 312 |
+
---
|
| 313 |
+
|
| 314 |
+
## π What's NOT Deployed
|
| 315 |
+
|
| 316 |
+
Since this is backend-only:
|
| 317 |
+
|
| 318 |
+
- β Next.js frontend (stays in GitHub)
|
| 319 |
+
- β Frontend's node_modules
|
| 320 |
+
- β Frontend's build artifacts
|
| 321 |
+
|
| 322 |
+
**To access the API from a frontend elsewhere:**
|
| 323 |
+
```typescript
|
| 324 |
+
// In your frontend deployed somewhere else
|
| 325 |
+
const API_URL = 'https://YOUR-USERNAME-wagerkit-backend.hf.space/api';
|
| 326 |
+
|
| 327 |
+
const markets = await fetch(`${API_URL}/markets`).then(r => r.json());
|
| 328 |
+
```
|
| 329 |
+
|
| 330 |
+
---
|
| 331 |
+
|
| 332 |
+
## π Next Steps
|
| 333 |
+
|
| 334 |
+
1. **Test all endpoints** to ensure they work
|
| 335 |
+
2. **Update DOME_API_KEY** if needed (via HF Secrets)
|
| 336 |
+
3. **Monitor usage** in HF Space β Analytics
|
| 337 |
+
4. **Deploy frontend separately** (Vercel, Netlify, or another HF Space)
|
| 338 |
+
5. **Set up CORS** if frontend is on different domain
|
| 339 |
+
|
| 340 |
+
---
|
| 341 |
+
|
| 342 |
+
## π‘ Pro Tips
|
| 343 |
+
|
| 344 |
+
β
**Use GitHub Sync** for automatic deployments
|
| 345 |
+
β
**Add health checks** in your NestJS app
|
| 346 |
+
β
**Log important events** for debugging
|
| 347 |
+
β
**Use HF Secrets** for sensitive data (API keys)
|
| 348 |
+
β
**Monitor build times** - optimize if builds take >10 mins
|
| 349 |
+
β
**Test locally first** with Docker: `docker build -f Dockerfile.backend -t wagerkit-backend .`
|
| 350 |
+
|
| 351 |
+
---
|
| 352 |
+
|
| 353 |
+
## π Need Help?
|
| 354 |
+
|
| 355 |
+
- **HF Spaces Docs:** https://huggingface.co/docs/hub/spaces
|
| 356 |
+
- **Build logs:** Your Space β Logs tab
|
| 357 |
+
- **Community:** https://discuss.huggingface.co/
|
| 358 |
+
|
| 359 |
+
---
|
| 360 |
+
|
| 361 |
+
**Your backend is now running on Hugging Face Spaces at port 7860! π**
|
Dockerfile
CHANGED
|
@@ -1,97 +1,64 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
# Copy backend package files
|
| 9 |
COPY backend/package*.json ./
|
|
|
|
|
|
|
| 10 |
RUN npm ci --only=production
|
| 11 |
|
| 12 |
-
# Copy backend source
|
| 13 |
COPY backend ./
|
| 14 |
-
RUN npm run build
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
FROM node:20-alpine AS frontend-build
|
| 18 |
-
|
| 19 |
-
WORKDIR /app/frontend
|
| 20 |
-
|
| 21 |
-
# Copy frontend package files
|
| 22 |
-
COPY frontend/package*.json ./
|
| 23 |
-
RUN npm ci
|
| 24 |
-
|
| 25 |
-
# Copy frontend source
|
| 26 |
-
COPY frontend ./
|
| 27 |
-
|
| 28 |
-
# Build Next.js frontend (standalone mode)
|
| 29 |
-
ENV NEXT_PUBLIC_API_URL=/api
|
| 30 |
RUN npm run build
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
FROM node:20-alpine
|
| 34 |
-
|
| 35 |
-
WORKDIR /app
|
| 36 |
-
|
| 37 |
-
# Install dependencies for Redis and process management
|
| 38 |
-
RUN apk add --no-cache redis bash
|
| 39 |
-
|
| 40 |
-
# Copy backend production files
|
| 41 |
-
COPY --from=backend-build /app/backend/dist ./backend/dist
|
| 42 |
-
COPY --from=backend-build /app/backend/node_modules ./backend/node_modules
|
| 43 |
-
COPY --from=backend-build /app/backend/package.json ./backend/
|
| 44 |
-
|
| 45 |
-
# Copy backend .env (if exists)
|
| 46 |
-
COPY backend/.env ./backend/.env 2>/dev/null || true
|
| 47 |
-
|
| 48 |
-
# Copy frontend production files (standalone build)
|
| 49 |
-
COPY --from=frontend-build /app/frontend/.next/standalone ./frontend/
|
| 50 |
-
COPY --from=frontend-build /app/frontend/.next/static ./frontend/.next/static
|
| 51 |
-
COPY --from=frontend-build /app/frontend/public ./frontend/public
|
| 52 |
-
COPY --from=frontend-build /app/frontend/package.json ./frontend/
|
| 53 |
-
|
| 54 |
-
# Environment variables for Hugging Face Spaces
|
| 55 |
ENV NODE_ENV=production
|
| 56 |
ENV PORT=7860
|
| 57 |
ENV REDIS_HOST=127.0.0.1
|
| 58 |
ENV REDIS_PORT=6379
|
| 59 |
-
ENV NEXT_PUBLIC_API_URL=/api
|
| 60 |
|
| 61 |
-
# Expose
|
| 62 |
EXPOSE 7860
|
| 63 |
|
| 64 |
-
# Create
|
| 65 |
RUN cat > /app/start.sh << 'EOF'
|
| 66 |
#!/bin/bash
|
| 67 |
set -e
|
| 68 |
|
| 69 |
-
echo "π Starting WagerKit on Hugging Face Spaces..."
|
| 70 |
|
| 71 |
# Start Redis in background
|
| 72 |
echo "π¦ Starting Redis..."
|
| 73 |
-
redis-server --daemonize yes --bind 127.0.0.1 --port 6379 --loglevel warning
|
| 74 |
sleep 2
|
| 75 |
|
| 76 |
# Verify Redis is running
|
| 77 |
-
redis-cli ping > /dev/null
|
| 78 |
-
echo "β
Redis is ready"
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# Start Frontend (foreground - keeps container alive)
|
| 89 |
-
echo "π Starting Next.js frontend on port 7860..."
|
| 90 |
-
cd /app/frontend
|
| 91 |
-
PORT=7860 node server.js
|
| 92 |
|
| 93 |
EOF
|
| 94 |
|
| 95 |
RUN chmod +x /app/start.sh
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
CMD ["/app/start.sh"]
|
|
|
|
| 1 |
+
# Backend-Only Dockerfile for Hugging Face Spaces
|
| 2 |
+
# Includes Redis for BullMQ job processing
|
| 3 |
|
| 4 |
+
FROM node:20-alpine
|
| 5 |
+
|
| 6 |
+
WORKDIR /app
|
| 7 |
|
| 8 |
+
# Install Redis and bash
|
| 9 |
+
RUN apk add --no-cache redis bash curl
|
| 10 |
|
| 11 |
# Copy backend package files
|
| 12 |
COPY backend/package*.json ./
|
| 13 |
+
|
| 14 |
+
# Install dependencies
|
| 15 |
RUN npm ci --only=production
|
| 16 |
|
| 17 |
+
# Copy backend source
|
| 18 |
COPY backend ./
|
|
|
|
| 19 |
|
| 20 |
+
# Build NestJS application
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
RUN npm run build
|
| 22 |
|
| 23 |
+
# Environment variables for HF Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
ENV NODE_ENV=production
|
| 25 |
ENV PORT=7860
|
| 26 |
ENV REDIS_HOST=127.0.0.1
|
| 27 |
ENV REDIS_PORT=6379
|
|
|
|
| 28 |
|
| 29 |
+
# Expose HF Spaces default port
|
| 30 |
EXPOSE 7860
|
| 31 |
|
| 32 |
+
# Create startup script
|
| 33 |
RUN cat > /app/start.sh << 'EOF'
|
| 34 |
#!/bin/bash
|
| 35 |
set -e
|
| 36 |
|
| 37 |
+
echo "π Starting WagerKit Backend on Hugging Face Spaces..."
|
| 38 |
|
| 39 |
# Start Redis in background
|
| 40 |
echo "π¦ Starting Redis..."
|
| 41 |
+
redis-server --daemonize yes --bind 127.0.0.1 --port 6379 --loglevel warning --save ""
|
| 42 |
sleep 2
|
| 43 |
|
| 44 |
# Verify Redis is running
|
| 45 |
+
if redis-cli ping > /dev/null 2>&1; then
|
| 46 |
+
echo "β
Redis is ready"
|
| 47 |
+
else
|
| 48 |
+
echo "β Redis failed to start"
|
| 49 |
+
exit 1
|
| 50 |
+
fi
|
| 51 |
+
|
| 52 |
+
# Start NestJS backend on port 7860
|
| 53 |
+
echo "π§ Starting NestJS backend on port 7860..."
|
| 54 |
+
PORT=7860 node dist/main.js
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
EOF
|
| 57 |
|
| 58 |
RUN chmod +x /app/start.sh
|
| 59 |
|
| 60 |
+
# Health check
|
| 61 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
| 62 |
+
CMD curl -f http://localhost:7860/api/markets || exit 1
|
| 63 |
+
|
| 64 |
CMD ["/app/start.sh"]
|
Dockerfile.fullstack
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Multi-stage Dockerfile for WagerKit (Hugging Face Spaces Compatible)
|
| 2 |
+
|
| 3 |
+
# Stage 1: Build Backend
|
| 4 |
+
FROM node:20-alpine AS backend-build
|
| 5 |
+
|
| 6 |
+
WORKDIR /app/backend
|
| 7 |
+
|
| 8 |
+
# Copy backend package files
|
| 9 |
+
COPY backend/package*.json ./
|
| 10 |
+
RUN npm ci --only=production
|
| 11 |
+
|
| 12 |
+
# Copy backend source and build
|
| 13 |
+
COPY backend ./
|
| 14 |
+
RUN npm run build
|
| 15 |
+
|
| 16 |
+
# Stage 2: Build Frontend
|
| 17 |
+
FROM node:20-alpine AS frontend-build
|
| 18 |
+
|
| 19 |
+
WORKDIR /app/frontend
|
| 20 |
+
|
| 21 |
+
# Copy frontend package files
|
| 22 |
+
COPY frontend/package*.json ./
|
| 23 |
+
RUN npm ci
|
| 24 |
+
|
| 25 |
+
# Copy frontend source
|
| 26 |
+
COPY frontend ./
|
| 27 |
+
|
| 28 |
+
# Build Next.js frontend (standalone mode)
|
| 29 |
+
ENV NEXT_PUBLIC_API_URL=/api
|
| 30 |
+
RUN npm run build
|
| 31 |
+
|
| 32 |
+
# Stage 3: Production Runtime
|
| 33 |
+
FROM node:20-alpine
|
| 34 |
+
|
| 35 |
+
WORKDIR /app
|
| 36 |
+
|
| 37 |
+
# Install dependencies for Redis and process management
|
| 38 |
+
RUN apk add --no-cache redis bash
|
| 39 |
+
|
| 40 |
+
# Copy backend production files
|
| 41 |
+
COPY --from=backend-build /app/backend/dist ./backend/dist
|
| 42 |
+
COPY --from=backend-build /app/backend/node_modules ./backend/node_modules
|
| 43 |
+
COPY --from=backend-build /app/backend/package.json ./backend/
|
| 44 |
+
|
| 45 |
+
# Copy backend .env (if exists)
|
| 46 |
+
COPY backend/.env ./backend/.env 2>/dev/null || true
|
| 47 |
+
|
| 48 |
+
# Copy frontend production files (standalone build)
|
| 49 |
+
COPY --from=frontend-build /app/frontend/.next/standalone ./frontend/
|
| 50 |
+
COPY --from=frontend-build /app/frontend/.next/static ./frontend/.next/static
|
| 51 |
+
COPY --from=frontend-build /app/frontend/public ./frontend/public
|
| 52 |
+
COPY --from=frontend-build /app/frontend/package.json ./frontend/
|
| 53 |
+
|
| 54 |
+
# Environment variables for Hugging Face Spaces
|
| 55 |
+
ENV NODE_ENV=production
|
| 56 |
+
ENV PORT=7860
|
| 57 |
+
ENV REDIS_HOST=127.0.0.1
|
| 58 |
+
ENV REDIS_PORT=6379
|
| 59 |
+
ENV NEXT_PUBLIC_API_URL=/api
|
| 60 |
+
|
| 61 |
+
# Expose Hugging Face Spaces default port
|
| 62 |
+
EXPOSE 7860
|
| 63 |
+
|
| 64 |
+
# Create comprehensive startup script
|
| 65 |
+
RUN cat > /app/start.sh << 'EOF'
|
| 66 |
+
#!/bin/bash
|
| 67 |
+
set -e
|
| 68 |
+
|
| 69 |
+
echo "π Starting WagerKit on Hugging Face Spaces..."
|
| 70 |
+
|
| 71 |
+
# Start Redis in background
|
| 72 |
+
echo "π¦ Starting Redis..."
|
| 73 |
+
redis-server --daemonize yes --bind 127.0.0.1 --port 6379 --loglevel warning
|
| 74 |
+
sleep 2
|
| 75 |
+
|
| 76 |
+
# Verify Redis is running
|
| 77 |
+
redis-cli ping > /dev/null
|
| 78 |
+
echo "β
Redis is ready"
|
| 79 |
+
|
| 80 |
+
# Start Backend in background
|
| 81 |
+
echo "π§ Starting NestJS backend..."
|
| 82 |
+
cd /app/backend
|
| 83 |
+
PORT=3001 node dist/main.js &
|
| 84 |
+
BACKEND_PID=$!
|
| 85 |
+
sleep 3
|
| 86 |
+
echo "β
Backend started (PID: $BACKEND_PID)"
|
| 87 |
+
|
| 88 |
+
# Start Frontend (foreground - keeps container alive)
|
| 89 |
+
echo "π Starting Next.js frontend on port 7860..."
|
| 90 |
+
cd /app/frontend
|
| 91 |
+
PORT=7860 node server.js
|
| 92 |
+
|
| 93 |
+
EOF
|
| 94 |
+
|
| 95 |
+
RUN chmod +x /app/start.sh
|
| 96 |
+
|
| 97 |
+
CMD ["/app/start.sh"]
|
backend/src/main.ts
CHANGED
|
@@ -3,12 +3,23 @@ import { AppModule } from './app.module';
|
|
| 3 |
|
| 4 |
async function bootstrap() {
|
| 5 |
const app = await NestFactory.create(AppModule);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
app.enableCors({
|
| 7 |
-
origin:
|
| 8 |
credentials: true,
|
| 9 |
});
|
|
|
|
| 10 |
app.setGlobalPrefix('api');
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
}
|
| 14 |
bootstrap();
|
|
|
|
| 3 |
|
| 4 |
async function bootstrap() {
|
| 5 |
const app = await NestFactory.create(AppModule);
|
| 6 |
+
|
| 7 |
+
// Configure CORS for both local dev and HF Spaces
|
| 8 |
+
const allowedOrigins = [
|
| 9 |
+
'http://localhost:3000',
|
| 10 |
+
'http://localhost:7860',
|
| 11 |
+
/\.hf\.space$/, // Allow all HF Spaces domains
|
| 12 |
+
];
|
| 13 |
+
|
| 14 |
app.enableCors({
|
| 15 |
+
origin: allowedOrigins,
|
| 16 |
credentials: true,
|
| 17 |
});
|
| 18 |
+
|
| 19 |
app.setGlobalPrefix('api');
|
| 20 |
+
|
| 21 |
+
const port = process.env.PORT || 3001;
|
| 22 |
+
await app.listen(port);
|
| 23 |
+
console.log(`WagerKit API running on http://localhost:${port}`);
|
| 24 |
}
|
| 25 |
bootstrap();
|