Skip to main content

A web framework base on Sanic

Project description

sanic Framework boot | sanic-boot

sanic-boot

sanic-boot 基于 Sanic 框架,为开发者提供与 Spring Boot 类似的装饰器,便于路由系统、模型类的自动装配与配置。

Sanic | Build fast. Run fast.

Sanic is a Python 3.9+ web server and web framework that's written to go fast. It allows the usage of the async/await syntax added in Python 3.5, which makes your code non-blocking and speedy.

注意

本框架没有对原生的 sanic 框架造成任何侵入性的修改,但由于模型(Models)、路由(Routers)、任务(Task)等自动加载实现机制较为笨拙,可能会对 sanic 的网络请求造成不可预估的影响。

TODO List

  • 自动装配基础路由
  • 自动装配类视图路由
  • 自动装配 Background Task
  • 自动装配模型类
  • 文档
  • Websocket
  • 流式传输(Streaming)
  • 为 Controller 注入 CRUD 操作
  • 封装 APScheduler 定时任务框架
  • 更符合人性化、更规范的异常处理

安装

# 使用 pip
pip install sanic-boot

# 使用uv
uv add sanic-boot

可能的依赖

# 使用 pip
pip install sanic sanic-ext tortoise-orm
# 或
pip install sanic[ext] tortoise-orm[asyncpg|psycopg|asyncmy|asyncodbc]

# 使用 uv
uv add ...

sanic-ext 提供 docs、cors 等; tortoise-orm 提供 ORM 等。

Hello World Example

from sanic_boot import sanicBoot
import sanic

app = sanicBoot(__name__)


# 使用 sanic 自带的装饰器仍然有效
@app.get("/")
def index(request):
    return sanic.response.text("hello")


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8088, debug=True, auto_reload=True)

1. Create NORMAL route by common function (which can be with async)

from sanic import Request, text
from sanic_boot import GetMapping


# 面向函数
@GetMapping('/index')
async def index(request: Request):
    return text('Hello')

2. Create Router View from class, which just collect the route -- A simple method instead of blueprint

from sanic import Request, text, json
from sanic_boot import Controller, GetMapping, RequestMapping


# 面向类
@Controller('/person')
class PersonController:
    # 使用 RequestMapping 装饰器后,若发现目标是类且不是 HTTPMethodView 的子类,则会在继承链中添加 HTTPMethodView 类。
    # (*注意*:此举可能会造成多继承冲突!!!)
    @RequestMapping('/login')
    class Login:
        async def get(self, request: Request):
            return text('the route is /person/login')

    # 除视图外,还可以使用普通方法构造路由
    @GetMapping('/logout')
    async def logout(self, request: Request):
        return json({
            'code': 200,
            'data': '退出登录'
        })

3. Create Task by decorator Task, which is allowed to get a parameter named name or

taskName. If there is no parameter has, the value None will be the default value assigned to argument name

import asyncio
from sanic import Sanic
from sanic_boot import Task


@Task(name='')  # name 或 taskName 默认为 None
async def gen(app: Sanic):
    while True:
        print([item for item in range(10)])
        await asyncio.sleep(5000)

