uvify / server.py
aviol's picture
Initial commit for uvify UI
d51321a
#!/usr/bin/env python3
"""
Production server for uvify HuggingFace deployment
Serves both React frontend and FastAPI backend together
"""
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import uvify
# Create a new FastAPI app that combines everything
app = FastAPI(
title="Uvify",
description="Analyze GitHub repositories to generate uv commands",
version="1.0.0"
)
# Add CORS middleware for HuggingFace deployment
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins for HuggingFace
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount the uvify API under /api prefix to avoid conflicts
app.mount("/api", uvify.api, name="uvify_api")
# Mount static files for the built React app
app.mount("/static", StaticFiles(directory="static"), name="static")
# Serve the React app for the root route
@app.get("/")
async def serve_index():
"""Serve the React app index page"""
return FileResponse("static/index.html")
# Serve the React app for all other frontend routes
@app.get("/{path:path}")
async def serve_frontend(path: str):
"""Serve static files or fallback to React app for client-side routing"""
# Skip API routes - they're handled by the mounted API
if path.startswith("api/"):
return {"error": "API route not found"}
# Try to serve static file first (CSS, JS, images, etc.)
file_path = f"static/{path}"
if os.path.exists(file_path) and os.path.isfile(file_path):
return FileResponse(file_path)
# Fallback to React app index.html for client-side routing
return FileResponse("static/index.html")
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", 7860))
uvicorn.run(app, host="0.0.0.0", port=port)