2025-12-18 09:07:06 +03:00

31 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# routes/web.py
from fastapi import APIRouter, Request
from fastapi.templating import Jinja2Templates
from sqlalchemy import text
from app.database.session import engine
router = APIRouter()
templates = Jinja2Templates(directory="templates")
@router.get("/")
def home(request: Request):
db_info = {"status": "Не удалось подключиться", "url": ""}
try:
# Пробуем выполнить простой запрос
with engine.connect() as conn:
result = conn.execute(text("SELECT 1"))
if result.fetchone():
db_info["status"] = "✅ Подключено"
db_info["url"] = str(engine.url)
except Exception as e:
db_info["status"] = f"❌ Ошибка: {str(e)[:60]}..." # Обрезаем длинные ошибки
return templates.TemplateResponse(
"index.html",
{
"request": request,
"title": "FastAPI + SQLite",
"db_info": db_info
}
)