# sanic **Repository Path**: chenbool/sanic ## Basic Information - **Project Name**: sanic - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-25 - **Last Updated**: 2026-04-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Sanic Web 项目 基于 Sanic 异步框架的 RESTful API 项目,使用 peewee 作为 ORM 连接到 MySQL 数据库。 ## 技术栈 | 类别 | 技术 | 版本 | |------|------|------| | Web 框架 | Sanic | 21.3.4 | | ORM | peewee | 3.14.4 | | 数据库 | MySQL | - | | 环境配置 | python-dotenv | 0.21.0 | ## 项目结构 ``` sanic/ ├── api/ # API 蓝图模块 │ ├── __init__.py # 蓝图路由注册 │ ├── login.py # 登录接口 │ └── info.py # 信息接口 ├── config/ # 配置文件 │ ├── database.py # 数据库配置 │ └── settings.py # 应用配置 ├── model/ # 数据模型 │ ├── model.py # peewee 模型定义 │ └── record.py # 记录相关模型 ├── utils/ # 工具模块 │ └── response.py # 统一响应格式 ├── app.py # 主应用入口 ├── .env # 环境变量(勿提交) ├── .env.example # 环境变量示例 ├── requirements.txt # 依赖列表 └── .gitignore # Git 忽略文件 ``` ## 功能特性 | 功能 | 说明 | |------|------| | 异步 RESTful API | 基于 Sanic 异步框架的高性能 API | | peewee ORM | 简洁高效的数据库 ORM 操作 | | 统一响应格式 | 标准化的 JSON 响应格式 | | 中间件支持 | 请求日志、Cookie 设置 | | Blueprint 模块化 | 模块化路由管理 | | 环境变量配置 | 通过 .env 文件管理配置 | | CORS 跨域支持 | 前后端分离开发支持 | ## 快速开始 ### 1. 安装依赖 ```bash pip3 install -r requirements.txt ``` ### 2. 配置环境变量 复制 `.env.example` 为 `.env` 并修改配置: ```bash cp .env.example .env ``` 编辑 `.env` 文件: | 参数 | 说明 | 默认值 | |------|------|--------| | DEBUG | 调试模式 | true | | HOST | 服务器地址 | 127.0.0.1 | | PORT | 服务器端口 | 8000 | | MYSQL_HOST | 数据库地址 | 127.0.0.1 | | MYSQL_PORT | 数据库端口 | 3306 | | MYSQL_USER | 数据库用户名 | root | | MYSQL_PASSWORD | 数据库密码 | root | | MYSQL_DATABASE | 数据库名称 | sanic | | MYSQL_CHARSET | 字符编码 | utf8 | ### 3. 初始化数据库 ```bash python -m model.model ``` ### 4. 运行项目 ```bash python app.py ``` 服务启动后访问 http://127.0.0.1:8000 ## API 接口 | 方法 | 路径 | 说明 | 响应示例 | |------|------|------|----------| | GET | `/` | 根路由,返回欢迎信息 | `{"message": "Welcome to Sanic API"}` | | GET | `/api/login/` | 登录接口 | `{"code": 0, "message": "success"}` | | POST | `/api/login/register` | 注册接口 | `{"code": 0, "message": "success"}` | | GET | `/api/info/` | 信息接口 | `{"code": 0, "message": "success"}` | | GET | `/api/info/` | 获取用户信息 | `{"code": 0, "message": "success"}` | | PUT | `/api/info/update` | 更新信息 | `{"code": 0, "message": "success"}` | ## 响应格式 ### 成功响应 ```json { "code": 0, "message": "success", "data": {} } ``` ### 错误响应 | HTTP 状态码 | 业务错误码 | 说明 | |-------------|------------|------| | 400 | 400 | 请求参数错误 | | 401 | 401 | 未授权 | | 404 | 404 | 资源不存在 | | 500 | 500 | 服务器内部错误 | ```json { "code": 400, "message": "错误信息", "data": null } ``` ## 数据库操作 ### 创建表 ```python from model.model import init_tables init_tables() ``` ### 定义模型 | 字段类型 | 说明 | 示例 | |----------|------|------| | CharField | 字符串 | `name = CharField()` | | IntegerField | 整数 | `age = IntegerField()` | | BooleanField | 布尔 | `is_active = BooleanField()` | | DateField | 日期 | `birthday = DateField()` | | DateTimeField | 日期时间 | `created_at = DateTimeField()` | | TextField | 长文本 | `content = TextField()` | ```python from peewee import * from model.model import BaseModel class User(BaseModel): name = CharField() email = CharField() class Meta: table_name = 'user' ``` ### 常用操作 | 操作 | 方法 | |------|------| | 插入 | `User.create(name='Tom', email='tom@example.com')` | | 查询单条 | `User.get(User.id == 1)` | | 查询多条 | `User.select().where(User.is_active == True)` | | 更新 | `User.update({User.name: 'NewName'}).where(User.id == 1).execute()` | | 删除 | `User.delete().where(User.id == 1).execute()` | ## 注意事项 | 事项 | 说明 | |------|------| | MySQL 服务 | 确保 MySQL 数据库已启动 | | 数据库创建 | 首次运行前需创建对应的数据库 (`CREATE DATABASE sanic;`) | | 敏感信息 | `.env` 文件包含敏感信息,请勿提交到版本控制 | | MySQL 驱动 | 如遇到 `MySQL driver not installed!` 错误,执行 `pip install pymysql` |