# isadoramsouza/rinha-backend-go-2024-q1 architecture

> Architecture diagram of the GitHub repository isadoramsouza/rinha-backend-go-2024-q1, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/isadoramsouza/rinha-backend-go-2024-q1
- Repository: https://github.com/isadoramsouza/rinha-backend-go-2024-q1

## Overview

##### Project Type and Purpose
- This is a single-service, full-stack backend API written in Go (using the Gin framework) that implements two endpoints for transaction recording and statement retrieval.
- It uses PostgreSQL for persistence and NGINX as a reverse proxy/load-balancer, all orchestrated via Docker Compose.

##### File Structure
- cmd/api/: application entrypoint
- main.go: bootstraps Gin, loads routes (cmd/api/routes/routes.go) and handlers (cmd/api/handler/transacao.go).
- internal/: business logic
- domain/: core entities (cliente, transacao, extrato)
- transacao/: service layer (service.go) + repository (repository.go)
- pkg/web/: response models/serializers
- config/deploy: Dockerfile, docker-compose.yml, docker-compose-local.yml, nginx.conf, init.sql
- Scripts: run.sh for local startup
- CI: .github/workflows/ci.yml

##### README Highlights
- Two HTTP endpoints (POST /clientes/:id/transacoes, GET /clientes/:id/extrato)
- Stack: Go 1.21 (Gin), PostgreSQL, NGINX
- Run via `docker-compose up -d` exposing port 9999

##### Main Components and Their Relationships
a. Client (Postman/cURL/Browser)
b. NGINX reverse proxy (nginx.conf)
c. Go/Gin API service
- HTTP Router → Handlers → Service layer → Repository → PostgreSQL
d. PostgreSQL database
e. Supporting artifacts
- init.sql for schema
- Docker containers

##### Architectural Patterns & Design Principles
- Clean/Chex pattern: separation of layers (presentation in cmd/, domain in internal/domain, application logic in internal/transacao, delivery in pkg/web)
- Dependency inversion: handlers depend on service interfaces, services depend on repository interfaces
- Containerization: each component (NGINX, API, Postgres) runs in its own Docker container

##### Essential Elements
- Clear labels on all components (e.g., “Gin API Service,” “PostgreSQL DB,” “NGINX Proxy”)
- Version or config references (e.g., “↓ port 9999,” “init.sql → DB schema”)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["Client (Postman/cURL)"]:::client

    %% Deployment Configurations
    subgraph "Deployment Config"
        direction TB
        composeProd["docker-compose.yml"]:::config
        composeLocal["docker-compose-local.yml"]:::config
        dockerfile["Dockerfile"]:::config
        nginxConf["nginx.conf"]:::config
        initSql["init.sql"]:::config
        runSh["run.sh"]:::config
    end

    %% Docker Containers Group
    subgraph "Docker Containers" 
        direction TB

        %% NGINX Proxy Container
        subgraph "NGINX Proxy"
            nginx["NGINX Proxy"]:::proxy
        end

        %% API Service Container
        subgraph "Go/Gin API Service"
            direction TB
            main["API Entrypoint\n(cmd/api/main.go)"]:::api
            router["Gin Router\n(cmd/api/routes/routes.go)"]:::api
            handler["Transaction Handlers\n(cmd/api/handler/transacao.go)"]:::api
            service["Transaction Service\n(internal/transacao/service.go)"]:::service
            repository["Transaction Repository\n(internal/transacao/repository.go)"]:::service
            domainModels["Domain Models\n(internal/domain/*.go)"]:::domain
            webModels["Web Models\n(pkg/web/response.go)"]:::api
        end

        %% PostgreSQL Container
        subgraph "PostgreSQL"
            db["PostgreSQL DB\n(port 5432)"]:::db
        end
    end

    %% CI/CD Pipeline
    subgraph "CI/CD Pipeline"
        ci["GitHub Actions\n(.github/workflows/ci.yml)"]:::ci
    end

    %% Request Flow
    Client -->|"HTTP/JSON\n:9999"| nginx
    nginx -->|"Proxy to API\nHTTP/JSON"| main
    main -->|"Loads Routes"| router
    router -->|"Handles Endpoints"| handler
    handler -->|"Calls Service"| service
    service -->|"Uses Repository"| repository
    repository -->|"SQL Queries"| db
    db -->|"SQL Results"| repository
    repository -->|"Data"| service
    service -->|"Response Objects"| webModels
    webModels -->|"JSON"| handler
    handler -->|"HTTP Response"| router
    router -->|"HTTP/JSON"| main
    main -->|"Response via NGINX"| nginx
    nginx -->|"HTTP/JSON"| Client

    %% Configurations to Containers
    composeProd --> nginx
    composeProd --> main
    composeProd --> db
    composeLocal --> nginx
    composeLocal --> main
    composeLocal --> db
    dockerfile --> main
    nginxConf --> nginx
    initSql --> db
    runSh --> composeLocal
    ci --> composeProd

    %% Domain Models usage
    service --> domainModels
    handler --> domainModels

    %% Click Events
    click main "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/cmd/api/main.go"
    click router "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/cmd/api/routes/routes.go"
    click handler "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/cmd/api/handler/transacao.go"
    click domainModels "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/internal/domain/cliente.go"
    click domainModels "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/internal/domain/transacao.go"
    click domainModels "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/internal/domain/extrato.go"
    click service "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/internal/transacao/service.go"
    click repository "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/internal/transacao/repository.go"
    click webModels "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/pkg/web/response.go"
    click composeProd "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/docker-compose.yml"
    click composeLocal "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/docker-compose-local.yml"
    click dockerfile "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/tree/master/Dockerfile"
    click nginxConf "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/nginx.conf"
    click initSql "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/init.sql"
    click runSh "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/run.sh"
    click ci "https://github.com/isadoramsouza/rinha-backend-go-2024-q1/blob/master/.github/workflows/ci.yml"

    %% Styles
    classDef client fill:#a8e6cf,stroke:#333,stroke-width:1px
    classDef proxy fill:#d0e1f9,stroke:#333,stroke-width:1px
    classDef api fill:#fff4b1,stroke:#333,stroke-width:1px
    classDef service fill:#ffd8b1,stroke:#333,stroke-width:1px
    classDef domain fill:#ffefd5,stroke:#333,stroke-width:1px
    classDef db fill:#f9d5e5,stroke:#333,stroke-width:1px,shape:cylinder
    classDef config fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef ci fill:#e2bef7,stroke:#333,stroke-width:1px
```
