refactor: extract all routes to app/api/v1/endpoints/ with proper structure
This commit is contained in:
75
app/api/v1/endpoints/pages.py
Normal file
75
app/api/v1/endpoints/pages.py
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
LogBoard+ - Страницы API
|
||||
Автор: Сергей Антропов
|
||||
Сайт: https://devops.org.ru
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Request, Depends, HTTPException
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse, PlainTextResponse
|
||||
|
||||
from app.core.auth import verify_token, get_current_user
|
||||
from app.core.config import templates, AJAX_UPDATE_INTERVAL, DEFAULT_TAIL, SKIP_UNHEALTHY
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def index(request: Request):
|
||||
"""Главная страница приложения"""
|
||||
# Проверяем наличие токена в cookie
|
||||
access_token = request.cookies.get("access_token")
|
||||
if not access_token:
|
||||
return RedirectResponse(url="/login")
|
||||
|
||||
# Проверяем валидность токена
|
||||
username = verify_token(access_token)
|
||||
if not username:
|
||||
return RedirectResponse(url="/login")
|
||||
|
||||
return templates.TemplateResponse("index.html", {"request": request})
|
||||
|
||||
@router.get("/login", response_class=HTMLResponse)
|
||||
async def login_page(request: Request):
|
||||
"""Страница входа"""
|
||||
return templates.TemplateResponse("login.html", {"request": request})
|
||||
|
||||
@router.get("/healthz", response_class=PlainTextResponse)
|
||||
def healthz():
|
||||
"""Health check endpoint"""
|
||||
return "ok"
|
||||
|
||||
@router.get("/settings")
|
||||
async def get_settings(current_user: str = Depends(get_current_user)):
|
||||
"""Получить настройки приложения"""
|
||||
return {
|
||||
"ajax_update_interval": AJAX_UPDATE_INTERVAL,
|
||||
"default_tail": DEFAULT_TAIL,
|
||||
"skip_unhealthy": SKIP_UNHEALTHY
|
||||
}
|
||||
|
||||
# Маршруты для тестирования страниц ошибок (только в режиме разработки)
|
||||
@router.get("/test/error/404")
|
||||
async def test_404_error():
|
||||
"""Тест страницы ошибки 404"""
|
||||
raise HTTPException(status_code=404, detail="Тестовая ошибка 404")
|
||||
|
||||
@router.get("/test/error/401")
|
||||
async def test_401_error():
|
||||
"""Тест страницы ошибки 401"""
|
||||
raise HTTPException(status_code=401, detail="Тестовая ошибка 401")
|
||||
|
||||
@router.get("/test/error/403")
|
||||
async def test_403_error():
|
||||
"""Тест страницы ошибки 403"""
|
||||
raise HTTPException(status_code=403, detail="Тестовая ошибка 403")
|
||||
|
||||
@router.get("/test/error/500")
|
||||
async def test_500_error():
|
||||
"""Тест страницы ошибки 500"""
|
||||
raise HTTPException(status_code=500, detail="Тестовая ошибка 500")
|
||||
|
||||
@router.get("/test/error/general")
|
||||
async def test_general_error():
|
||||
"""Тест общей ошибки"""
|
||||
raise Exception("Тестовая общая ошибка")
|
||||
Reference in New Issue
Block a user