This commit is contained in:
Redsandy
2026-08-12 11:26:05 +03:00
commit c9f66f330e
17 changed files with 1569 additions and 0 deletions

27
app/core/config.py Normal file
View File

@@ -0,0 +1,27 @@
from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
database_url: str = "sqlite:///./vyryi.db"
jwt_secret: str = "dev-secret-change-me"
jwt_algorithm: str = "HS256"
jwt_expire_minutes: int = 60 * 24 * 7
cors_origins: str = (
"http://localhost:5173,http://127.0.0.1:5173,"
"http://localhost:5174,http://127.0.0.1:5174"
)
cors_origin_regex: str = r"http://(localhost|127\.0\.0\.1):\d+"
bot_token: str = ""
@property
def cors_origins_list(self) -> list[str]:
return [origin.strip() for origin in self.cors_origins.split(",") if origin.strip()]
@lru_cache
def get_settings() -> Settings:
return Settings()

27
app/core/database.py Normal file
View File

@@ -0,0 +1,27 @@
from collections.abc import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
from app.core.config import get_settings
settings = get_settings()
connect_args: dict = {}
if settings.database_url.startswith("sqlite"):
connect_args = {"check_same_thread": False}
engine = create_engine(settings.database_url, echo=False, connect_args=connect_args)
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
class Base(DeclarativeBase):
pass
def get_db() -> Generator[Session, None, None]:
db = SessionLocal()
try:
yield db
finally:
db.close()

22
app/core/security.py Normal file
View File

@@ -0,0 +1,22 @@
from datetime import datetime, timedelta, timezone
from jose import JWTError, jwt
from app.core.config import get_settings
def create_access_token(subject: str) -> str:
settings = get_settings()
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.jwt_expire_minutes)
payload = {"sub": subject, "exp": expire}
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
def decode_access_token(token: str) -> str | None:
settings = get_settings()
try:
payload = jwt.decode(token, settings.jwt_secret, algorithms=[settings.jwt_algorithm])
except JWTError:
return None
subject = payload.get("sub")
return str(subject) if subject is not None else None