Skip to content

Commit d39599e

Browse files
committed
feat: HaloLight Python API - FastAPI 企业级后端服务
完整功能实现: - 用户认证系统(注册、登录、JWT、刷新令牌) - 密码重置功能(令牌生成、验证、原子更新) - 文档管理(CRUD、分享、标签) - 文件存储(上传、下载、预览) - 团队协作(团队管理、成员权限) - 消息系统(对话、实时消息) - 日历功能(事件、提醒、参与者) - 通知系统(实时推送) - 仪表盘统计(数据分析、图表) 技术栈: - Python 3.11 + FastAPI - SQLAlchemy ORM + PostgreSQL - JWT 认证 + BCrypt 密码哈希 - Pydantic 数据验证 - Docker + Fly.io 部署 代码质量: - 完整的类型注解 - 异常处理体系 - 原子操作防竞态 - 符合 FastAPI 最佳实践
0 parents  commit d39599e

62 files changed

Lines changed: 9686 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Application settings
2+
APP_NAME=HaloLight API
3+
APP_VERSION=1.0.0
4+
DEBUG=false
5+
ENVIRONMENT=production
6+
7+
# API settings
8+
API_PREFIX=/api
9+
CORS_ORIGINS=["http://localhost:3000", "http://localhost:5173"]
10+
11+
# Database settings
12+
DATABASE_URL=postgresql://user:password@localhost:5432/halolight
13+
DATABASE_ECHO=false
14+
15+
# JWT settings
16+
JWT_SECRET_KEY=your-secret-key-change-this-in-production
17+
JWT_ALGORITHM=HS256
18+
JWT_ACCESS_TOKEN_EXPIRE_MINUTES=10080
19+
20+
# Security
21+
PASSWORD_MIN_LENGTH=6

