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
- Install with
poetry add fastapi seatools-starter-server-uvicorn seatools-starter-web-fastapi - Configure
config/application.ymlas 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
- 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()
- Run, see
seatools-starter-server-*, exampleseatools-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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file seatools_starter_web_fastapi-1.0.3.tar.gz.
File metadata
- Download URL: seatools_starter_web_fastapi-1.0.3.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83006bc4dc483bd8c2e744b339966ae53de9b8536b8b290ee450d3959c8f17f8
|
|
| MD5 |
d86923c281c624f4594a84861a5fb1ce
|
|
| BLAKE2b-256 |
82a8e62063b944e9855e00300ad66233e07b8987d5147b8866c5f5b0c5f58946
|
File details
Details for the file seatools_starter_web_fastapi-1.0.3-py3-none-any.whl.
File metadata
- Download URL: seatools_starter_web_fastapi-1.0.3-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2dcb86700dc70b60fc07cb4abe4f2ded6761e27f13aa584e644efad9ec3ed7b
|
|
| MD5 |
67267a5050fca6780ad3420de5ba8d63
|
|
| BLAKE2b-256 |
5ea8f0b73c70a9f7a2ef673ba7ae336c8d5f94ba06a0a0c87038d9160bb88225
|