todo-full-stack / src /main.py
codeverseinnovation
update files
a7a252e
Raw History Blame Contribute Delete
2.06 kB
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .api.v1.router import api_router
from .config import settings
import structlog
import logging
# Configure structlog
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.JSONRenderer()
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
logger = structlog.get_logger()
# Create FastAPI app
app = FastAPI(
title="Todo API",
description="Full-stack todo application API with authentication",
version="0.1.0",
openapi_url=f"{settings.API_V1_PREFIX}/openapi.json",
docs_url=f"{settings.API_V1_PREFIX}/docs",
redoc_url=f"{settings.API_V1_PREFIX}/redoc",
)
# Add CORS middleware
cors_origins = settings.get_cors_origins
if cors_origins:
app.add_middleware(
CORSMiddleware,
allow_origins=cors_origins,
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=True, # Allow credentials for JWT in cookies
)
# Include API router
app.include_router(api_router, prefix=settings.API_V1_PREFIX)
@app.get("/health")
async def health_check():
"""
Health check endpoint to verify the API is running.
"""
logger.info("Health check endpoint called")
return {"status": "ok", "service": "todo-backend"}
@app.get("/")
async def root():
"""
Root endpoint for the API.
"""
logger.info("Root endpoint called")
return {"message": "Welcome to the Todo API", "version": "0.1.0"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)