{"id":19655850,"url":"https://github.com/kaxiluo/fastapi-skeleton","last_synced_at":"2025-04-10T05:05:40.015Z","repository":{"id":41088420,"uuid":"508259957","full_name":"kaxiluo/fastapi-skeleton","owner":"kaxiluo","description":"fastapi skeleton. framework integration orm jwt-auth loguru dotenv APScheduler...","archived":false,"fork":false,"pushed_at":"2023-10-11T03:51:14.000Z","size":37,"stargazers_count":204,"open_issues_count":1,"forks_count":64,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-03T01:11:18.163Z","etag":null,"topics":["fastapi","skeleton"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaxiluo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-28T10:51:47.000Z","updated_at":"2025-03-31T05:09:50.000Z","dependencies_parsed_at":"2023-10-11T05:37:05.566Z","dependency_job_id":null,"html_url":"https://github.com/kaxiluo/fastapi-skeleton","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Ffastapi-skeleton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Ffastapi-skeleton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Ffastapi-skeleton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Ffastapi-skeleton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaxiluo","download_url":"https://codeload.github.com/kaxiluo/fastapi-skeleton/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161275,"owners_count":21057555,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","skeleton"],"created_at":"2024-11-11T15:24:14.625Z","updated_at":"2025-04-10T05:05:39.994Z","avatar_url":"https://github.com/kaxiluo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"这是一个开箱即用的FastAPI脚手架，集成了ORM模型、JWT认证、日志系统、异常处理、路由注册、系统配置、调度任务等常用的模块。\n\n## 设计思想\n\n- 层级结构清晰\n- 简洁优雅\n- 易于扩展\n- 开箱即用\n\n## 项目结构\n\n```\n/kaxiluo/fastapi-skeleton/\n|-- app\n|   |-- commands                                ----- 放置一些命令行\n|   |   `-- __init__.py\n|   |-- exceptions                              ----- 自定义的异常类\n|   |   |-- __init__.py\n|   |   `-- exception.py\n|   |-- http                                    ----- http目录\n|   |   |-- api                                 ----- api控制器目录\n|   |   |   |-- __init__.py\n|   |   |   |-- auth.py                         ----- 登录认证api的控制器\n|   |   |   |-- demo.py\n|   |   |   `-- users.py\n|   |   |-- middleware                          ----- 放置自定义中间件\n|   |   |   `-- __init__.py\n|   |   |-- __init__.py\n|   |   `-- deps.py                             ----- 依赖\n|   |-- jobs                                    ----- 调度任务\n|   |   |-- __init__.py\n|   |   `-- demo_job.py\n|   |-- models                                  ----- 模型目录\n|   |   |-- __init__.py\n|   |   |-- base_model.py                       ----- 定义模型的基类\n|   |   `-- user.py\n|   |-- providers                               ----- 核心服务提供者\n|   |   |-- __init__.py\n|   |   |-- app_provider.py                     ----- 注册应用的全局事件、中间件等\n|   |   |-- database.py                         ----- 数据库连接\n|   |   |-- handle_exception.py                 ----- 异常处理器\n|   |   |-- logging_provider.py                 ----- 集成loguru日志系统\n|   |   `-- route_provider.py                   ----- 注册路由文件routes/*\n|   |-- schemas                                 ----- 数据模型，负责请求和响应资源数据的定义和格式转换\n|   |   |-- __init__.py\n|   |   `-- user.py\n|   |-- services                                ----- 服务层，业务逻辑层\n|   |   |-- auth                                ----- 认证相关服务\n|   |   |   |-- __init__.py\n|   |   |   |-- grant.py                        ----- 认证核心类\n|   |   |   |-- hashing.py\n|   |   |   |-- jwt_helper.py\n|   |   |   |-- oauth2_schema.py\n|   |   |   `-- random_code_verifier.py\n|   |   `-- __init__.py\n|   |-- support                                 ----- 公共方法\n|   |   |-- __init__.py\n|   |   `-- helper.py\n|   `-- __init__.py\n|-- bootstrap                                   ----- 启动项\n|   |-- __init__.py\n|   |-- application.py                          ----- 创建app实例\n|   `-- scheduler.py                            ----- 创建调度器实例\n|-- config                                      ----- 配置目录\n|   |-- auth.py                                 ----- 认证-JWT配置\n|   |-- config.py                               ----- app配置\n|   |-- database.py                             ----- 数据库配置\n|   `-- logging.py                              ----- 日志配置\n|-- database\n|   `-- migrations                              ----- 初始化SQL\n|       `-- 2022_09_07_create_users_table.sql\n|-- routes                                      ----- 路由目录\n|   |-- __init__.py\n|   `-- api.py                                  ----- api路由\n|-- storage\n|   `-- logs                                    ----- 日志目录\n|-- README.md\n|-- main.py                                     ----- app/api启动入口\n|-- requirements.txt\n`-- scheduler.py                                ----- 调度任务启动入口\n```\n\n## 集成的模块\n\n- 日志系统\n\n集成 `loguru`，一个优雅、简洁的日志库\n\n- 异常处理\n\n定义认证异常类，注册 `Exception Handler`\n\n- 路由注册\n\n路由集中注册，按模块划分为不同的文件，代码层次结构清晰\n\n- 系统配置\n\n基于 `pydantic.BaseSettings`，使用 `.env` 文件设置环境变量。配置文件按功能模块划分，默认定义了app基础配置、数据库配置(mysql+redis)、日志配置、认证配置\n\n- 数据库 ORM模型\n\n基于 `peewee`，一个轻量级的Python ORM框架\n\n- 中间件\n\n默认注册了全局CORS中间件\n\n- JWT认证\n\n默认提供了账号密码和手机号验证码两种认证方式。框架易于扩展新的认证方式。\n\n测试登录认证请先执行初始化的SQL：`fastapi-skeleton/database/migrations/*.sql`\n\n注：验证码的存储依赖redis\n\n- 调度任务\n\n基于 `APScheduler` 调度任务框架\n\n注：定时任务与api是分开启动的\n\n## 运行\n\n1. 执行初始化SQL：`/database/migrations/2022_09_07_create_users_table.sql`\n\n2. API\n\n```bash\nuvicorn main:app --host 0.0.0.0 --port 8080\n```\n\n3. 调度器\n\n```bash\npython scheduler.py \n```\n\n关于部署部分，参见我的另一篇文章 [fastapi部署](https://www.kxler.com/2022/10/21/fastapi-deployment-venv-gunicorn-service/)\n\n## 参考\n\n[FastAPI官方中文文档](https://fastapi.tiangolo.com/zh/)\n\nFastAPI作者的全栈项目脚手架 [full-stack-fastapi-postgresql](https://github.com/tiangolo/full-stack-fastapi-postgresql)\n\n代码结构组织风格参考 [Laravel框架](https://github.com/laravel/laravel)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaxiluo%2Ffastapi-skeleton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaxiluo%2Ffastapi-skeleton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaxiluo%2Ffastapi-skeleton/lists"}