Skip to main content

sanic-framework-boot-logo-400x93.png

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.

安装

pip install sanic-boot

项目目录结构

.
├── main.py               # 项目入口文件
├── pyproject.toml        # 项目工程文件
├── resource              # 配置文件与静态文件
│   └── application.toml  # 配置文件
├── src                   # 源代码文件
│   ├── Application       # 后端应用
│   │   ├── SanicTasks    # 基于 APScheduler 调度的任务
│   │   ├── Services      # 服务逻辑
│   │   ├── Signals       # 信号处理逻辑
│   │   └── Tasks         # 基于 sanic 原生调度的任务
│   └── web               # 前端应用
└── uv.lock

使用

配置【Configureation】

请优先完成配置文件的创建。为保证服务正常启动,请至少完成一下基础配置项的设置。

数据库配置请参考 sanic 官方文档,对 SanicBootApplication 的实例或 实例的 app 属性做配置。

# File : {projectWorkspaceRootDir}/resource/Application.toml

# 基本配置

[Boot.Application]
name = "app"

[Boot.DataSource]
url = ""
username = ""
password = ""

[Server]
host = "0.0.0.0"
port = 8080
debug = true

入口文件【main.py】

# File : {projectWorkspaceRootDir}/main.py

# 直接创建 SanicBootApplication 实例
app = SanicBootApplication()

if __name__ == '__main__':
    app.run()

#############################################

# 如果需要以类的形式管理后端应用,可以继承 SanicBootApplication 后创建实例

from SanicBoot import SanicBootApplication

class Application(SanicBootApplication):
    def __init__(self):
        super().__init__(prefix='/agri')

if __name__ == '__main__':
    Application().run()

如需对 sanic 实例做自定义配置,请优先使用 SanicBootApplication 的 app 属性。建议在继承 SanicBootApplication 父类后,在类中对 app 父类属性自定义,使用 self.app.XXX = "XXX" 或官方文件中的其他形式。

路由装配【Controller、RequestMapping、XxxMapping、WebSocket】

SanicBootApplication 默认不使用官方的蓝图 BluePrint 构造路由系统,而是充分利用 Python 语言的模块化特性,按照目录约定被动加载路由约束。

# File Services/demo.py

from sanic import Request, json

from SanicBoot import GetMapping

# 基于函数的路由处理
@GetMapping('/get')
async def get_data(request: Request):
    return json({
        "data": [1, 2, 3]
    })

# 基于类的路由处理

# 支持对类或类的方法做路由约束
@RquestMapping('/controller') # 或使用 @Controller('/controller')
class Controller:
    @GepMapping('/get-name') # 完整路由是 `/controller/get-name`,若“手抖”多打了一个'/',会对其容错处理
    async def getName(self, request: Request):
        return json({"data": 'name'})

SanicBoot 使用官方提供的 app.add_route 创建路由,故支持使用路由参数。

支持的装饰器:

  • RequestMapping

  • Controller

    支持默认参数 uri = '',但括号目前不可省略

  • GetMapping

  • PostMapping

  • PutMapping

  • DeleteMapping

  • PatchMapping

  • ConnectMapping

  • OptionsMapping

  • HeadMapping

  • WebSocket

任务

基础定时任务

如果你希望使用 sanic 原生方案自动执行定时任务,请使用 SanicTaskMixin 中提供的装饰器 Task,该装饰器允许你提供一个任务名称,以便在运行时中通过任务名称获取该任务句柄。

@Task()
async def task1(app):
    ...

高级定时任务

如果你希望使用 APScheudler 提供的配置形式,请使用 TaskMixin 中提供的装饰器 Task,该装饰器将会自动创建一个执行器用来执行你创建的所有任务。

@Task('cron', ...) # APScheduler 支持的参数
async def task2(...):
    ...

Metadata

Release files for sanic-boot 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-boot 0.1.0
File Size Uploaded
sanic_boot-0.1.0.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sanic-boot 0.1.0
File Interpreter ABI Platform
sanic_boot-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / sanic_boot-0.1.0.tar.gz

Download URL sanic_boot-0.1.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2a84b1dc8c7ba3aa81340f6478852518494dde5fd04ceab204d9761dc50ad32c
BLAKE2b-256 checksum
How to use checksums
d719ee1c08015b3ed49c0cd41704e49b479b15af1d2f15bc70a7d49f7ebd5470
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / sanic_boot-0.1.0-py3-none-any.whl

Download URL sanic_boot-0.1.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6302734a48dc9fc721fd29489740fb455f8430d0417d134ed9ed9c452fac8f3d
BLAKE2b-256 checksum
How to use checksums
1b550918b23d2a32369ef3815c64bfc62c5776079041505a29b2d088873d8a5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page