fastapi-authly 0.1.2__tar.gz → 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/PKG-INFO +1 -1
- fastapi_authly-0.1.3/examples/correct_usage.py +74 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/pyproject.toml +1 -1
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/__about__.py +1 -1
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/__init__.py +4 -2
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/core/__init__.py +2 -1
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/core/config.py +21 -0
- fastapi_authly-0.1.2/CHANGELOG_SCALAR.md +0 -84
- fastapi_authly-0.1.2/docs/SCALAR_DOCS_USAGE.md +0 -121
- fastapi_authly-0.1.2/examples/complete_example.py +0 -63
- fastapi_authly-0.1.2/examples/use_scalar_docs.py +0 -53
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/.gitignore +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/LICENSE +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/README.md +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/README.zh.md +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/coverage.xml +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/docs//345/207/275/346/225/260/345/274/217/347/274/226/347/250/213/344/270/203/346/255/246/345/231/250.md" +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/.gitignore +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/class_index.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/coverage_html_cb_dd2e7eb5.js +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/favicon_32_cb_c827f16f.png +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/function_index.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/index.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/keybd_closed_cb_900cfef5.png +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/status.json +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/style_cb_9ff733b0.css +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_af1bec017750c6fc___init___py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_af1bec017750c6fc_user_py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_b9d93864b1b0ad6e___about___py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_b9d93864b1b0ad6e___init___py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_b9d93864b1b0ad6e_auth_py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_b9d93864b1b0ad6e_interfaces_py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_d015ea9b27b0258e___init___py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_d015ea9b27b0258e_config_py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_d015ea9b27b0258e_security_py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_ddd01122054512b0___init___py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_ddd01122054512b0_tortoise_pg_py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_feee2d9ae7f7fd96___init___py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_feee2d9ae7f7fd96_user_py.html +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/auth.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/contrib/__init__.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/contrib/tortoise_pg.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/core/security.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/deps.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/docs.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/interfaces.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/models/__init__.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/models/user.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/schemas/__init__.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/schemas/user.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/static/scalar/standalone.js +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/static/scalar/style.css +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/test.py +0 -0
- {fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/uv.lock +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi-authly
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: A modular authentication system for FastAPI with OAuth2, JWT, and password recovery
|
|
5
5
|
Project-URL: Homepage, https://github.com/yourusername/fastapi-auth-module
|
|
6
6
|
Project-URL: Documentation, https://yourusername.github.io/fastapi-auth-module/
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""
|
|
2
|
+
正确的使用方式示例
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from fastapi import FastAPI
|
|
6
|
+
from tortoise.contrib.fastapi import register_tortoise
|
|
7
|
+
from fastapi_authly import (
|
|
8
|
+
AuthConfig,
|
|
9
|
+
AuthDependencyConfig,
|
|
10
|
+
create_auth_router,
|
|
11
|
+
JwtConfig,
|
|
12
|
+
setup_scalar_docs
|
|
13
|
+
)
|
|
14
|
+
from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
15
|
+
|
|
16
|
+
app = FastAPI(title="FastAPI Authly 测试应用")
|
|
17
|
+
|
|
18
|
+
# 初始化 Tortoise + Postgres
|
|
19
|
+
register_tortoise(
|
|
20
|
+
app,
|
|
21
|
+
db_url="postgres://user:password@localhost:5432/testdb",
|
|
22
|
+
modules={"models": ["fastapi_authly.models.user"]},
|
|
23
|
+
generate_schemas=True,
|
|
24
|
+
add_exception_handlers=True,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# 方式 1: 使用 JwtConfig 实例(推荐)
|
|
28
|
+
config = AuthConfig(
|
|
29
|
+
jwt=JwtConfig(
|
|
30
|
+
secret_key="your-secret-key-change-in-production",
|
|
31
|
+
algorithm="HS256",
|
|
32
|
+
access_token_expires_minutes=30,
|
|
33
|
+
refresh_token_expire_days=7,
|
|
34
|
+
),
|
|
35
|
+
router_prefix="/auth",
|
|
36
|
+
token_url="login",
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# 方式 2: 使用字典更新(也可以)
|
|
40
|
+
# config = AuthConfig(
|
|
41
|
+
# jwt={
|
|
42
|
+
# "secret_key": "your-secret-key-change-in-production",
|
|
43
|
+
# "algorithm": "HS256",
|
|
44
|
+
# "access_token_expires_minutes": 30,
|
|
45
|
+
# },
|
|
46
|
+
# router_prefix="/auth",
|
|
47
|
+
# token_url="login",
|
|
48
|
+
# )
|
|
49
|
+
|
|
50
|
+
# 方式 3: 使用默认配置,然后通过环境变量覆盖
|
|
51
|
+
# 设置环境变量: AUTH_JWT__SECRET_KEY=your-secret-key
|
|
52
|
+
# config = AuthConfig(
|
|
53
|
+
# router_prefix="/auth",
|
|
54
|
+
# token_url="login",
|
|
55
|
+
# )
|
|
56
|
+
|
|
57
|
+
deps = AuthDependencyConfig(
|
|
58
|
+
user_repository=TortoiseUserRepository(),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# 注册认证路由
|
|
62
|
+
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
63
|
+
app.include_router(auth_router)
|
|
64
|
+
|
|
65
|
+
# 设置 Scalar 文档
|
|
66
|
+
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
67
|
+
|
|
68
|
+
@app.get("/")
|
|
69
|
+
async def root():
|
|
70
|
+
return {"message": "FastAPI Authly 测试应用已启动"}
|
|
71
|
+
|
|
72
|
+
if __name__ == "__main__":
|
|
73
|
+
import uvicorn
|
|
74
|
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
@@ -7,21 +7,23 @@ This package provides a complete authentication solution with:
|
|
|
7
7
|
- Password recovery
|
|
8
8
|
- User management
|
|
9
9
|
- Modular and configurable design
|
|
10
|
+
- Built-in Scalar API documentation
|
|
10
11
|
"""
|
|
11
12
|
|
|
12
13
|
from .auth import AuthModule, create_auth_router
|
|
13
|
-
from .core import AuthConfig, AuthDependencyConfig, BcryptPasswordHasher, JWTTokenService
|
|
14
|
+
from .core import AuthConfig, AuthDependencyConfig, JwtConfig, BcryptPasswordHasher, JWTTokenService
|
|
14
15
|
from .docs import setup_scalar_docs
|
|
15
16
|
from .interfaces import Mailer, PasswordHasher, TokenService, UserRepository
|
|
16
17
|
from .schemas.user import UserBase, UserCreate, UserUpdate, UserPublic, Token, TokenData
|
|
17
18
|
|
|
18
|
-
__version__ = "0.1.
|
|
19
|
+
__version__ = "0.1.1"
|
|
19
20
|
|
|
20
21
|
__all__ = [
|
|
21
22
|
# Main classes
|
|
22
23
|
"AuthModule",
|
|
23
24
|
"AuthConfig",
|
|
24
25
|
"AuthDependencyConfig",
|
|
26
|
+
"JwtConfig",
|
|
25
27
|
|
|
26
28
|
# Core functions
|
|
27
29
|
"create_auth_router",
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"""Core functionality for fastapi-authly."""
|
|
2
2
|
|
|
3
|
-
from .config import AuthConfig, AuthDependencyConfig
|
|
3
|
+
from .config import AuthConfig, AuthDependencyConfig, JwtConfig
|
|
4
4
|
from .security import BcryptPasswordHasher, JWTTokenService
|
|
5
5
|
|
|
6
6
|
__all__ = [
|
|
7
7
|
"AuthConfig",
|
|
8
8
|
"AuthDependencyConfig",
|
|
9
|
+
"JwtConfig",
|
|
9
10
|
"BcryptPasswordHasher",
|
|
10
11
|
"JWTTokenService",
|
|
11
12
|
]
|
|
@@ -58,6 +58,27 @@ class AuthConfig(BaseSettings):
|
|
|
58
58
|
env_prefix="AUTH_",
|
|
59
59
|
extra="ignore",
|
|
60
60
|
)
|
|
61
|
+
|
|
62
|
+
# 属性访问器:直接访问 jwt 中的属性,兼容现有代码
|
|
63
|
+
@property
|
|
64
|
+
def secret_key(self) -> str:
|
|
65
|
+
"""JWT secret key"""
|
|
66
|
+
return self.jwt.secret_key
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def algorithm(self) -> str:
|
|
70
|
+
"""JWT algorithm"""
|
|
71
|
+
return self.jwt.algorithm
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def access_token_expire_minutes(self) -> Optional[int]:
|
|
75
|
+
"""Access token expiration in minutes (compatible with auth.py)"""
|
|
76
|
+
return self.jwt.access_token_expires_minutes
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def refresh_token_expire_days(self) -> int:
|
|
80
|
+
"""Refresh token expiration in days"""
|
|
81
|
+
return self.jwt.refresh_token_expire_days
|
|
61
82
|
|
|
62
83
|
_config = AuthConfig()
|
|
63
84
|
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
# Scalar 文档功能集成说明
|
|
2
|
-
|
|
3
|
-
## 改动概述
|
|
4
|
-
|
|
5
|
-
已将 Scalar API 文档功能集成到 `fastapi-authly` 包中,包含所有必要的静态资源文件。用户只需导入并使用 `setup_scalar_docs` 函数即可启用文档功能,无需手动管理静态文件。
|
|
6
|
-
|
|
7
|
-
## 新增文件
|
|
8
|
-
|
|
9
|
-
1. **`src/fastapi_authly/docs.py`**: 文档功能模块
|
|
10
|
-
- `setup_scalar_docs()`: 主要函数,用于设置 Scalar 文档
|
|
11
|
-
- `get_static_dir()`: 获取包内静态文件目录路径
|
|
12
|
-
|
|
13
|
-
2. **`src/fastapi_authly/static/scalar/`**: 静态资源目录
|
|
14
|
-
- `standalone.js`: Scalar JavaScript 文件
|
|
15
|
-
- `style.css`: Scalar CSS 样式文件
|
|
16
|
-
|
|
17
|
-
3. **示例文件**:
|
|
18
|
-
- `examples/use_scalar_docs.py`: 基本使用示例
|
|
19
|
-
- `examples/complete_example.py`: 完整使用示例
|
|
20
|
-
- `docs/SCALAR_DOCS_USAGE.md`: 详细使用文档
|
|
21
|
-
|
|
22
|
-
## 修改文件
|
|
23
|
-
|
|
24
|
-
1. **`src/fastapi_authly/__init__.py`**:
|
|
25
|
-
- 添加 `setup_scalar_docs` 的导入和导出
|
|
26
|
-
|
|
27
|
-
2. **`README.md` 和 `README.zh.md`**:
|
|
28
|
-
- 添加 Scalar 文档功能的使用说明
|
|
29
|
-
|
|
30
|
-
## 使用方法
|
|
31
|
-
|
|
32
|
-
### 基本使用
|
|
33
|
-
|
|
34
|
-
```python
|
|
35
|
-
from fastapi import FastAPI
|
|
36
|
-
from fastapi_authly import setup_scalar_docs
|
|
37
|
-
|
|
38
|
-
app = FastAPI(title="My API")
|
|
39
|
-
setup_scalar_docs(app)
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### 自定义配置
|
|
43
|
-
|
|
44
|
-
```python
|
|
45
|
-
setup_scalar_docs(
|
|
46
|
-
app,
|
|
47
|
-
docs_url="/api-docs",
|
|
48
|
-
static_url="/assets",
|
|
49
|
-
title="Custom API Docs",
|
|
50
|
-
openapi_url="/openapi.json"
|
|
51
|
-
)
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## 优势
|
|
55
|
-
|
|
56
|
-
1. **零配置**: 无需手动复制静态文件
|
|
57
|
-
2. **可移植**: 安装包后即可使用
|
|
58
|
-
3. **简单**: 一行代码启用
|
|
59
|
-
4. **灵活**: 支持自定义 URL 和配置
|
|
60
|
-
|
|
61
|
-
## 构建和发布
|
|
62
|
-
|
|
63
|
-
静态文件会自动包含在构建的包中,因为它们在 `src/fastapi_authly/static/` 目录下,属于包的一部分。
|
|
64
|
-
|
|
65
|
-
构建和发布流程不变:
|
|
66
|
-
|
|
67
|
-
```bash
|
|
68
|
-
uv build
|
|
69
|
-
uv publish --token your-token
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## 测试
|
|
73
|
-
|
|
74
|
-
安装包后,可以这样测试:
|
|
75
|
-
|
|
76
|
-
```python
|
|
77
|
-
from fastapi import FastAPI
|
|
78
|
-
from fastapi_authly import setup_scalar_docs
|
|
79
|
-
|
|
80
|
-
app = FastAPI(title="Test API")
|
|
81
|
-
setup_scalar_docs(app)
|
|
82
|
-
|
|
83
|
-
# 运行应用后,访问 http://localhost:8000/docs 即可看到文档
|
|
84
|
-
```
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# Scalar API 文档功能使用指南
|
|
2
|
-
|
|
3
|
-
## 概述
|
|
4
|
-
|
|
5
|
-
`fastapi-authly` 现在内置了 Scalar API 文档支持,包含所有必要的静态资源文件。你无需手动复制静态文件,只需一行代码即可启用美观的 API 文档。
|
|
6
|
-
|
|
7
|
-
## 快速开始
|
|
8
|
-
|
|
9
|
-
### 基本使用
|
|
10
|
-
|
|
11
|
-
```python
|
|
12
|
-
from fastapi import FastAPI
|
|
13
|
-
from fastapi_authly import setup_scalar_docs
|
|
14
|
-
|
|
15
|
-
app = FastAPI(title="My API")
|
|
16
|
-
|
|
17
|
-
# 一行代码启用 Scalar 文档
|
|
18
|
-
setup_scalar_docs(app)
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
这将会:
|
|
22
|
-
- 自动挂载静态文件到 `/static`
|
|
23
|
-
- 创建文档页面到 `/docs`
|
|
24
|
-
- 使用应用的 `title` 和 `openapi_url`
|
|
25
|
-
|
|
26
|
-
### 完整示例
|
|
27
|
-
|
|
28
|
-
```python
|
|
29
|
-
from fastapi import FastAPI
|
|
30
|
-
from fastapi_authly import setup_scalar_docs, create_auth_router, AuthConfig, AuthDependencyConfig
|
|
31
|
-
from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
32
|
-
from tortoise.contrib.fastapi import register_tortoise
|
|
33
|
-
|
|
34
|
-
app = FastAPI(
|
|
35
|
-
title="Complaint API",
|
|
36
|
-
description="Complaint Data Streaming API",
|
|
37
|
-
version="2.0.1",
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
# 初始化数据库
|
|
41
|
-
register_tortoise(
|
|
42
|
-
app,
|
|
43
|
-
db_url="postgres://user:password@localhost:5432/mydb",
|
|
44
|
-
modules={"models": ["fastapi_authly.models.user"]},
|
|
45
|
-
generate_schemas=True,
|
|
46
|
-
add_exception_handlers=True,
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
# 配置认证路由
|
|
50
|
-
config = AuthConfig()
|
|
51
|
-
deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
52
|
-
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
53
|
-
app.include_router(auth_router)
|
|
54
|
-
|
|
55
|
-
# 设置 Scalar 文档
|
|
56
|
-
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
57
|
-
|
|
58
|
-
if __name__ == "__main__":
|
|
59
|
-
import uvicorn
|
|
60
|
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## 自定义配置
|
|
64
|
-
|
|
65
|
-
```python
|
|
66
|
-
setup_scalar_docs(
|
|
67
|
-
app,
|
|
68
|
-
docs_url="/api-docs", # 自定义文档 URL
|
|
69
|
-
static_url="/assets", # 自定义静态文件前缀
|
|
70
|
-
title="Custom API Docs", # 自定义标题
|
|
71
|
-
openapi_url="/openapi.json" # 自定义 OpenAPI schema URL
|
|
72
|
-
)
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
## 参数说明
|
|
76
|
-
|
|
77
|
-
- `app` (必需): FastAPI 应用实例
|
|
78
|
-
- `docs_url` (可选): 文档页面 URL,默认为 `"/docs"`
|
|
79
|
-
- `static_url` (可选): 静态文件 URL 前缀,默认为 `"/static"`
|
|
80
|
-
- `title` (可选): 文档标题,默认使用 `app.title`
|
|
81
|
-
- `openapi_url` (可选): OpenAPI schema URL,默认使用 `app.openapi_url`
|
|
82
|
-
|
|
83
|
-
## 注意事项
|
|
84
|
-
|
|
85
|
-
1. **静态文件位置**: 静态文件已经打包在 `fastapi_authly` 包内,无需手动复制
|
|
86
|
-
2. **URL 冲突**: 确保 `docs_url` 和 `static_url` 不与现有路由冲突
|
|
87
|
-
3. **OpenAPI Schema**: 确保 FastAPI 应用已启用 OpenAPI schema(默认启用)
|
|
88
|
-
|
|
89
|
-
## 迁移指南
|
|
90
|
-
|
|
91
|
-
如果你之前手动配置了 Scalar 文档,可以按以下步骤迁移:
|
|
92
|
-
|
|
93
|
-
### 之前的方式
|
|
94
|
-
|
|
95
|
-
```python
|
|
96
|
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
97
|
-
|
|
98
|
-
@app.get("/docs", include_in_schema=False)
|
|
99
|
-
async def scalar_html(_app):
|
|
100
|
-
html_content = f"""
|
|
101
|
-
<!DOCTYPE html>
|
|
102
|
-
...
|
|
103
|
-
"""
|
|
104
|
-
return HTMLResponse(content=html_content)
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### 现在的方式
|
|
108
|
-
|
|
109
|
-
```python
|
|
110
|
-
from fastapi_authly import setup_scalar_docs
|
|
111
|
-
|
|
112
|
-
# 一行代码替代上面的所有配置
|
|
113
|
-
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## 优势
|
|
117
|
-
|
|
118
|
-
1. **无需手动管理静态文件**: 所有静态资源已打包在包内
|
|
119
|
-
2. **简单易用**: 一行代码即可启用
|
|
120
|
-
3. **可移植**: 其他项目安装包后即可使用,无需额外配置
|
|
121
|
-
4. **可定制**: 支持自定义 URL 和标题
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
完整示例:展示如何像原项目一样使用 fastapi-authly 的文档功能
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from fastapi import FastAPI
|
|
6
|
-
from fastapi_authly import setup_scalar_docs, create_auth_router, AuthConfig, AuthDependencyConfig
|
|
7
|
-
from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
8
|
-
from tortoise.contrib.fastapi import register_tortoise, tortoise_exception_handlers
|
|
9
|
-
from contextlib import asynccontextmanager
|
|
10
|
-
|
|
11
|
-
# 生命周期管理
|
|
12
|
-
@asynccontextmanager
|
|
13
|
-
async def lifespan(app: FastAPI):
|
|
14
|
-
# 启动时执行
|
|
15
|
-
yield
|
|
16
|
-
# 关闭时执行
|
|
17
|
-
|
|
18
|
-
# 创建 FastAPI 应用
|
|
19
|
-
app = FastAPI(
|
|
20
|
-
title="Complaint API",
|
|
21
|
-
description="Complaint Data Streaming API",
|
|
22
|
-
version="2.0.1",
|
|
23
|
-
exception_handlers=tortoise_exception_handlers(),
|
|
24
|
-
lifespan=lifespan,
|
|
25
|
-
docs_url="/old_docs", # 保留旧的 docs,但使用 Scalar
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
# 初始化数据库
|
|
29
|
-
register_tortoise(
|
|
30
|
-
app,
|
|
31
|
-
db_url="postgres://user:password@localhost:5432/mydb",
|
|
32
|
-
modules={"models": ["fastapi_authly.models.user"]},
|
|
33
|
-
generate_schemas=True,
|
|
34
|
-
add_exception_handlers=True,
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
# 配置认证路由
|
|
38
|
-
config = AuthConfig()
|
|
39
|
-
deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
40
|
-
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
41
|
-
app.include_router(auth_router)
|
|
42
|
-
|
|
43
|
-
# 设置 Scalar 文档
|
|
44
|
-
# 这会自动挂载静态文件并创建文档页面
|
|
45
|
-
setup_scalar_docs(
|
|
46
|
-
app,
|
|
47
|
-
docs_url="/docs", # 文档页面 URL
|
|
48
|
-
static_url="/static", # 静态文件 URL 前缀(与原来的保持一致)
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
# 其他 API 路由
|
|
52
|
-
@app.get("/")
|
|
53
|
-
async def root():
|
|
54
|
-
return {"message": "API is running"}
|
|
55
|
-
|
|
56
|
-
if __name__ == "__main__":
|
|
57
|
-
import uvicorn
|
|
58
|
-
uvicorn.run(
|
|
59
|
-
app,
|
|
60
|
-
host="0.0.0.0",
|
|
61
|
-
port=3040,
|
|
62
|
-
loop="uvloop",
|
|
63
|
-
)
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
使用示例:如何在项目中使用 fastapi-authly 的 Scalar 文档功能
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from fastapi import FastAPI
|
|
6
|
-
from fastapi_authly import setup_scalar_docs, create_auth_router, AuthConfig, AuthDependencyConfig
|
|
7
|
-
from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
8
|
-
from tortoise.contrib.fastapi import register_tortoise
|
|
9
|
-
|
|
10
|
-
# 创建 FastAPI 应用
|
|
11
|
-
app = FastAPI(
|
|
12
|
-
title="My API",
|
|
13
|
-
description="My API with Scalar Documentation",
|
|
14
|
-
version="1.0.0",
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
# 配置认证路由(示例)
|
|
18
|
-
config = AuthConfig()
|
|
19
|
-
deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
20
|
-
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
21
|
-
app.include_router(auth_router)
|
|
22
|
-
|
|
23
|
-
# 设置 Scalar 文档
|
|
24
|
-
# 这会自动:
|
|
25
|
-
# 1. 挂载静态文件到 /static
|
|
26
|
-
# 2. 创建文档页面到 /docs
|
|
27
|
-
setup_scalar_docs(
|
|
28
|
-
app,
|
|
29
|
-
docs_url="/docs", # 文档页面 URL
|
|
30
|
-
static_url="/static", # 静态文件 URL 前缀
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
# 或者自定义配置
|
|
34
|
-
# setup_scalar_docs(
|
|
35
|
-
# app,
|
|
36
|
-
# docs_url="/api-docs", # 自定义文档 URL
|
|
37
|
-
# static_url="/assets", # 自定义静态文件前缀
|
|
38
|
-
# title="Custom API Docs", # 自定义标题
|
|
39
|
-
# openapi_url="/openapi.json", # 自定义 OpenAPI schema URL
|
|
40
|
-
# )
|
|
41
|
-
|
|
42
|
-
# 初始化数据库(示例)
|
|
43
|
-
register_tortoise(
|
|
44
|
-
app,
|
|
45
|
-
db_url="postgres://user:password@localhost:5432/mydb",
|
|
46
|
-
modules={"models": ["fastapi_authly.models.user"]},
|
|
47
|
-
generate_schemas=True,
|
|
48
|
-
add_exception_handlers=True,
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
if __name__ == "__main__":
|
|
52
|
-
import uvicorn
|
|
53
|
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/htmlcov/z_ddd01122054512b0_tortoise_pg_py.html
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_authly-0.1.2 → fastapi_authly-0.1.3}/src/fastapi_authly/static/scalar/standalone.js
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|