Complete admin package restructuring
This commit is contained in:
@ -0,0 +1,70 @@
|
||||
"""Front page view factory."""
|
||||
|
||||
# pyright: reportUnusedFunction=false
|
||||
import logging
|
||||
from typing import Annotated
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
from pydantic import BaseModel
|
||||
from sshecret_admin.auth import User
|
||||
from sshecret_admin.services import AdminBackend
|
||||
|
||||
from ..dependencies import FrontendDependencies
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
START_PAGE = "/dashboard"
|
||||
LOGIN_PAGE = "/login"
|
||||
|
||||
|
||||
class StatsView(BaseModel):
|
||||
"""Stats for the frontend."""
|
||||
|
||||
clients: int = 0
|
||||
secrets: int = 0
|
||||
audit_events: int = 0
|
||||
|
||||
|
||||
async def get_stats(admin: AdminBackend) -> StatsView:
|
||||
"""Get stats for the frontpage."""
|
||||
clients = await admin.get_clients()
|
||||
secrets = await admin.get_secrets()
|
||||
audit = await admin.get_audit_log_count()
|
||||
return StatsView(clients=len(clients), secrets=len(secrets), audit_events=audit)
|
||||
|
||||
|
||||
def create_router(dependencies: FrontendDependencies) -> APIRouter:
|
||||
"""Create auth router."""
|
||||
|
||||
app = APIRouter()
|
||||
templates = dependencies.templates
|
||||
|
||||
@app.get("/")
|
||||
def get_index(logged_in: Annotated[bool, Depends(dependencies.get_login_status)]):
|
||||
"""Get the index."""
|
||||
next = LOGIN_PAGE
|
||||
if logged_in:
|
||||
next = START_PAGE
|
||||
|
||||
return RedirectResponse(url=next)
|
||||
|
||||
@app.get("/dashboard")
|
||||
async def get_dashboard(
|
||||
request: Request,
|
||||
admin: Annotated[AdminBackend, Depends(dependencies.get_admin_backend)],
|
||||
current_user: Annotated[User, Depends(dependencies.get_user_from_access_token)],
|
||||
):
|
||||
"""Dashboard for mocking up the dashboard."""
|
||||
stats = await get_stats(admin)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"dashboard.html",
|
||||
{
|
||||
"page_title": "sshecret",
|
||||
"user": current_user.username,
|
||||
"stats": stats,
|
||||
},
|
||||
)
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user