remove nginx, serve ui from api itself

This commit is contained in:
pratik
2025-10-15 14:22:44 -05:00
parent c5b85126c0
commit b8cabbe0c8
7 changed files with 91 additions and 45 deletions

View File

@@ -1,5 +1,7 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from app.api.artifacts import router as artifacts_router
from app.api.seed import router as seed_router
from app.api.tags import router as tags_router
@@ -7,6 +9,7 @@ from app.database import init_db
from app.config import settings
import logging
import os
from pathlib import Path
# Configure logging
logging.basicConfig(
@@ -39,7 +42,8 @@ app.include_router(artifacts_router)
app.include_router(seed_router)
app.include_router(tags_router)
# Note: Frontend is now served separately as an Angular application
# Static files configuration - will be set up after routes
static_dir = Path("/app/static")
@app.on_event("startup")
@@ -65,16 +69,20 @@ async def api_root():
@app.get("/")
async def ui_root():
"""API root - Frontend is served separately"""
return {
"message": "Test Artifact Data Lake API",
"version": "1.0.0",
"docs": "/docs",
"frontend": "Frontend is served separately on port 4200 (development) or via reverse proxy (production)",
"deployment_mode": settings.deployment_mode,
"storage_backend": settings.storage_backend
}
async def serve_angular_app():
"""Serve Angular app index.html"""
static_dir = Path("/app/static")
if static_dir.exists():
return FileResponse(static_dir / "index.html")
else:
# Fallback if static files not found
return {
"message": "Test Artifact Data Lake API",
"version": "1.0.0",
"docs": "/docs",
"deployment_mode": settings.deployment_mode,
"storage_backend": settings.storage_backend
}
@app.get("/health")
@@ -83,6 +91,31 @@ async def health_check():
return {"status": "healthy"}
# Catch-all route for Angular client-side routing
# This must be last to not interfere with API routes
@app.get("/{full_path:path}")
async def catch_all(full_path: str):
"""Serve Angular app for all non-API routes (SPA routing)"""
if static_dir.exists():
# Check if the requested path is a file in the static directory
file_path = static_dir / full_path
if file_path.is_file() and file_path.exists():
# Determine media type based on file extension
media_type = None
if file_path.suffix == ".js":
media_type = "application/javascript"
elif file_path.suffix == ".css":
media_type = "text/css"
elif file_path.suffix in [".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico"]:
media_type = f"image/{file_path.suffix[1:]}"
return FileResponse(file_path, media_type=media_type)
# Otherwise, serve index.html for client-side routing
index_path = static_dir / "index.html"
if index_path.exists():
return FileResponse(index_path, media_type="text/html")
return {"error": "Static files not found"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(