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


app = Autowired(cls=FastAPI)

# Add middleware

from fastapi.middleware.cors import CORSMiddleware
    
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Add global exception handler
# Custom exception handling
@app.exception_handler(AssertionError)
async def assert_exception_handler(request, exc):
    ...

# Add route method
@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"


@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
@app.get('/serviceA')
async def serviceA():
    serviceA = Autowired(cls=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.3.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.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for seatools_starter_web_fastapi-1.0.3.tar.gz
Algorithm Hash digest
SHA256 83006bc4dc483bd8c2e744b339966ae53de9b8536b8b290ee450d3959c8f17f8
MD5 d86923c281c624f4594a84861a5fb1ce
BLAKE2b-256 82a8e62063b944e9855e00300ad66233e07b8987d5147b8866c5f5b0c5f58946

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for seatools_starter_web_fastapi-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f2dcb86700dc70b60fc07cb4abe4f2ded6761e27f13aa584e644efad9ec3ed7b
MD5 67267a5050fca6780ad3420de5ba8d63
BLAKE2b-256 5ea8f0b73c70a9f7a2ef673ba7ae336c8d5f94ba06a0a0c87038d9160bb88225

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