91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
"""
|
|
Daily Journal Prompt Generator - FastAPI Backend
|
|
Main application entry point
|
|
"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
|
|
from app.api.v1.api import api_router
|
|
from app.core.config import settings
|
|
from app.core.logging import setup_logging
|
|
from app.core.exception_handlers import setup_exception_handlers
|
|
|
|
# Setup logging
|
|
logger = setup_logging()
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Lifespan context manager for startup and shutdown events."""
|
|
# Startup
|
|
logger.info("Starting Daily Journal Prompt Generator API")
|
|
logger.info(f"Environment: {settings.ENVIRONMENT}")
|
|
logger.info(f"Debug mode: {settings.DEBUG}")
|
|
|
|
# Create data directory if it doesn't exist
|
|
data_dir = Path(settings.DATA_DIR)
|
|
data_dir.mkdir(exist_ok=True)
|
|
logger.info(f"Data directory: {data_dir.absolute()}")
|
|
|
|
yield
|
|
|
|
# Shutdown
|
|
logger.info("Shutting down Daily Journal Prompt Generator API")
|
|
|
|
# Create FastAPI app
|
|
app = FastAPI(
|
|
title="Daily Journal Prompt Generator API",
|
|
description="API for generating and managing journal writing prompts",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# Setup exception handlers
|
|
setup_exception_handlers(app)
|
|
|
|
# Configure CORS
|
|
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 API router
|
|
app.include_router(api_router, prefix="/api/v1")
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Root endpoint with API information."""
|
|
return {
|
|
"name": "Daily Journal Prompt Generator API",
|
|
"version": "1.0.0",
|
|
"description": "API for generating and managing journal writing prompts",
|
|
"docs": "/docs",
|
|
"redoc": "/redoc",
|
|
"health": "/health"
|
|
}
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""Health check endpoint."""
|
|
return {"status": "healthy", "service": "daily-journal-prompt-api"}
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"main:app",
|
|
host=settings.HOST,
|
|
port=settings.PORT,
|
|
reload=settings.DEBUG,
|
|
log_level="info"
|
|
)
|
|
|