【通用消息通知服务】0x2 - 初始化服务

【通用消息通知服务】0x2 - 初始化服务

项目地址: A generic message notification system[Github]

准备依赖

[tool.poetry.dependencies]
python = "^3.10"
sanic = {extras = ["http3"], version = "^23.3.0"}
tortoise-orm = "^0.19.3"
pydantic = {extras = ["email"], version = "^2.0.3"}
aio-pika = "^9.1.4"
grpcio = "^1.56.0"
redis = "^4.6.0"
orjson = "^3.9.2"
environ-config = "^23.2.0"
aiohttp = "^3.8.4"
python-ulid = "^1.1.0"
sanic-ext = "^23.3.0"
aioquic = "^0.9.21"
mongoengine = "^0.27.0"
motor = "^3.2.0"
umongo = "^3.1.0"
asgiref = "^3.7.2"
uvicorn = "^0.23.2"
redis-om = "^0.2.1"
sanic-pydantic = "^1.3.1"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.3.3"
black = "^23.7.0"
isort = "^5.12.0"
flake8 = "^6.0.0"
trustme = "^1.1.0"


[tool.poetry.group.test.dependencies]
pytest = "^7.4.0"

编写服务端入口

# server.py

import orjson
from sanic import Sanic
from sanic import text

app = Sanic(
    name="Message",
    strict_slashes=False, # /a/和/a都可以匹配到/a上
    dumps=orjson.dumps, # 替换一下sanic的json库, 加速一下序列化和反序列化
    loads=orjson.loads,
)

@app.get("/")
async def index(request):
	return text("ok")

运行服务

sanic server:app --port 8000

总结

sanic和fastapi都是优秀的asyncio网络框架, 相比之下fastapi目标是Django,sanic追求的是flask。
fastapi内置足够多的优良特性,对开发者非常友好。但是有时候我就是愿意选复杂的一条路。

你可能感兴趣的:(通用消息通知服务,sanic,python,poetry)