Skip to main content

Seatools Starter Web Fastapi

Project description

Seatools FastAPI Starter

This framework must be used in conjunction with the seatools-starter-server-* packages, using seatools-starter-server-uvicorn as an example here.

中文文档

Usage Guide

  1. Install with poetry add fastapi seatools-starter-server-uvicorn seatools-starter-web-fastapi
  2. Configure config/application.yml as follows:
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
  1. Usage, load by defining ioc container functions
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=["*"],
)

# 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):
        ...

# Add route method
@Bean
def xxx_router(app: FastAPI):
    
    @app.get('/')
    async def hello():
        return "hello"

# Add route
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)

# 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()
    
# 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()
  1. Run, see seatools-starter-server-*, example seatools-starter-server-uvicorn

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seatools_starter_web_fastapi-1.0.2.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

seatools_starter_web_fastapi-1.0.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file seatools_starter_web_fastapi-1.0.2.tar.gz.

File metadata

File hashes

Hashes for seatools_starter_web_fastapi-1.0.2.tar.gz
Algorithm Hash digest
SHA256 70ed40fa1c6b250467ecfc686883041fdc81bc43494045522f50a88d953b5935
MD5 9bc5509536c342e51ae1dcca55e7d93c
BLAKE2b-256 8832d0fcd40f2869d75650dbb04fb1a67df050b3977a2544b7348593a5964e65

See more details on using hashes here.

File details

Details for the file seatools_starter_web_fastapi-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for seatools_starter_web_fastapi-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a2023950d7d80d99499e5d0ef02247ebc5cb335a1ac9c467694b0f7f1d13ccc0
MD5 6a99095a0538c308a90a8516594b0865
BLAKE2b-256 78f6b135625fb2f2d9dc0e1aa682511dcba686a42e73780249b0eb486d4fad90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page