.github/workflows/ci.yml

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main, develop]
6+
pull_request:
7+
branches: [main, develop]
8+
workflow_dispatch:
9+
10+
# 取消同一分支的之前运行,节省资源
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
env:
16+
PYTHON_VERSION: "3.11"
17+
CI: "true"
18+
19+
jobs:
20+
# ============================================================================
21+
# 代码质量检查
22+
# ============================================================================
23+
lint:
24+
name: Lint & Type Check
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout repository
28+
uses: actions/checkout@v4
29+
30+
- name: Setup Python
31+
uses: actions/setup-python@v5
32+
with:
33+
python-version: ${{ env.PYTHON_VERSION }}
34+
35+
- name: Cache pip dependencies
36+
uses: actions/cache@v4
37+
with:
38+
path: ~/.cache/pip
39+
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
40+
restore-keys: |
41+
${{ runner.os }}-pip-
42+
43+
- name: Install dependencies
44+
run: |
45+
python -m pip install --upgrade pip
46+
pip install -e ".[dev]"
47+
48+
- name: Run Ruff linter
49+
run: ruff check app tests
50+
51+
- name: Run Black formatter check
52+
run: black --check app tests
53+
54+
- name: Run MyPy type checker
55+
run: mypy app --ignore-missing-imports
56+
continue-on-error: true
57+
58+
# ============================================================================
59+
# 单元测试
60+
# ============================================================================
61+
test:
62+
name: Unit Tests
63+
runs-on: ubuntu-latest
64+
needs: lint
65+
steps:
66+
- name: Checkout repository
67+
uses: actions/checkout@v4
68+
69+
- name: Setup Python
70+
uses: actions/setup-python@v5
71+
with:
72+
python-version: ${{ env.PYTHON_VERSION }}
73+
74+
- name: Cache pip dependencies
75+
uses: actions/cache@v4
76+
with:
77+
path: ~/.cache/pip
78+
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
79+
restore-keys: |
80+
${{ runner.os }}-pip-
81+
82+
- name: Install dependencies
83+
run: |
84+
python -m pip install --upgrade pip
85+
pip install -e ".[dev]"
86+
87+
- name: Run tests with coverage
88+
env:
89+
DATABASE_URL: sqlite:///./test.db
90+
JWT_SECRET_KEY: test-secret-key-for-ci
91+
JWT_REFRESH_SECRET_KEY: test-refresh-secret-key-for-ci
92+
ENVIRONMENT: test
93+
run: pytest --cov=app --cov-report=xml --cov-report=term-missing tests/
94+
95+
- name: Upload coverage to Codecov
96+
uses: codecov/codecov-action@v4
97+
if: always()
98+
with:
99+
token: ${{ secrets.CODECOV_TOKEN }}
100+
fail_ci_if_error: false
101+
files: ./coverage.xml
102+
flags: unittests
103+
name: codecov-umbrella
104+
105+
# ============================================================================
106+
# E2E 测试
107+
# ============================================================================
108+
e2e:
109+
name: E2E Tests
110+
runs-on: ubuntu-latest
111+
needs: lint
112+
113+
# PostgreSQL 服务
114+
services:
115+
postgres:
116+
image: postgres:16-alpine
117+
env:
118+
POSTGRES_USER: test
119+
POSTGRES_PASSWORD: test
120+
POSTGRES_DB: halolight_test
121+
options: >-
122+
--health-cmd pg_isready
123+
--health-interval 10s
124+
--health-timeout 5s
125+
--health-retries 5
126+
ports:
127+
- 5432:5432
128+
129+
env:
130+
ENVIRONMENT: test
131+
DATABASE_URL: postgresql://test:test@localhost:5432/halolight_test
132+
JWT_SECRET_KEY: test-secret-key-for-ci
133+
JWT_REFRESH_SECRET_KEY: test-refresh-secret-key-for-ci
134+
135+
steps:
136+
- name: Checkout repository
137+
uses: actions/checkout@v4
138+
139+
- name: Setup Python
140+
uses: actions/setup-python@v5
141+
with:
142+
python-version: ${{ env.PYTHON_VERSION }}
143+
144+
- name: Cache pip dependencies
145+
uses: actions/cache@v4
146+
with:
147+
path: ~/.cache/pip
148+
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
149+
restore-keys: |
150+
${{ runner.os }}-pip-
151+
152+
- name: Install dependencies
153+
run: |
154+
python -m pip install --upgrade pip
155+
pip install -e ".[dev]"
156+
157+
- name: Run database migrations
158+
run: alembic upgrade head
159+
160+
- name: Run E2E tests
161+
run: |
162+
if [ -d "tests/e2e" ]; then
163+
pytest tests/e2e/ -v --tb=short
164+
else
165+
echo "E2E tests directory not found, skipping..."
166+
fi
167+
168+
# ============================================================================
169+
# 构建检查
170+
# ============================================================================
171+
build:
172+
name: Build
173+
runs-on: ubuntu-latest
174+
needs: lint
175+
steps:
176+
- name: Checkout repository
177+
uses: actions/checkout@v4
178+
179+
- name: Setup Python
180+
uses: actions/setup-python@v5
181+
with:
182+
python-version: ${{ env.PYTHON_VERSION }}
183+
184+
- name: Cache pip dependencies
185+
uses: actions/cache@v4
186+
with:
187+
path: ~/.cache/pip
188+
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
189+
restore-keys: |
190+
${{ runner.os }}-pip-
191+
192+
- name: Install dependencies
193+
run: |
194+
python -m pip install --upgrade pip
195+
pip install -e ".[dev]"
196+
pip install build
197+
198+
- name: Build package
199+
run: python -m build
200+
201+
- name: Upload build artifacts
202+
uses: actions/upload-artifact@v4
203+
with:
204+
name: build-output
205+
path: |
206+
dist/
207+
retention-days: 7
208+
209+
# ============================================================================
210+
# Docker 构建检查
211+
# ============================================================================
212+
docker:
213+
name: Docker Build
214+
runs-on: ubuntu-latest
215+
needs: [lint, test]
216+
steps:
217+
- name: Checkout repository
218+
uses: actions/checkout@v4
219+
220+
- name: Set up Docker Buildx
221+
uses: docker/setup-buildx-action@v3
222+
223+
- name: Build Docker image
224+
uses: docker/build-push-action@v5
225+
with:
226+
context: .
227+
push: false
228+
tags: halolight-api-python:${{ github.sha }}
229+
cache-from: type=gha
230+
cache-to: type=gha,mode=max
231+
232+
# ============================================================================
233+
# 依赖安全审计
234+
# ============================================================================
235+
security:
236+
name: Security Audit
237+
runs-on: ubuntu-latest
238+
steps:
239+
- name: Checkout repository
240+
uses: actions/checkout@v4
241+
242+
- name: Setup Python
243+
uses: actions/setup-python@v5
244+
with:
245+
python-version: ${{ env.PYTHON_VERSION }}
246+
247+
- name: Install dependencies
248+
run: |
249+
python -m pip install --upgrade pip
250+
pip install -e .
251+
pip install bandit safety pip-audit
252+
253+
- name: Run Bandit security linter
254+
run: bandit -r app -ll -ii
255+
continue-on-error: true
256+
257+
- name: Run pip-audit for vulnerabilities
258+
run: pip-audit
259+
continue-on-error: true
260+
261+
# ============================================================================
262+
# 依赖更新检查(仅 PR)
263+
# ============================================================================
264+
dependency-review:
265+
name: Dependency Review
266+
runs-on: ubuntu-latest
267+
if: github.event_name == 'pull_request'
268+
steps:
269+
- name: Checkout repository
270+
uses: actions/checkout@v4
271+
272+
- name: Dependency Review
273+
uses: actions/dependency-review-action@v4
274+
with:
275+
fail-on-severity: high
276+
allow-licenses: MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, ISC, 0BSD, PSF-2.0

.gitignore

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
pip-wheel-metadata/
20+
share/python-wheels/
21+
*.egg-info/
22+
.installed.cfg
23+
*.egg
24+
MANIFEST
25+
26+
# Virtual environments
27+
.env
28+
.venv
29+
env/
30+
venv/
31+
ENV/
32+
env.bak/
33+
venv.bak/
34+
35+
# PyCharm
36+
.idea/
37+
38+
# VS Code
39+
.vscode/
40+
41+
# pytest
42+
.pytest_cache/
43+
.coverage
44+
htmlcov/
45+
coverage.xml
46+
*.cover
47+
48+
# mypy
49+
.mypy_cache/
50+
.dmypy.json
51+
dmypy.json
52+
53+
# Database
54+
*.db
55+
*.sqlite3
56+
57+
# Alembic
58+
alembic/versions/*.py
59+
!alembic/versions/.gitkeep
60+
61+
# Logs
62+
*.log
63+
64+
# OS
65+
.DS_Store
66+
Thumbs.db
67+
68+
# Docker
69+
*.env.local

0 commit comments

Comments
 (0)