Metadata-Version: 2.4
Name: sanic-boot
Version: 0.1.0
Summary: A sanic-based application framework inspired by SpringBoot.
Author-email: 嬴寒 <yinghan22@163.com>
License-Expression: MIT
License-File: LICENSE
Keywords: framework,sanic,sanic-boot,springboot,web
Classifier: Framework :: AsyncIO
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.14
Requires-Python: >=3.14
Requires-Dist: pydantic
Requires-Dist: sanic
Requires-Dist: sanic-ext
Requires-Dist: tortoise-orm
Description-Content-Type: text/markdown

![sanic-framework-boot-logo-400x93.png](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`

## 项目目录结构

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

## 使用

### 配置【Configureation】

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

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

```toml
# 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】

```Python
# 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 语言的模块化特性，按照目录约定被动加载路由约束。

```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`，该装饰器允许你提供一个任务名称，以便在运行时中通过任务名称获取该任务句柄。

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

#### 高级定时任务

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

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