Decorator Description

  • @Controller (uri : str = '')

    装饰器 Controller 意在收集目标注解对象内部注册的路由,是 sanic 框架中 blueprint 蓝图系统的自动化实现。

    sanic 官方网站中提供了自动收集 blueprint 的辅助文件,能够帮助开发者给予蓝图系统快速构建路由系统。而装饰器 Controller 则延伸了官方方案,参考 Spring Boot 装饰器思路,提供能够相对完备的路由系统自动组装方案。

    装饰器 Controller允许不携带任何参数,此时将自动分配路由,分配的路由与目标注解的类名有关,类名推荐的格式为 ^[A-Z][A-Za-z0-9]*Controller$ ,(即大写字母开头的有意义单词,并以单词 Controller 结尾),分配所得路由为全小写且不含 Controller。如 TestController 的路由为 testTestTest2Controller 的路由为 test-test2

    注意: 若最终注册的路由存在重复将会造成路由冲突,此外,若注册的函数名在相同层级上存在重复,也会造成路由系统收集异常。故建议开发者在不同业务中为路由句柄(handler)赋予不同的名称

  • @RequestMapping(uri : str, methods: list[RequestMethod] | RequestMethod | None = None)

    使用 RequestMapping 时若注解对象是一个视图类,则第二个参数可缺省,sanic 内部的路由系统将会根据视图类的方法自动分配路由方法。装饰器 RequestMappingsanic 框架中类视图注册方法 app.add_route (Class.as_view(), ...) 的自动化实现。

  • @[Get|Post|Head|Put|Delete|Patch|Options]Mapping(uri : str)

    @GetMapping (uri : str) 等装饰器是 @RequestMapping ('/login', RequestMethod.GET) 的简便写法。需要注意的是这些简便写法尽量不要应用在视图类上。但基于实际的代码逻辑实现而言,即使对视图类上进行应用,也不会造成任何不可预计的后果。

  • @Task(name : str = None)

    在 Tasks 目录下注册任务事件。目前尚未想好如何以最佳的形式将 APScheduler 封装进去,故开发者可根据自己需要进行封装。

  • @CRUD.crud(model: type[tortoise.Model], baseUri: str, flag : int = 0, createName: str = 'create', readName: str = 'query', updateName: str = 'modify', deleteName: str = 'delete')

    使用 CRUD 对基础操作进行统一配置,是对 @CRUD.[create|read|update|delete](...) 的快捷书写形式。CRUD装饰器仅可对控制器Controller使用。

    • model : type[tortoise.Model] 该参数必须是 Model 类的子类,不能为空,后续所有操作都需要使用 model 类操作数据库记录

    • baseUri : str 针对基础操作的基础路由

    例如 baseUri = 'person',则生成的路由分别是

    1. /person/create
    2. /person/query
    3. /person/modify (故意使用 modify 而非 update
    4. /person/delete
    • flag : int 对四个基本操作分别使用1个二进制位表示,CRUD 按顺序每位取 1 表示激活。为避免误操作,默认为 0,即均不激活。

    • createName: str = 'create', readName: str = 'query', updateName: str = 'modify' 当且仅当 CRUD 的 flag 取值中对应的基本操作激活时(位上为 1)可对有效的CRUD操作的路由进行自定义命名。

    例:当设置 flag = CRUD.Flag.R | CRUD.Flag.D

    若此时对所有的 [create|read|update|delete]Name 设置自定义值,仅 ReadNameDeleteName 有效。

  • @CRUD.[create|read|update|delete](model: type[tortoise.Model], uri: str = '', baseUri: str = '')

    注意: 由于参数 model 除类型必须是 {ORM}.Model 模型类的子类这一类型约束外,没有任何赋值上的限制,故而理论上可以在任意类中使用CRUD 的相关装饰器生成数据库基本操作,因此强烈建议不要在相关的接口中使用CRUD装饰生成其他模型的CRUD接口。此外,CRUD 装饰器生成的接口仅是基本的数据库操作,对于复杂的关联性操作,请自定义接口进行实现。

    提醒:查询装饰器select仅支持两种查询方式:select ... from ... where <field-name> = <field-value> 以及 select ... from ...where <field-name> in (...),多个字段之间使用 逻辑。

    在使用 CRUD 装饰器时,允许开发者对运行时的操作数据进行干预。

    1. create
    1. before_create 创建数据实例之前
    2. create 创建数据实例并获取数据的字典(dict)之后
    1. select
    1. select_condition 收集条件之后
    2. selected 查询数据之后
    1. update
    1. before_update 收集完更新字段之后,更新数据之前
    2. updated 更新数据记录并获取数据的字典(dict)之后
    1. delete
    1. deleted 删除数据之后

    注意delete 删除数据库记录的操作最好自定义实现,除非你确定要实际删除指定的数据记录 以上所有函数都会将request和condition/data传入供自定义逻辑使用。

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

sanic_boot-0.0.4.2.tar.gz (52.1 kB view details)

Uploaded Source

Built Distribution

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

sanic_boot-0.0.4.2-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file sanic_boot-0.0.4.2.tar.gz.

File metadata

  • Download URL: sanic_boot-0.0.4.2.tar.gz
  • Upload date:
  • Size: 52.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.19

File hashes

Hashes for sanic_boot-0.0.4.2.tar.gz
Algorithm Hash digest
SHA256 86ce83a7f897ef68dfe3441d4949793ffa95faf729e13b1dd2dd26fc451a8c7b
MD5 0bb14dce18c89ded6c8f7a89fbe438b2
BLAKE2b-256 14291a2e4835d99aee2f549de570cf98fc306e74bcc62f6579f0eeaa47d82b3d

See more details on using hashes here.

File details

Details for the file sanic_boot-0.0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sanic_boot-0.0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 792a0163050f2b82ec142cbdebadf26964375da34bf296bbc781f6c96f4910da
MD5 9df0d82084901c539a5257bea987a397
BLAKE2b-256 67160cb2c434095ff4b41f04e1775013f1c01562d8be1afd72e5c6ad554e9ddb

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