From 8e91724462b4e4f8e5005e13ec209f1b9c86ad0e Mon Sep 17 00:00:00 2001 From: julius Date: Thu, 20 Feb 2025 08:52:48 +0100 Subject: [PATCH] feat: features for bad internet connections --- db.py | 2 +- security.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/db.py b/db.py index c5d9c2b..dc61f42 100644 --- a/db.py +++ b/db.py @@ -12,7 +12,7 @@ from sqlmodel import ( with open("db.secrets", "r") as f: db_secrets = f.readline().strip() -engine = create_engine(db_secrets) +engine = create_engine(db_secrets, connect_args={"connect_timeout": 8}) del db_secrets diff --git a/security.py b/security.py index 71e1d84..85d76c5 100644 --- a/security.py +++ b/security.py @@ -9,6 +9,7 @@ from db import engine, User from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from pydantic_settings import BaseSettings, SettingsConfigDict from passlib.context import CryptContext +from sqlalchemy.exc import OperationalError class Config(BaseSettings): @@ -47,10 +48,13 @@ def get_password_hash(password): def get_user(username: str | None): if username: - with Session(engine) as session: - return session.exec( - select(User).where(User.username == username) - ).one_or_none() + try: + with Session(engine) as session: + return session.exec( + select(User).where(User.username == username) + ).one_or_none() + except OperationalError: + return def authenticate_user(username: str, password: str):