{"id":50418666,"url":"https://github.com/borgesme/fastapi-general-template","last_synced_at":"2026-05-31T07:04:00.406Z","repository":{"id":350126151,"uuid":"1205432906","full_name":"borgesme/fastapi-general-template","owner":"borgesme","description":"基于 FastAPI + SQLAlchemy + PostgreSQL + Redis 的单体应用模板。","archived":false,"fork":false,"pushed_at":"2026-04-09T00:56:11.000Z","size":44,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-09T02:36:07.701Z","etag":null,"topics":["fastapi","logger","postgresql","python","redis","skills","swagger"],"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/borgesme.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-09T00:48:13.000Z","updated_at":"2026-04-09T02:20:36.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/borgesme/fastapi-general-template","commit_stats":null,"previous_names":["borgesme/fastapi-general-template"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/borgesme/fastapi-general-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgesme%2Ffastapi-general-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgesme%2Ffastapi-general-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgesme%2Ffastapi-general-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgesme%2Ffastapi-general-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/borgesme","download_url":"https://codeload.github.com/borgesme/fastapi-general-template/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgesme%2Ffastapi-general-template/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33722162,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-31T02:00:06.040Z","response_time":95,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","logger","postgresql","python","redis","skills","swagger"],"created_at":"2026-05-31T07:04:00.275Z","updated_at":"2026-05-31T07:04:00.393Z","avatar_url":"https://github.com/borgesme.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI 微服务模板\n\n基于 FastAPI + SQLAlchemy + PostgreSQL + Redis 的单体应用模板。\n\n## 快速开始\n\n### 1. 安装依赖\n\n```bash\npython -m venv venv\nsource venv/bin/activate\n# Windows PowerShell:\n# venv\\Scripts\\Activate.ps1\n# Windows bash:\n# source venv/Scripts/activate\npip install -r requirements.txt\n```\n\n### 2. 配置环境变量\n\n```bash\ncp .env.example .env\n# 编辑 .env，填写数据库和 Redis 连接信息\n# 生成 SECRET_KEY: python -c \"import secrets; print(secrets.token_hex(32))\"\n```\n\n### 3. 数据库迁移\n\n```bash\nalembic upgrade head\n```\n\n### 4. 启动服务\n\n```bash\nuvicorn app.main:app --reload --host 0.0.0.0 --port 8000\n```\n\n访问: http://localhost:8000/docs （Swagger UI）\n\n## API 文档\n\n| 路径 | 说明 |\n|------|------|\n| GET /health | 健康检查 |\n| POST /api/v1/auth/register | 用户注册 |\n| POST /api/v1/auth/login | 用户登录 |\n| POST /api/v1/auth/refresh | 刷新 Token |\n| POST /api/v1/auth/logout | 登出 |\n| GET /api/v1/users/me | 获取当前用户 |\n| PUT /api/v1/users/me | 更新个人资料 |\n| PUT /api/v1/users/me/password | 修改密码 |\n\n## 运行测试\n\n```bash\npip install aiosqlite\npytest -v\n```\n\n## Docker 部署（生产环境）\n\n### 前置准备\n\n```bash\n# 复制环境变量模板并填入真实密钥\ncp .env.production.example .env.production\n# 编辑 .env.production，填入 SECRET_KEY 和 POSTGRES_PASSWORD\n# 生成密钥: python -c \"import secrets; print(secrets.token_hex(32))\"\n```\n\n### 启动所有服务\n\n```bash\n# 构建镜像并启动（app + postgres:16 + redis:7）\ndocker compose up --build -d\n\n# 查看日志\ndocker compose logs -f app\n\n# 验证健康检查\ncurl http://localhost:8000/health\n```\n\n### 各服务说明\n\n| 容器 | 端口 | 说明 |\n|------|------|------|\n| app | 8000 | FastAPI 应用（4 workers） |\n| postgres | 5432 | PostgreSQL 16 |\n| redis | 6379 | Redis 7 |\n\n### 常用操作\n\n```bash\n# 重新构建（代码变更后）\ndocker compose up --build -d\n\n# 停止所有服务\ndocker compose down\n\n# 停止并清除数据卷（慎用）\ndocker compose down -v\n\n# 进入 app 容器\ndocker compose exec app bash\n\n# 手动执行数据库迁移\ndocker compose exec app python -m alembic upgrade head\n```\n\n## 项目结构\n\n```\napp/\n├── main.py              # 应用入口\n├── config.py            # 配置管理\n├── dependencies.py      # 依赖注入\n├── api/                 # 路由层\n│   ├── root.py          # 健康检查\n│   └── v1/              # API v1\n│       ├── auth.py      # 认证接口\n│       └── users.py     # 用户管理\n├── core/                # 核心模块\n│   ├── logger.py        # 日志配置（structlog + loguru）\n│   ├── security.py      # JWT 安全\n│   └── exceptions.py    # 统一异常\n├── middleware/          # 中间件\n│   ├── request_id.py    # 请求 ID 追踪\n│   └── request_logging.py  # 请求日志\n├── models/              # 数据库模型\n├── schemas/             # Pydantic Schema\n├── services/            # 业务逻辑层\n├── db/                  # 数据库层\n├── cache/               # Redis 缓存\n└── utils/               # 工具函数\n\nDocker 相关\n├── Dockerfile           # 多阶段构建镜像\n├── docker-compose.yml   # 服务编排\n└── entrypoint.sh        # 启动脚本（迁移 + 启动）\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborgesme%2Ffastapi-general-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fborgesme%2Ffastapi-general-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborgesme%2Ffastapi-general-template/lists"}