### Define Basic FastAPI Route with Bean Source: https://github.com/seatools-py/seatools-starter-web-fastapi/blob/master/README.md Illustrates defining a simple GET route (`/`) that returns 'hello' using the `@Bean` decorator. This method integrates the route definition function directly into the FastAPI application via the Seatools IoC container. ```python from seatools.ioc import Autowired, Bean from fastapi import FastAPI # Add route method @Bean def xxx_router(app: FastAPI): @app.get('/') async def hello(): return "hello" ``` -------------------------------- ### Add CORS Middleware to FastAPI Application Source: https://github.com/seatools-py/seatools-starter-web-fastapi/blob/master/README.md Demonstrates how to add a CORS middleware to a FastAPI application using the `@Bean` decorator from `seatools.ioc`. This example configures `CORSMiddleware` to allow all origins, credentials, methods, and headers for broad access. ```python from seatools.ioc import Autowired, Bean from fastapi import FastAPI # Add middleware @Bean def xxx_middleware(app: FastAPI): from fastapi.middleware.cors import CORSMiddleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) ``` -------------------------------- ### Configure FastAPI and Uvicorn with YAML Source: https://github.com/seatools-py/seatools-starter-web-fastapi/blob/master/README.md Illustrates how to configure Uvicorn server parameters (host, port, workers, reload) and FastAPI application settings (title, description, docs_url) using a `config/application.yml` file within the Seatools framework. ```yaml seatools: server: # Here are the uvicorn parameter configurations uvicorn: host: 0.0.0.0 port: 8000 workers: 1 reload: true # Here are the FastAPI configurations fastapi: title: xxxxx description: xxx docs_url: none ``` -------------------------------- ### Inject Services at FastAPI Router Level Source: https://github.com/seatools-py/seatools-starter-web-fastapi/blob/master/README.md Presents an alternative, recommended method for service injection where the service (`ServiceA`) is injected directly into the router function's parameters. This makes the service instance available to all routes defined within that router, simplifying controller signatures. ```python from seatools.ioc import Autowired, Bean from fastapi import FastAPI # Or inject at the router level, this method is more recommended @Bean def a2_router(app: FastAPI, serviceA: ServiceA): # Specific injection method see seatools @app.get('/serviceA') async def serviceA(): return await serviceA.hello() ``` -------------------------------- ### Integrate Custom APIRouter in FastAPI Source: https://github.com/seatools-py/seatools-starter-web-fastapi/blob/master/README.md Demonstrates how to define a separate `APIRouter` with a prefix (`/custom`) and include it in the main FastAPI application using `app.include_router`. The `@Bean` decorator ensures the router registration function is managed by Seatools IoC. ```python from fastapi import APIRouter custom_router = APIRouter(prefix='/custom') @custom_router.get('/') async def custom_hello(): return "custom hello" @Bean def register_custom_router(app: FastAPI): app.include_router(custom_router) ``` -------------------------------- ### Inject Services into FastAPI Controller Parameters Source: https://github.com/seatools-py/seatools-starter-web-fastapi/blob/master/README.md Explains how to inject a service (`ServiceA`) into a FastAPI controller's parameter using `seatools.ioc.Autowired`. It highlights the importance of not explicitly declaring the type for FastAPI compatibility when using `Autowired` in this manner. ```python from seatools.ioc import Autowired, Bean from fastapi import FastAPI # FastAPI integration with seatools ioc injection @Bean class ServiceA: async def hello(self): return "serviceA" @Bean def a_router(app: FastAPI): @app.get('/serviceA') async def serviceA(serviceA = Autowired(cls=ServiceA)): # Inject seatools.ioc in the controller parameter using Autowired, note that the type should not be explicitly declared here, FastAPI will fail on parameter type checking for unsupported types return await serviceA.hello() ``` -------------------------------- ### Implement Global Exception Handler in FastAPI Source: https://github.com/seatools-py/seatools-starter-web-fastapi/blob/master/README.md Shows how to register a global exception handler for `AssertionError` in a FastAPI application. The `@Bean` decorator is used to integrate this custom exception handling function into the application's lifecycle. ```python from seatools.ioc import Autowired, Bean from fastapi import FastAPI # Add global exception handler @Bean def global_exception_handler(app: FastAPI): # Custom exception handling @app.exception_handler(AssertionError) async def assert_exception_handler(request, exc): ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.