# Chat App - FastAPI + React (HuggingFace Space) from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, JSONResponse from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware import uvicorn import os app = FastAPI(title="Chat App", version="1.0.0") # Enable CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Get the directory where this file is located BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Mount frontend static files frontend_path = os.path.join(BASE_DIR, "frontend") if os.path.exists(frontend_path): app.mount("/frontend", StaticFiles(directory=frontend_path), name="frontend") # API Routes @app.get("/") async def root(request: Request): """Serve the frontend index.html""" index_path = os.path.join(BASE_DIR, "index.html") if os.path.exists(index_path): with open(index_path, "r") as f: content = f.read() return HTMLResponse(content=content) return {"message": "Chat App API", "status": "running"} @app.get("/api/health") async def health(): """Health check endpoint""" return {"status": "healthy", "service": "chat-app"} @app.get("/api/v1") async def api_info(): """API info endpoint""" return { "name": "Chat App API", "version": "1.0.0", "endpoints": { "auth": "/api/v1/auth", "users": "/api/v1/users", "chat": "/api/v1/chat", "groups": "/api/v1/groups" } } # Auth endpoints @app.post("/api/v1/auth/register") async def register(data: dict): return {"message": "Register endpoint - requires database"} @app.post("/api/v1/auth/login") async def login(data: dict): return {"message": "Login endpoint - requires database"} # Chat endpoints @app.get("/api/v1/chat/conversations") async def get_conversations(): return {"conversations": [], "message": "Requires database setup"} @app.post("/api/v1/chat/send") async def send_message(data: dict): return {"message": "Send message - requires database"} # Group endpoints @app.get("/api/v1/groups") async def get_groups(): return {"groups": [], "message": "Requires database setup"} @app.post("/api/v1/groups/create") async def create_group(data: dict): return {"message": "Create group - requires database"} if __name__ == "__main__": port = int(os.environ.get("PORT", 7860)) uvicorn.run(app, host="0.0.0.0", port=port)