# kksctf/yatb architecture

> Architecture diagram of the GitHub repository kksctf/yatb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kksctf/yatb
- Repository: https://github.com/kksctf/yatb

## Overview

##### Project Type and Purpose
- YATB is a web application designed to serve both API endpoints and HTML-based views for different user roles (admin and basic users). The README indicates that the project is launched via uvicorn, suggesting it is built on an ASGI framework (likely FastAPI). There’s a clear separation of concerns in the file structure for API endpoints, admin routes, CLI commands, database interactions (using Beanie), static files, and templates.
- The project also supports CLI operations (through the cli/ package) and WebSocket interactions (ws.py). Deployment is supported via Docker and docker-compose, with configuration hints for nginx as a reverse proxy.

##### Analyzing the File Structure
- Top-Level Directories/Files:
- app: Contains the core application code including the API endpoints (app/api), CLI commands (app/cli), database interactions (app/db), plugins, schemas, tests, utility functions, and web view components.
- view: Houses HTML templates and static assets, evidencing separation between UI/presentation and application logic.
- Configuration & Deployment: Dockerfile.* files, docker-compose.yml, nginx configs, and various requirements files indicate readiness for multiple environments.
- CLI and auxiliary scripts: cli.py and flag_generator.py suggest additional command-line utilities.
- Architecture Implications:
- An MVC-like separation is apparent (controllers under API, views under app/view).
- Modular design with clear segmentation for authentication (api_auth, schema/auth), data models (schema/*), and database operations (db/*).

##### Key Components
a) Main Application Server:
- Uvicorn/ASGI server launching main:app as the central component that handles incoming HTTP requests.
b) API Layer:
- Subdivide into endpoints for admin (app/api/admin) and other domains (api_tasks, api_users, and api_auth). Indicate interactions with authentication modules from app/schema/auth.
c) Web Views/Frontend:
- Rendering of HTML templates and static assets served from the view directory for both user-facing pages (login, tasks, scoreboard) and admin pages.
d) Database Layer:
e) CLI Utilities:
- Components under app/cli (including command modules like get, load, stress) that interact directly with application logic.
f) Plugins and Utilities:
- Plugin system (app/plugins), which might extend or modify system behavior, plus various “utils” for logging, metrics, markdown processing, etc.
g) External and Infrastructure Components:
- Docker and docker-compose files, plus nginx configuration files, indicate that deployment and reverse-proxy setup are important parts of the architecture.

##### Relationships and Interactions
- HTTP Requests: Incoming requests (via a client web browser or API calls) hit the uvicorn server and are routed either to the API endpoints or to the view renderers.
- API to Database: API modules (including admin and user-specific APIs) interface with the database layer, performing CRUD operations via the Beanie integration.
- CLI Interaction: CLI commands access core modules (e.g., db, api, tasks) for administrative, debugging, or data import/export tasks.
- Middleware and Plugins: Optionally, represent middleware components for authentication (via token or OAuth strategies) that intercept or augment requests.
- Infrastructure Flow: Incorporate Docker and nginx as elements that manage deployment, load balancing, and reverse proxying.

- Don’t skimp on details – break down the project into as many logical components as possible.
- Both runtime (web server, API, DB) and developer utilities (CLI, testing) to give a full picture of how the system operates.
- If possible, include deployment-specific elements (Docker containers, reverse proxy configurations) since they are crucial for understanding the production environment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction
    Client("Client (Browser/API)"):::external

    %% Application Server Layer
    subgraph "Application Server"
        ASGI("ASGI Server (uvicorn)"):::backend
        subgraph "API Layer"
            APIAdmin("API - Admin Endpoints"):::backend
            APIGeneral("API - General Endpoints"):::backend
            Auth("Authentication Module"):::backend
        end
        Views("Web Views & Static Assets"):::frontend
        Database("Database (Beanie)"):::database
        CLI("CLI Utilities"):::backend
        Plugins("Plugin System"):::backend
        Utils("Utility Functions"):::backend
    end

    %% Deployment & Infrastructure
    subgraph "Deployment & Infrastructure"
        DockerProd("Dockerfile.production"):::deployment
        DockerTest("Dockerfile.test"):::deployment
        Compose("docker-compose.yml"):::deployment
        Nginx("nginx Configurations"):::deployment
    end

    %% Connections
    Client -->|"HTTP_Request"| ASGI
    ASGI -->|"Routes_Admin"| APIAdmin
    ASGI -->|"Routes_API"| APIGeneral
    ASGI -->|"Serves_HTML"| Views
    ASGI -->|"Uses_Plugins"| Plugins
    ASGI -->|"Utilizes_Utils"| Utils

    APIAdmin -->|"DB_Operations"| Database
    APIGeneral -->|"DB_Operations"| Database
    APIGeneral -->|"Auth_Check"| Auth

    CLI -->|"Executes_DB_Tasks"| Database
    CLI -->|"Triggers_API"| APIAdmin
    CLI -->|"Triggers_API"| APIGeneral

    DockerProd -->|"Containerizes"| ASGI
    DockerTest -->|"Containerizes_Test"| ASGI
    Compose -->|"Orchestrates"| ASGI
    Nginx -->|"Reverse_Proxies"| ASGI

    %% Click Events
    click ASGI "https://github.com/kksctf/yatb/blob/master/app/main.py"
    click ASGI "https://github.com/kksctf/yatb/blob/master/main.py"
    click APIAdmin "https://github.com/kksctf/yatb/tree/master/app/api/admin"
    click APIGeneral "https://github.com/kksctf/yatb/tree/master/app/api"
    click Views "https://github.com/kksctf/yatb/tree/master/app/view/templates"
    click Views "https://github.com/kksctf/yatb/tree/master/app/view/static"
    click Database "https://github.com/kksctf/yatb/tree/master/app/db"
    click CLI "https://github.com/kksctf/yatb/tree/master/app/cli"
    click CLI "https://github.com/kksctf/yatb/blob/master/cli.py"
    click CLI "https://github.com/kksctf/yatb/blob/master/flag_generator.py"
    click Auth "https://github.com/kksctf/yatb/tree/master/app/schema/auth"
    click Plugins "https://github.com/kksctf/yatb/tree/master/app/plugins"
    click Utils "https://github.com/kksctf/yatb/tree/master/app/utils"
    click DockerProd "https://github.com/kksctf/yatb/blob/master/Dockerfile.production"
    click DockerTest "https://github.com/kksctf/yatb/blob/master/Dockerfile.test"
    click Compose "https://github.com/kksctf/yatb/blob/master/docker-compose.yml"
    click Nginx "https://github.com/kksctf/yatb/tree/master/nginx"

    %% Styles
    classDef external fill:#f8e71c,stroke:#333,stroke-width:2px;
    classDef backend fill:#8dc63f,stroke:#333,stroke-width:2px;
    classDef frontend fill:#50e3c2,stroke:#333,stroke-width:2px;
    classDef database fill:#f5a623,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef deployment fill:#d0021b,stroke:#333,stroke-width:2px;
    
    %% End of Diagram
```
