# neko-dream/api architecture

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

- Interactive diagram: https://gitdiagram.com/neko-dream/api
- Repository: https://github.com/neko-dream/api

## Overview

##### Main Components

- API Server & CLI Tools
- The “cmd/server” directory contains the entry point for the API server, which listens for incoming HTTP requests.
- The “cmd/migration” directory holds a migration tool (for database encryption of demographics data) to assist in database setup and migrations.

- API Specification (OpenAPI)
- The “api” directory and subdirectories (especially “analysis.openapi.json”, “apidog.openapi.yaml”, etc.) indicate the project’s API specification. This is crucial to understand the endpoints and operations.

- Presentation Layer
- Found in “internal/presentation/handler” and “internal/presentation/oas”, this layer is responsible for routing HTTP requests, handling response formatting, and integrating with middleware (observed in “internal/infrastructure/http/middleware”).

- Use Case Layer
- “internal/usecase” organizes command and query operations. This layer orchestrates business logic by coordinating between the presentation layer and the domain layer.

- Domain Layer
- “internal/domain” is the heart of the business logic including domain models (e.g., user, opinion, talksession, vote) and domain services. This helps encapsulate the core rules and behaviors of the system.

- Infrastructure Layer
- This layer (inside “internal/infrastructure”) handles concerns such as persistence (PostgreSQL via “persistence”), authentication (JWT, OAuth in “auth”), cryptography, telemetries (sentry, otelinji) and external integrations.
- The “internal/infrastructure/di” directory indicates the use of dependency injection, uniting the domains, use cases, and presentations with their respective infrastructure implementations.
- Database connectivity and management is split between “internal/infrastructure/persistence” (repositories, query building, and SQLC generated files) and migration scripts are in the “migrations” folder.
- External client communication modules (e.g., “internal/infrastructure/external/analysis”) represent integration with third-party services.

- Deployment & DevOps
- Docker- and air-related files (“compose.yaml”, “container/Dockerfile”, .air.toml, GitHub workflows) help define the environment and deployment automation.
- The project uses Docker Compose to manage the PostgreSQL database (noting that it requires PostgreSQL 16 with PostGIS).

##### Component Interactions

- Directional Data Flow:
- External clients (via REST/OpenAPI) send HTTP requests to the API Server.
- The entry point in “cmd/server” directs these requests to the presentation layer (HTTP handlers).
- Handlers trigger appropriate use case logic in “internal/usecase” (commands and queries).
- The use cases invoke domain services/logic (located in “internal/domain”).
- Persistence/repository calls in “internal/infrastructure/persistence” fetch or modify data in the PostgreSQL database.
- Authentication components based on JWT and OAuth (in “internal/infrastructure/auth”) protect and validate requests.
- Other external services (e.g., analysis via “internal/infrastructure/external/analysis”) are invoked as needed.

- Additional Flows:
- Middleware (in “internal/infrastructure/http/middleware”) intercepts requests to handle authentication, logging, or error management.
- The DI setup in “internal/infrastructure/di” wires together all the above layers.
- The generated OpenAPI specifications and scripts (in “scripts/”) support automated code generation and testing.

##### Architectural Patterns & Design Principles

- Clean/Onion Architecture:
- Presentation, use case, domain, and infrastructure layers are cleanly separated.
- Business rules reside in the domain layer to ensure that infrastructure concerns (such as DB access and HTTP handling) remain independent.

- Dependency Injection:
- The DI folders link the layers while promoting testability and loose coupling.

- Repository & Query Patterns:
- Persistence code uses SQLC-generated files and query-specific modules to abstract database operations.

##### Final Elements

- Each component must be clearly labeled (e.g., “API Server – cmd/server”, “HTTP Handlers”, “Use Cases”, “Domain Models”, “Repositories & Persistence”, “Authentication (JWT/OAuth)”, “External Services”, “Database (PostgreSQL with PostGIS)”).
- Rectangles for application layers/components.
- Optionally, include the build/deployment pipeline showing integration with the GitHub workflows and Docker Compose.

