
- Modified database connection to use project-relative paths - Updated Alembic configuration to dynamically use the SQLAlchemy URL - Added a root endpoint for easy testing - Fixed imports in migration environment
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
import os
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
|
|
|
|
from app.api.api import api_router
|
|
from app.core.config import settings
|
|
from app.db.database import Base, engine
|
|
|
|
# Create the database tables if they don't exist
|
|
# In production, you'd use Alembic migrations instead
|
|
if os.environ.get("ENVIRONMENT") != "production":
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
|
docs_url=None, # We'll define custom endpoints for docs
|
|
redoc_url=None,
|
|
)
|
|
|
|
# Set up CORS middleware
|
|
if settings.BACKEND_CORS_ORIGINS:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include our router
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
# Root endpoint for quick testing
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Welcome to the Generic REST API Service. Visit /docs for documentation."}
|
|
|
|
# Custom documentation endpoints
|
|
@app.get("/docs", include_in_schema=False)
|
|
async def custom_swagger_ui_html():
|
|
return get_swagger_ui_html(
|
|
openapi_url=app.openapi_url,
|
|
title=app.title + " - Swagger UI",
|
|
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
|
|
)
|
|
|
|
|
|
@app.get("/redoc", include_in_schema=False)
|
|
async def redoc_html():
|
|
return get_redoc_html(
|
|
openapi_url=app.openapi_url,
|
|
title=app.title + " - ReDoc",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |