Initial commit: Базовая структура сайта

This commit is contained in:
2026-02-11 12:06:30 +05:00
parent b41f161e8f
commit d9a2ad7f15
62 changed files with 3901 additions and 0 deletions

0
backend/app/core/__init__.py Executable file
View File

37
backend/app/core/config.py Executable file
View File

@@ -0,0 +1,37 @@
"""
Конфигурация приложения
"""
from pydantic_settings import BaseSettings
from typing import List
class Settings(BaseSettings):
# ManicTime Database (Read-Only)
MANICTIME_DB_HOST: str
MANICTIME_DB_PORT: int = 5432
MANICTIME_DB_NAME: str
MANICTIME_DB_USER: str
MANICTIME_DB_PASSWORD: str
# Service Database
SERVICE_DB_HOST: str
SERVICE_DB_PORT: int = 5432
SERVICE_DB_NAME: str
SERVICE_DB_USER: str
SERVICE_DB_PASSWORD: str
# JWT Settings
JWT_SECRET_KEY: str
JWT_ALGORITHM: str = "HS256"
JWT_ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
# CORS
CORS_ORIGINS: List[str] = ["http://localhost:3000", "http://localhost:5173"]
class Config:
env_file = ".env"
case_sensitive = True
settings = Settings()

103
backend/app/core/database.py Executable file
View File

@@ -0,0 +1,103 @@
"""
Управление подключениями к базам данных
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.core.config import settings
import logging
logger = logging.getLogger(__name__)
# Service Database (создаем первой, чтобы можно было читать конфигурацию)
service_engine = create_engine(
f"postgresql://{settings.SERVICE_DB_USER}:{settings.SERVICE_DB_PASSWORD}@"
f"{settings.SERVICE_DB_HOST}:{settings.SERVICE_DB_PORT}/{settings.SERVICE_DB_NAME}",
pool_pre_ping=True,
echo=False
)
ServiceSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=service_engine)
Base = declarative_base()
def get_manictime_config():
"""Получение конфигурации подключения к ManicTime из служебной БД"""
db = ServiceSessionLocal()
try:
from app.models.service_db import AppConfiguration
config = {}
config_items = db.query(AppConfiguration).filter(
AppConfiguration.key.in_(["manictime_host", "manictime_port", "manictime_dbname"])
).all()
for item in config_items:
key = item.key.replace("manictime_", "")
config[key] = item.value
# Используем значения из БД, если они есть, иначе из .env
host = config.get("host") or settings.MANICTIME_DB_HOST
port = config.get("port") or settings.MANICTIME_DB_PORT
dbname = config.get("dbname") or settings.MANICTIME_DB_NAME
return {
"host": host,
"port": port,
"dbname": dbname,
"user": settings.MANICTIME_DB_USER, # Всегда из .env
"password": settings.MANICTIME_DB_PASSWORD # Всегда из .env
}
except Exception as e:
logger.warning(f"Не удалось загрузить конфигурацию из БД, используем .env: {e}")
return {
"host": settings.MANICTIME_DB_HOST,
"port": settings.MANICTIME_DB_PORT,
"dbname": settings.MANICTIME_DB_NAME,
"user": settings.MANICTIME_DB_USER,
"password": settings.MANICTIME_DB_PASSWORD
}
finally:
db.close()
def get_manictime_engine():
"""Создание engine для ManicTime с учетом конфигурации из БД"""
config = get_manictime_config()
return create_engine(
f"postgresql://{config['user']}:{config['password']}@"
f"{config['host']}:{config['port']}/{config['dbname']}",
pool_pre_ping=True,
echo=False
)
# ManicTime Database (Read-Only) - создается динамически
manictime_engine = None
ManicTimeSessionLocal = None
def get_manictime_db():
"""Dependency для получения сессии ManicTime БД (read-only)"""
global manictime_engine, ManicTimeSessionLocal
# Создаем engine динамически, чтобы учитывать изменения конфигурации
if manictime_engine is None:
manictime_engine = get_manictime_engine()
ManicTimeSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=manictime_engine)
db = ManicTimeSessionLocal()
try:
yield db
finally:
db.close()
def get_service_db():
"""Dependency для получения сессии служебной БД"""
db = ServiceSessionLocal()
try:
yield db
finally:
db.close()

74
backend/app/core/security.py Executable file
View File

@@ -0,0 +1,74 @@
"""
Модуль безопасности: JWT, хеширование паролей
"""
from datetime import datetime, timedelta
from typing import Optional
from jose import JWTError, jwt
from passlib.context import CryptContext
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlalchemy.orm import Session
from app.core.config import settings
from app.core.database import get_service_db
from app.models.service_db import AppUser
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
security = HTTPBearer()
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""Проверка пароля"""
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
"""Хеширование пароля"""
return pwd_context.hash(password)
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
"""Создание JWT токена"""
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=settings.JWT_ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, settings.JWT_SECRET_KEY, algorithm=settings.JWT_ALGORITHM)
return encoded_jwt
async def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security),
db: Session = Depends(get_service_db)
) -> AppUser:
"""Получение текущего пользователя из JWT токена"""
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Не удалось проверить учетные данные",
headers={"WWW-Authenticate": "Bearer"},
)
try:
token = credentials.credentials
payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithms=[settings.JWT_ALGORITHM])
user_id: int = payload.get("sub")
if user_id is None:
raise credentials_exception
except JWTError:
raise credentials_exception
user = db.query(AppUser).filter(AppUser.id == user_id, AppUser.is_active == True).first()
if user is None:
raise credentials_exception
return user
async def get_admin_user(current_user: AppUser = Depends(get_current_user)) -> AppUser:
"""Проверка, что пользователь является администратором"""
if current_user.role.role_name != "Admin":
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Недостаточно прав доступа"
)
return current_user