This structured approach will enable the stakeholders to quickly understand the architecture and the separation of concerns that ensures maintainability, testability, and scalability of the project.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Points
    subgraph "Entry Points"
        External_Client["External Clients"]:::infra
        API_Server["API Server"]:::api
        CLI_Migration["CLI Migration Tool"]:::api
    end

    %% API Specification
    API_Spec["API Specification"]:::devops

    %% Presentation Layer
    subgraph "Presentation Layer"
        HTTP_Handlers["HTTP Handlers"]:::pres
        OAS["OAS Documentation"]:::pres
    end

    %% Use Case Layer
    Use_Cases["Use Cases"]:::usecase

    %% Domain Layer
    Domain["Domain Models & Services"]:::domain

    %% Infrastructure Layer
    subgraph "Infrastructure"
        DI["Dependency Injection"]:::infra
        Persistence["Repositories & Persistence"]:::infra
        Auth["Authentication (JWT/OAuth)"]:::infra
        Middleware["HTTP Middleware"]:::infra
        External_Services["External Services"]:::infra
        Telemetry["Telemetry"]:::infra
    end

    %% Database
    DB["PostgreSQL with PostGIS"]:::db

    %% Deployment & DevOps
    subgraph "Deployment & DevOps"
        DevOps["Docker & Environment"]:::devops
        Migrations["Database Migrations"]:::devops
        Scripts["Supporting Scripts"]:::devops
    end

    %% Data Flow Connections
    External_Client -->|"sends requests"| API_Server
    API_Spec -->|"defines API"| API_Server
    API_Spec -->|"guides routing"| HTTP_Handlers

    API_Server -->|"routes to"| HTTP_Handlers
    HTTP_Handlers -->|"calls"| Use_Cases
    Use_Cases -->|"orchestrates"| Domain
    Domain -->|"persists data via"| Persistence
    Persistence -->|"reads/writes"| DB

    %% Middleware and Authentication Flow
    HTTP_Handlers -->|"passes through"| Middleware
    Middleware -->|"validates via"| Auth

    %% External Services
    Use_Cases -->|"integrates with"| External_Services

    %% Dependency Injection Wiring (Dotted Lines)
    DI -.-> HTTP_Handlers
    DI -.-> Use_Cases
    DI -.-> Domain
    DI -.-> Persistence

    %% Deployment interactions
    DevOps -->|"deploys"| API_Server
    DevOps -->|"provisions"| DB
    CLI_Migration -->|"runs migrations"| Migrations
    Scripts -->|"support automation"| DevOps

    %% Click Events
    click API_Server "https://github.com/neko-dream/api/blob/develop/cmd/server/main.go"
    click CLI_Migration "https://github.com/neko-dream/api/blob/develop/cmd/migration/encrypt_demographics/main.go"
    click API_Spec "https://github.com/neko-dream/api/tree/develop/api"
    click HTTP_Handlers "https://github.com/neko-dream/api/tree/develop/internal/presentation/handler"
    click OAS "https://github.com/neko-dream/api/tree/develop/internal/presentation/oas"
    click Use_Cases "https://github.com/neko-dream/api/tree/develop/internal/usecase"
    click Domain "https://github.com/neko-dream/api/tree/develop/internal/domain"
    click DI "https://github.com/neko-dream/api/tree/develop/internal/infrastructure/di"
    click Persistence "https://github.com/neko-dream/api/tree/develop/internal/infrastructure/persistence"
    click Auth "https://github.com/neko-dream/api/tree/develop/internal/infrastructure/auth"
    click Middleware "https://github.com/neko-dream/api/tree/develop/internal/infrastructure/http/middleware"
    click External_Services "https://github.com/neko-dream/api/tree/develop/internal/infrastructure/external/analysis"
    click Telemetry "https://github.com/neko-dream/api/tree/develop/internal/infrastructure/telemetry"
    click DevOps "https://github.com/neko-dream/api/blob/develop/compose.yaml"
    click Migrations "https://github.com/neko-dream/api/tree/develop/migrations"
    click Scripts "https://github.com/neko-dream/api/tree/develop/scripts"

    %% Styles
    classDef api fill:#f9d9d9,stroke:#d9534f,stroke-width:2px;
    classDef pres fill:#d9edf7,stroke:#5bc0de,stroke-width:2px;
    classDef usecase fill:#fcf8e3,stroke:#f0ad4e,stroke-width:2px;
    classDef domain fill:#dff0d8,stroke:#5cb85c,stroke-width:2px;
    classDef infra fill:#eeeeee,stroke:#aaa,stroke-width:2px;
    classDef devops fill:#f5f5f5,stroke:#ccc,stroke-width:2px;
    classDef db fill:#ffffff,stroke:#000000,stroke-dasharray: 5 5,stroke-width:2px;
```
