FastAPI-Project / app /main.py
abdullah090809's picture
added basic front end
d28ead5
Raw
History Blame Contribute Delete
1.31 kB
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from app.database import Base, engine
from app.routers import post, user, auth, vote
from fastapi.middleware.cors import CORSMiddleware
import os
app = FastAPI()
origins=["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(post.router)
app.include_router(user.router)
app.include_router(auth.router)
app.include_router(vote.router)
@app.get("/api/health")
def home():
return {"message": "FastAPI Project Finished"}
# Serve static files
static_dir = os.path.join(os.path.dirname(__file__), "static")
if os.path.exists(static_dir):
app.mount("/static", StaticFiles(directory=static_dir), name="static")
@app.get("/")
def serve_frontend():
return FileResponse(os.path.join(static_dir, "index.html"))
@app.get("/{full_path:path}")
def serve_spa(full_path: str):
# Don't intercept API routes
if full_path.startswith(("posts", "users", "login", "vote", "api")):
from fastapi import HTTPException
raise HTTPException(status_code=404)
return FileResponse(os.path.join(static_dir, "index.html"))