non-building checkpoint 1
This commit is contained in:
88
backend/main.py
Normal file
88
backend/main.py
Normal file
@@ -0,0 +1,88 @@
|
||||
"""
|
||||
Daily Journal Prompt Generator - FastAPI Backend
|
||||
Main application entry point
|
||||
"""
|
||||
|
||||
import os
|
||||
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 = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data")
|
||||
os.makedirs(data_dir, exist_ok=True)
|
||||
logger.info(f"Data directory: {data_dir}")
|
||||
|
||||
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" if settings.DEBUG else None,
|
||||
redoc_url="/redoc" if settings.DEBUG else None,
|
||||
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" if settings.DEBUG else None,
|
||||
"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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user