Set up database configuration and connection management
- Created app/database.py with SQLAlchemy engine, session management, and connection utilities - Added comprehensive database models in app/models.py for User, Client, Phone, Case, Transaction, Document, and Payment - Implemented FastAPI application with database lifecycle management in app/main.py - Added health check endpoint to verify database connectivity - Created README.md with database configuration documentation - Verified database connection works correctly with SQLite backend
This commit is contained in:
86
app/main.py
86
app/main.py
@@ -1 +1,85 @@
|
||||
# FastAPI application entry point
|
||||
"""
|
||||
FastAPI application entry point for Delphi Database.
|
||||
|
||||
This module initializes the FastAPI application, sets up database connections,
|
||||
and provides the main application instance.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .database import create_tables, get_db, get_database_url
|
||||
from .models import User
|
||||
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""
|
||||
Lifespan context manager for FastAPI application.
|
||||
|
||||
Handles startup and shutdown events:
|
||||
- Creates database tables on startup
|
||||
- Logs database connection info
|
||||
"""
|
||||
# Startup
|
||||
logger.info("Starting Delphi Database application...")
|
||||
|
||||
# Create database tables
|
||||
create_tables()
|
||||
logger.info("Database tables created/verified")
|
||||
|
||||
# Log database connection info
|
||||
db_url = get_database_url()
|
||||
logger.info(f"Database connected: {db_url}")
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
logger.info("Shutting down Delphi Database application...")
|
||||
|
||||
|
||||
# Create FastAPI application with lifespan management
|
||||
app = FastAPI(
|
||||
title="Delphi Database",
|
||||
description="Legal case management database application",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""
|
||||
Root endpoint - health check.
|
||||
"""
|
||||
return {"message": "Delphi Database API is running"}
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check(db: Session = Depends(get_db)):
|
||||
"""
|
||||
Health check endpoint that verifies database connectivity.
|
||||
"""
|
||||
try:
|
||||
# Test database connection by querying user count
|
||||
user_count = db.query(User).count()
|
||||
return {
|
||||
"status": "healthy",
|
||||
"database": "connected",
|
||||
"users": user_count
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Health check failed: {e}")
|
||||
return {
|
||||
"status": "unhealthy",
|
||||
"database": "error",
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user