morethanadiagnosis-hub/backend/app/main.py
admin 078ed376eb feat(backend): implement 7-MVP FastAPI backend with all models and API stubs
Implements complete FastAPI backend infrastructure for MoreThanADiagnosis with:

Core Infrastructure:
- FastAPI application with CORS, error handling, health checks
- SQLAlchemy ORM with PostgreSQL support
- Pydantic configuration management
- Docker & Docker Compose for production deployment

Database Models (7 MVPs + Auth):
- User, Profile, Role, Consent (identity)
- RefreshToken, AuthAuditLog (authentication)
- ForumCategory, ForumThread, ForumPost, ForumReaction, ForumReport (forum)
- BlogPost (blog)
- PodcastEpisode (podcast)
- Resource (resources)
- TributeEntry (tribute)
- MerchProduct, Order, OrderItem (merch)

API Endpoints (Alphabetical MVPs):
- /api/v1/blog - Blog posts (list, get)
- /api/v1/forum - Categories, threads, posts, reactions, reports
- /api/v1/merch - Products, orders
- /api/v1/podcast - Episodes
- /api/v1/profiles - User profiles
- /api/v1/resources - Knowledge base
- /api/v1/tribute - Memorials
- /api/v1/health - Health checks

Configuration & Deployment:
- .env.example for configuration
- Dockerfile with multi-stage build
- docker-compose.yml for PostgreSQL + Redis + API
- Production-ready on nexus-vector with port 8000
- Non-root user, health checks, security best practices

Dependencies:
- FastAPI, SQLAlchemy, Pydantic
- PostgreSQL, Redis
- Testing (pytest), Security (passlib, python-jose)
- Full requirements.txt with 30+ packages

Status: Foundation complete, MVP endpoint stubs ready
Next: Database migrations, authentication implementation

Job ID: MTAD-IMPL-2025-11-18-CL

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 00:46:43 +00:00

88 lines
2.1 KiB
Python

"""
FastAPI main application entry point.
Job ID: MTAD-IMPL-2025-11-18-CL
"""
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.config import settings
from app.database import init_db, close_db
from app.api.v1 import api_router
# Configure logging
logging.basicConfig(
level=settings.log_level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for startup and shutdown events."""
# Startup
logger.info("Starting MoreThanADiagnosis API")
await init_db()
logger.info("Database initialized")
yield
# Shutdown
logger.info("Shutting down MoreThanADiagnosis API")
await close_db()
logger.info("Database connections closed")
# Create FastAPI app
app = FastAPI(
title=settings.api_title,
description=settings.api_description,
version=settings.api_version,
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=settings.cors_credentials,
allow_methods=settings.cors_methods,
allow_headers=settings.cors_headers,
)
# Include API routes
app.include_router(api_router, prefix=f"/api/{settings.api_version}")
# Health check endpoint
@app.get("/health", tags=["Health"])
async def health_check():
"""Health check endpoint for uptime monitoring."""
return {
"status": "healthy",
"version": settings.api_version,
"env": settings.env,
}
@app.get("/", tags=["Root"])
async def root():
"""Root endpoint."""
return {
"service": settings.api_title,
"version": settings.api_version,
"docs": "/docs",
"redoc": "/redoc",
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"app.main:app",
host="0.0.0.0",
port=8000,
reload=settings.debug,
log_level=settings.log_level.lower(),
)