feat: FastAPI 直接加载 SSL,ssl 目录纳入 gitignore

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
shijing 2026-07-10 15:55:02 +08:00
parent 6eb91076c5
commit 96241790dd
3 changed files with 27 additions and 4 deletions

1
.gitignore vendored
View File

@ -21,6 +21,7 @@ Thumbs.db
!.env.example !.env.example
*.pem *.pem
*.key *.key
ssl/
# ============ Backend (Python) ============ # ============ Backend (Python) ============
# 字节码 / 缓存 # 字节码 / 缓存

View File

@ -11,6 +11,9 @@ from app.db import SessionLocal, init_db
from app.services.user_service import ensure_default_admin from app.services.user_service import ensure_default_admin
FRONTEND_DIST = Path(__file__).resolve().parent.parent / "dist" FRONTEND_DIST = Path(__file__).resolve().parent.parent / "dist"
SSL_DIR = Path(__file__).resolve().parent.parent.parent / "ssl"
SSL_CERT_FILE = SSL_DIR / "finai.ctc-zc.com.pem"
SSL_KEY_FILE = SSL_DIR / "finai.ctc-zc.com.key"
def create_app() -> FastAPI: def create_app() -> FastAPI:
@ -69,3 +72,22 @@ def _mount_frontend(app: FastAPI) -> None:
app = create_app() app = create_app()
if __name__ == "__main__":
import uvicorn
ssl_kwargs: dict = {}
if SSL_CERT_FILE.is_file() and SSL_KEY_FILE.is_file():
ssl_kwargs = {
"ssl_certfile": str(SSL_CERT_FILE),
"ssl_keyfile": str(SSL_KEY_FILE),
}
uvicorn.run(
"app.main:app",
host="0.0.0.0",
port=8775,
reload=True,
**ssl_kwargs,
)

View File

@ -1,4 +1,4 @@
@echo off @echo off
chcp 65001 >nul chcp 65001 >nul
setlocal setlocal
@ -12,8 +12,8 @@ if not exist ".venv\Scripts\python.exe" (
".venv\Scripts\python.exe" -m pip install fastapi "uvicorn[standard]" sqlalchemy "psycopg[binary]" pydantic-settings "python-jose[cryptography]" "passlib[bcrypt]" python-multipart httpx tencentcloud-sdk-python-lke || goto :error ".venv\Scripts\python.exe" -m pip install fastapi "uvicorn[standard]" sqlalchemy "psycopg[binary]" pydantic-settings "python-jose[cryptography]" "passlib[bcrypt]" python-multipart httpx tencentcloud-sdk-python-lke || goto :error
) )
echo [backend] 启动 uvicorn app.main:app http://127.0.0.1:8775 echo [backend] 启动 uvicorn app.main:app https://0.0.0.0:8775 (SSL)
".venv\Scripts\python.exe" -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8775 ".venv\Scripts\python.exe" -m app.main
goto :end goto :end
:error :error
@ -23,4 +23,4 @@ exit /b 1
:end :end
endlocal endlocal
pause pause