90 lines
3.4 KiB
Python
90 lines
3.4 KiB
Python
# app/main.py
|
|
import logging
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.api_router import api_router # Import the main combined router
|
|
# Import database and models if needed for startup/shutdown events later
|
|
# from . import database, models
|
|
|
|
# --- Logging Setup ---
|
|
# Configure logging (can be more sophisticated later, e.g., using logging.yaml)
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# --- FastAPI App Instance ---
|
|
app = FastAPI(
|
|
title="Shared Lists API",
|
|
description="API for managing shared shopping lists, OCR, and cost splitting.",
|
|
version="0.1.0",
|
|
openapi_url="/api/openapi.json", # Place OpenAPI spec under /api
|
|
docs_url="/api/docs", # Place Swagger UI under /api
|
|
redoc_url="/api/redoc" # Place ReDoc under /api
|
|
)
|
|
|
|
# --- CORS Middleware ---
|
|
# Define allowed origins. Be specific in production!
|
|
# Use ["*"] for wide open access during early development if needed,
|
|
# but restrict it as soon as possible.
|
|
# SvelteKit default dev port is 5173
|
|
origins = [
|
|
"http://localhost:5174",
|
|
"http://localhost:8000", # Allow requests from the API itself (e.g., Swagger UI)
|
|
# Add your deployed frontend URL here later
|
|
# "https://your-frontend-domain.com",
|
|
]
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins, # List of origins that are allowed to make requests
|
|
allow_credentials=True, # Allow cookies to be included in requests
|
|
allow_methods=["*"], # Allow all methods (GET, POST, PUT, DELETE, etc.)
|
|
allow_headers=["*"], # Allow all headers
|
|
)
|
|
# --- End CORS Middleware ---
|
|
|
|
|
|
# --- Include API Routers ---
|
|
# All API endpoints will be prefixed with /api
|
|
app.include_router(api_router, prefix="/api")
|
|
# --- End Include API Routers ---
|
|
|
|
|
|
# --- Root Endpoint (Optional - outside the main API structure) ---
|
|
@app.get("/", tags=["Root"])
|
|
async def read_root():
|
|
"""
|
|
Provides a simple welcome message at the root path.
|
|
Useful for basic reachability checks.
|
|
"""
|
|
logger.info("Root endpoint '/' accessed.")
|
|
# You could redirect to the docs or return a simple message
|
|
# from fastapi.responses import RedirectResponse
|
|
# return RedirectResponse(url="/api/docs")
|
|
return {"message": "Welcome to the Shared Lists API! Docs available at /api/docs"}
|
|
# --- End Root Endpoint ---
|
|
|
|
|
|
# --- Application Startup/Shutdown Events (Optional) ---
|
|
# @app.on_event("startup")
|
|
# async def startup_event():
|
|
# logger.info("Application startup: Connecting to database...")
|
|
# # You might perform initial checks or warm-up here
|
|
# # await database.engine.connect() # Example check (get_db handles sessions per request)
|
|
# logger.info("Application startup complete.")
|
|
|
|
# @app.on_event("shutdown")
|
|
# async def shutdown_event():
|
|
# logger.info("Application shutdown: Disconnecting from database...")
|
|
# # await database.engine.dispose() # Close connection pool
|
|
# logger.info("Application shutdown complete.")
|
|
# --- End Events ---
|
|
|
|
|
|
# --- Direct Run (for simple local testing if needed) ---
|
|
# It's better to use `uvicorn app.main:app --reload` from the terminal
|
|
# if __name__ == "__main__":
|
|
# logger.info("Starting Uvicorn server directly from main.py")
|
|
# uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
# ------------------------------------------------------ |