This commit is contained in:
amb
2026-09-02 17:08:52 +08:00
parent 6a2de55332
commit cecb5f2714
4 changed files with 15 additions and 2 deletions
+2 -1
View File
@@ -19,4 +19,5 @@ VOLUME /app/data
EXPOSE 8000 EXPOSE 8000
# 启动:先跑迁移,再起服务 # 启动:先跑迁移,再起服务
CMD ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 2"] # 注意:单 worker —— Session 存在进程内存中,多 worker 会导致登录态随机丢失
CMD ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 1"]
+10
View File
@@ -20,6 +20,9 @@ class Settings(BaseSettings):
# --- 基础 --- # --- 基础 ---
environment: str = "local" # local / production environment: str = "local" # local / production
secret_key: str = Field(min_length=16) secret_key: str = Field(min_length=16)
# Cookie Secure 标记:默认跟随 environmentproduction→Secure)。
# 站点还是 HTTP 时必须显式设为 false,否则浏览器丢弃 Cookie 导致登录后立即被踢回。
cookie_secure: bool | None = None
# --- 数据库 --- # --- 数据库 ---
database_url: str = "mysql+pymysql://admin:Lzcc6-01@47.109.98.44:33306/amb_rag?charset=utf8mb4" database_url: str = "mysql+pymysql://admin:Lzcc6-01@47.109.98.44:33306/amb_rag?charset=utf8mb4"
@@ -42,6 +45,13 @@ class Settings(BaseSettings):
def is_production(self) -> bool: def is_production(self) -> bool:
return self.environment == "production" return self.environment == "production"
@property
def use_secure_cookie(self) -> bool:
"""HTTPS 站点才应启用 Secure Cookie;未显式配置时跟随 environment。"""
if self.cookie_secure is not None:
return self.cookie_secure
return self.is_production
@property @property
def is_mysql(self) -> bool: def is_mysql(self) -> bool:
return "mysql" in self.database_url return "mysql" in self.database_url
+1 -1
View File
@@ -68,7 +68,7 @@ def get_cookie_params() -> dict:
"key": SESSION_COOKIE_NAME, "key": SESSION_COOKIE_NAME,
"httponly": True, "httponly": True,
"samesite": "lax", "samesite": "lax",
"secure": settings.is_production, "secure": settings.use_secure_cookie,
"max_age": SESSION_TTL_SECONDS, "max_age": SESSION_TTL_SECONDS,
"path": "/", "path": "/",
} }
+2
View File
@@ -15,6 +15,8 @@ services:
environment: environment:
ENVIRONMENT: production ENVIRONMENT: production
SECRET_KEY: ${SECRET_KEY:-change-me-to-a-real-secret-key} SECRET_KEY: ${SECRET_KEY:-change-me-to-a-real-secret-key}
# 站点未配置 HTTPS 时必须为 false,否则浏览器丢弃 Cookie(登录后被踢回登录页)
COOKIE_SECURE: "false"
DATABASE_URL: mysql+pymysql://admin:${MYSQL_PASSWORD:-Lzcc6-01}@host.docker.internal:33306/amb_rag?charset=utf8mb4 DATABASE_URL: mysql+pymysql://admin:${MYSQL_PASSWORD:-Lzcc6-01}@host.docker.internal:33306/amb_rag?charset=utf8mb4
STORAGE_ROOT: /app/data STORAGE_ROOT: /app/data
DEFAULT_STORAGE_QUOTA: "104857600" DEFAULT_STORAGE_QUOTA: "104857600"