MyBoot - 类似 Spring Boot 的 Python 快速开发框架

MyBoot - 类似 Spring Boot 的 Python 快速开发框架

MyBoot 是一个功能丰富的 Python Web 框架,提供类似 Spring Boot 的自动配置和快速开发功能。它集成了 Web API、定时任务、日志管理、配置管理等核心功能,让您能够快速构建现代化的 Python 应用程序。

✨ 主要特性

  • 🚀 快速启动: 类似 Spring Boot 的自动配置和快速启动
  • 🎯 约定优于配置: 遵循约定,减少配置工作,自动发现和注册组件
  • 🌐 Web API: 基于 FastAPI 的高性能 Web API 开发
  • 🌐 REST API 统一响应格式
  • 高性能服务器: 默认使用 Hypercorn 服务器,支持 HTTP/2 和多进程
  • 定时任务: 强大的任务调度系统,支持 Cron 表达式和间隔任务
  • 📝 日志管理: 基于 loguru 的强大日志系统,支持结构化日志和第三方库日志控制
  • ⚙️ 配置管理: 基于 Dynaconf 的强大配置系统,支持 YAML 配置、环境变量覆盖和远程配置
  • 🔧 中间件支持: 丰富的中间件生态,包括 CORS、限流、安全等
  • 📊 健康检查: 内置健康检查、就绪检查和存活检查
  • 🎯 依赖注入: 简单的依赖注入和组件管理
  • 🔄 优雅关闭: 支持优雅关闭和资源清理
  • 📚 自动文档: 自动生成 API 文档和交互式界面

⚡ 一分钟快速上手

安装命令:

1
pip install myboot

创建项目:

1
2
3
myboot init --name my-app --template api
cd my-app
python main.py

访问 http://localhost:8000,一个完整的 API 服务就跑起来了。


✨ 示例:Hello MyBoot

1
2
3
4
5
6
7
8
9
10
11
from myboot.core.application import create_app
from myboot.core.decorators import get

app = create_app(name="HelloApp")

@get("/")
def hello():
return {"message": "Hello, MyBoot!"}

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

无需手动注册路由、配置日志或服务器。
MyBoot 会自动扫描、注册并运行一切组件。

统一响应格式

1
2
3
4
5
6
server:
response_format:
enabled: true # 是否启用自动格式化
exclude_paths: # 排除的路径(这些路径不会自动格式化)
- "/custom/path"
- "/another/path"

响应格式:

1
2
3
4
5
6
7
8
{
"success": true,
"code": 200,
"message": "操作成功",
"data": {
// 实际业务数据
}
}

🧠 自动依赖注入与服务发现

MyBoot 的依赖注入机制让组件之间解耦变得自然:

1
2
3
4
5
6
7
8
9
10
11
from myboot.core.decorators import service, get
from myboot.core.application import get_service

@service()
class UserService:
def get_user(self, user_id): return {"id": user_id, "name": f"用户{user_id}"}

@get("/users/{user_id}")
def get_user(user_id: int):
user_service = get_service('user_service')
return user_service.get_user(user_id)

无须繁琐配置,框架自动识别、注册并注入依赖。


🕒 定时任务与后台任务

1
2
3
4
5
from myboot.core.decorators import cron

@cron("0 */1 * * * *")
def hourly_job():
print("每小时执行一次任务")

还可轻松集成异步任务或后台处理逻辑,无需引入 Celery。


🧰 统一的配置与日志管理

1
2
3
4
5
6
7
8
# conf/config.yaml
app:
name: "MyBoot App"
server:
port: 8000
logging:
level: INFO
file: logs/app.log

配置自动加载,可被环境变量或远程文件覆盖。
内置 Loguru 日志系统,支持结构化与 JSON 输出。


📁 推荐项目结构

1
2
3
4
5
6
7
8
9
my-app/
├── main.py
├── app/
│ ├── api/ # 路由
│ ├── service/ # 服务层
│ ├── jobs/ # 定时任务
│ └── client/ # 外部客户端
├── conf/config.yaml
└── tests/

简洁、规范、开箱即用。


💬 结语

MyBoot 并不是又一个 Web 框架,而是让 Python 开发体验接近 Spring Boot 的一体化解决方案
它让你专注于业务逻辑,而不是重复配置。

📦 项目地址:github.com/TrumanDu/myboot
🧑‍💻 立即安装体验:

1
pip install myboot

让企业级 Python 开发变得更快、更优雅。 更多文档详见项目首页。🚀