# vsrecorder/core-apiserver architecture

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

- Interactive diagram: https://gitdiagram.com/vsrecorder/core-apiserver
- Repository: https://github.com/vsrecorder/core-apiserver

## Overview

##### Main Components Identification
- Core API Server: This is the entrypoint (located in the cmd/core-apiserver directory with main.go) where the application boots up, loads configuration, and sets up routing.
- Controller Layer: Under internal/controller, this layer handles incoming HTTP requests (for various resources like “deck”, “match”, “official_event”, “record”, “tonamel_event”, “user” as well as authentication/authorization). This component is responsible for dealing with input DTOs and validating requests.
- Use Case (Application) Layer: Found under internal/usecase. This layer implements business logic for each domain (deck, game, match, etc.) and orchestrates interactions between controllers and domain logic.
- Domain Layer: Under internal/domain (further subdivided into entity and repository directories), this layer defines business entities (e.g., deck, game, match, etc.) and repository interface contracts.
- Infrastructure Layer: Under internal/infrastructure, this layer provides concrete implementations for the repository interfaces. It includes integrations with external services like Postgres (internal/infrastructure/postgres) and Firebase (internal/infrastructure/firebase), as well as model definitions.
- External Components:
- Database: The project has a “db” folder containing schema.sql, and there are Docker configurations indicating that a database (likely PostgreSQL) is part of the deployment.
- Third-Party Services: The Firebase client in internal/infrastructure/firebase shows integration with Firebase.
- Testing & Mocks: The internal directory also contains mock implementations (internal/mock) for repository and usecase, demonstrating a layered test strategy.

##### Relationships and Data Flow
- At runtime, the Core API Server (main.go) initializes the server and configures routes mapped to various controllers.
- The Controller layer receives HTTP requests, performs necessary request validations (including parsing and authorization) and then calls the appropriate Use Case functions.
- Use Cases encapsulate the business rules and interact with Domain Entities. They delegate data access operations to repository interfaces.
- The Infrastructure layer provides concrete implementations of these repository interfaces by connecting to PostgreSQL (or Firebase) to persist or retrieve data.
- Data (entities) flows from the Infrastructure (data persistence) back up to the Domain, then processed by Use Cases, and finally formatted by Controllers to respond to the API client.

##### Architectural Patterns and Design Principles
- Clean Architecture / Hexagonal Architecture: The separation into controllers, use cases, domain, and infrastructure shows a clear separation of concerns and dependency inversion – the core business logic is isolated from peripheral concerns.
- Repository Pattern: Domain repositories are defined in the domain layer and then implemented in the infrastructure layer to interact with data sources.
- Testing Strategy: Presence of mocks indicates that the project is designed with testability in mind.

##### Important Inclusions
- Clearly separate each of the following elements:
- Core API Server (cmd/core-apiserver)
- Controller Layer (internal/controller & its sub-components such as auth, deck, match, etc.)
- DTOs and Presenters (for input/output transformation)
- Use Case Layer (internal/usecase)
- Domain Layer (internal/domain/entities and repositories)
- Infrastructure Layer (internal/infrastructure including postgres and Firebase modules)
- Testing/Mock components (can be mentioned in notes as they show the design’s emphasis on testability)
- External Dependencies (Database schema from db/schema.sql and third-party integrations like Firebase)
- Containerization/Deployment Components (Dockerfile, docker-compose, and GitHub workflows)

##### Final Recommendations
- That every component is clearly labeled with its directory name or function.
- Clean architecture by grouping dependent components in clusters; for example, group all controllers together.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Server Initialization"
        A["Core API Server"]:::server
    end

    subgraph "Presentation Layer"
        B["Controller Layer"]:::controller
    end

    subgraph "Business Layer"
        C["Use Case Layer"]:::usecase
        D["Domain Layer"]:::domain
    end

    subgraph "Persistence Layer"
        E["Infrastructure Layer"]:::infrastructure
    end

    subgraph "External Dependencies"
        F["Database (schema)"]:::database
        G["Firebase Integration"]:::firebase
    end

    subgraph "Deployment Components"
        I["Dockerfile"]:::deployment
        J["docker-compose.yml"]:::deployment
    end

    H["Testing & Mocks"]:::testing

    A -->|"initializes_routing"| B
    B -->|"calls_UseCases"| C
    C -->|"executes_business_logic"| D
    D -->|"delegates_to"| E
    E -->|"returns_data_to"| D
    E -->|"persists_to"| F
    E -->|"integrates_with"| G
    C ---|"tested_via_mocks"| H
    D ---|"tested_via_mocks"| H
    A ---|"containerized_by"| I
    I ---|"with"| J

    click A "https://github.com/vsrecorder/core-apiserver/blob/main/cmd/core-apiserver/main.go"
    click B "https://github.com/vsrecorder/core-apiserver/tree/main/internal/controller"
    click C "https://github.com/vsrecorder/core-apiserver/tree/main/internal/usecase"
    click D "https://github.com/vsrecorder/core-apiserver/tree/main/internal/domain"
    click E "https://github.com/vsrecorder/core-apiserver/tree/main/internal/infrastructure"
    click F "https://github.com/vsrecorder/core-apiserver/blob/main/db/schema.sql"
    click G "https://github.com/vsrecorder/core-apiserver/blob/main/internal/infrastructure/firebase/client.go"
    click H "https://github.com/vsrecorder/core-apiserver/tree/main/internal/mock"
    click I "https://github.com/vsrecorder/core-apiserver/tree/main/Dockerfile"
    click J "https://github.com/vsrecorder/core-apiserver/blob/main/docker-compose.yml"

    classDef server fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef controller fill:#AED6F1,stroke:#2980B9,stroke-width:2px;
    classDef usecase fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef domain fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef infrastructure fill:#FCF3CF,stroke:#F1C40F,stroke-width:2px;
    classDef database fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef firebase fill:#D1F2EB,stroke:#16A085,stroke-width:2px;
    classDef testing fill:#D5DBDB,stroke:#7F8C8D,stroke-width:2px;
    classDef deployment fill:#FADBD8,stroke:#D35400,stroke-width:2px;
```
