90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
from fastapi import FastAPI, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import FileResponse
|
|
from contextlib import asynccontextmanager
|
|
import os
|
|
|
|
from .config import settings
|
|
from .database import connect_db, close_db, get_database
|
|
from .routes import users, drinks, transactions, auth
|
|
from .routes.auth import _normalize_email, _hash_password
|
|
|
|
async def ensure_default_admin():
|
|
db = get_database()
|
|
email = _normalize_email(settings.admin_email)
|
|
existing = await db.users.find_one({"email": email, "isAdmin": True})
|
|
if existing:
|
|
return
|
|
|
|
admin_user = {
|
|
"firstname": "Admin",
|
|
"lastname": "Team",
|
|
"email": email,
|
|
"isAdmin": True,
|
|
"balance": 0.0,
|
|
"password_hash": _hash_password(settings.admin_password)
|
|
}
|
|
await db.users.insert_one(admin_user)
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Connect to MongoDB on startup
|
|
connect_db()
|
|
await ensure_default_admin()
|
|
yield
|
|
# Close connection on shutdown
|
|
close_db()
|
|
|
|
app = FastAPI(
|
|
title="Coffee Control Center API",
|
|
description="Backend API for managing university chair beverage inventory and user balances",
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# Enable CORS for frontend integration
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # Adjust for production if needed
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Register routes
|
|
app.include_router(users.router, prefix="/api")
|
|
app.include_router(drinks.router, prefix="/api")
|
|
app.include_router(transactions.router, prefix="/api")
|
|
app.include_router(auth.router, prefix="/api")
|
|
|
|
@app.get("/api/health")
|
|
async def health_check():
|
|
return {"status": "healthy", "service": "coffee-control-center-api"}
|
|
|
|
# Serve Frontend static files in production if built
|
|
# __file__ is /app/app/main.py → go up 2 levels to reach /app
|
|
_APP_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
FRONTEND_DIR = os.path.join(_APP_ROOT, "frontend", "dist", "coffee-control-center", "browser")
|
|
|
|
# Docker fallback: files are placed at /app/frontend/dist/... by the Dockerfile
|
|
if not os.path.exists(FRONTEND_DIR):
|
|
FRONTEND_DIR = "/app/frontend/dist/coffee-control-center/browser"
|
|
|
|
if os.path.exists(FRONTEND_DIR):
|
|
@app.get("/{fallback_path:path}")
|
|
async def serve_frontend(fallback_path: str):
|
|
if fallback_path.startswith("api"):
|
|
raise HTTPException(status_code=404, detail="API route not found")
|
|
|
|
file_path = os.path.join(FRONTEND_DIR, fallback_path)
|
|
if os.path.isfile(file_path):
|
|
return FileResponse(file_path)
|
|
|
|
index_path = os.path.join(FRONTEND_DIR, "index.html")
|
|
if os.path.isfile(index_path):
|
|
return FileResponse(index_path)
|
|
|
|
raise HTTPException(status_code=404, detail="Frontend file not found")
|
|
|