# goodfoodcesi/auth-api architecture

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

- Interactive diagram: https://gitdiagram.com/goodfoodcesi/auth-api
- Repository: https://github.com/goodfoodcesi/auth-api

## Overview

##### Main Components

a. External Layer (Client Interface):
- HTTP API Gateway/Load Balancer (Traefik, indicated by docker-compose-traefik.yaml)
- Rate Limiter (from middleware)

b. Application Core:
- HTTP Interface Layer
- Auth Handler
- User Handler
- Middleware (Authentication, Logging, Rate Limiting)

c. Domain Layer:
- Services
- Auth Service
- User Service
- Messaging Service

d. Infrastructure Layer:
- Database (SQL database, indicated by migrations)
- RabbitMQ Message Broker
- JWT Token Service
- Logger (Zap)

e. Background Workers:
- Message Consumer Service

##### Component Relationships

- External clients → Traefik Gateway
- Gateway → HTTP Interface Layer
- HTTP Handlers → Domain Services
- Domain Services → Infrastructure Layer
- Message Broker → Consumer Service
- All components → Logger

##### Specific Implementation Details

- Blue rectangles: External interfaces (API Gateway, Load Balancer)
- Green rectangles: Application services (Handlers, Domain Services)
- Yellow diamonds: Infrastructure components (Database, Message Queue)
- Red rectangles: Security components (JWT, Auth Middleware)
- Purple rectangles: Background processes (Consumers)

##### Data Flow Indicators

Following flows:
- Authentication flow: Client → Gateway → Auth Handler → Auth Service → JWT Service
- User operations flow: Client → Gateway → User Handler → User Service → Database
- Messaging flow: Services → RabbitMQ → Consumer

##### Deployment Context

A separate section showing:
- Kubernetes deployment structure (from kube directory)
- Development vs Production environments
- Container orchestration

##### Technical Stack Callouts

Following technologies:
- Go (main implementation language)
- PostgreSQL (indicated by SQL migrations)
- RabbitMQ (message broker)
- Traefik (reverse proxy)
- Kubernetes (container orchestration)
- Docker (containerization)

##### Additional Elements

Annotations for:
- Security measures (rate limiting, JWT authentication)
- Database migrations
- API versioning (if applicable)
- Monitoring and logging points

- External interfaces at the top
- Application logic in the middle
- Infrastructure components at the bottom

##### Related components
- Keep all authentication-related components together
- Database operations
- Cluster messaging components

##### Environment boundaries
- Development environment
- Production environment
- Testing environment

This structure reflects the clean architecture pattern evident in the project's organization, with clear separation between interfaces, domain logic, and infrastructure concerns, while also showing the modern cloud-native aspects of the system including message queuing, containerization, and orchestration.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and Classes
    classDef external fill:#2196F3,stroke:#1565C0,color:white
    classDef handler fill:#4CAF50,stroke:#388E3B,color:white
    classDef service fill:#81C784,stroke:#388E3B,color:white
    classDef infrastructure fill:#FFD54F,stroke:#FFA000,color:white
    classDef security fill:#FF5252,stroke:#D32F2F,color:white
    classDef messaging fill:#CE93D8,stroke:#7B1FA2,color:white
    classDef deployment fill:#90A4AE,stroke:#546E7A,color:white

    %% External Layer
    Client[Client Applications]:::external
    LoadBalancer["Load Balancer (Traefik)"]:::external
    click LoadBalancer "https://github.com/goodfoodcesi/auth-api/blob/develop/docker-compose-traefik.yaml"

    %% HTTP Interface Layer
    subgraph InterfaceLayer["HTTP Interface Layer"]
        Router["API Router"]:::handler
        RateLimiter["Rate Limiter"]:::security
        AuthMiddleware["Auth Middleware"]:::security
        AuthHandler["Auth Handler"]:::handler
        UserHandler["User Handler"]:::handler
        click Router "https://github.com/goodfoodcesi/auth-api/blob/develop/interfaces/http/router/router.go"
        click RateLimiter "https://github.com/goodfoodcesi/auth-api/blob/develop/interfaces/http/middleware/ratelimiter.go"
        click AuthMiddleware "https://github.com/goodfoodcesi/auth-api/blob/develop/interfaces/http/handler/auth.go"
        click AuthHandler "https://github.com/goodfoodcesi/auth-api/blob/develop/interfaces/http/handler/auth.go"
        click UserHandler "https://github.com/goodfoodcesi/auth-api/blob/develop/interfaces/http/handler/user.go"
    end

    %% Domain Layer
    subgraph DomainLayer["Domain Services"]
        AuthService["Auth Service"]:::service
        UserService["User Service"]:::service
        MessagingService["Messaging Service"]:::service
        click AuthService "https://github.com/goodfoodcesi/auth-api/blob/develop/domain/service/auth.go"
        click UserService "https://github.com/goodfoodcesi/auth-api/blob/develop/domain/service/user.go"
        click MessagingService "https://github.com/goodfoodcesi/auth-api/blob/develop/domain/service/messaging.go"
    end

    %% Infrastructure Layer
    subgraph InfrastructureLayer["Infrastructure Layer"]
        Database[(Database)]:::infrastructure
        RabbitMQ[("RabbitMQ")]:::messaging
        JWTService["JWT Service"]:::security
        Logger["Logger (Zap)"]:::infrastructure
        MessageConsumer["Message Consumer"]:::messaging
        DBMigrations["Database Migrations"]:::infrastructure
        click Database "https://github.com/goodfoodcesi/auth-api/blob/develop/infrastructure/database/db.go"
        click RabbitMQ "https://github.com/goodfoodcesi/auth-api/blob/develop/infrastructure/messaging/rabbitmq/rabbitmq.go"
        click JWTService "https://github.com/goodfoodcesi/auth-api/blob/develop/infrastructure/jwt/token.go"
        click Logger "https://github.com/goodfoodcesi/auth-api/blob/develop/infrastructure/logger/zap.go"
        click MessageConsumer "https://github.com/goodfoodcesi/auth-api/blob/develop/infrastructure/messaging/consumer/user_consumer.go"
        click DBMigrations "https://github.com/goodfoodcesi/auth-api/tree/develop/infrastructure/database/migration/"
    end

    %% Deployment Layer
    subgraph DeploymentLayer["Deployment Configuration"]
        KubeDev["Kubernetes Dev"]:::deployment
        KubeProd["Kubernetes Prod"]:::deployment
        click KubeDev "https://github.com/goodfoodcesi/auth-api/tree/develop/kube/dev/"
        click KubeProd "https://github.com/goodfoodcesi/auth-api/tree/develop/kube/prod/"
    end

    %% Connections
    Client --> LoadBalancer
    LoadBalancer --> Router
    Router --> RateLimiter
    Router --> AuthMiddleware
    RateLimiter --> |Rate Check| AuthHandler
    RateLimiter --> |Rate Check| UserHandler
    AuthMiddleware --> |Auth Check| AuthHandler
    AuthMiddleware --> |Auth Check| UserHandler
    AuthHandler --> AuthService
    UserHandler --> UserService
    AuthService --> JWTService
    AuthService --> Database
    UserService --> Database
    UserService --> MessagingService
    MessagingService --> RabbitMQ
    RabbitMQ --> MessageConsumer
    Database --> DBMigrations

    %% Logging Connections
    AuthHandler -.-> Logger
    UserHandler -.-> Logger
    AuthService -.-> Logger
    UserService -.-> Logger
    MessageConsumer -.-> Logger

    %% Legend
    subgraph Legend
        ExtComp["External Component"]:::external
        HandlerComp["Handler Component"]:::handler
        ServiceComp["Service Component"]:::service
        InfraComp["Infrastructure Component"]:::infrastructure
        SecComp["Security Component"]:::security
        MsgComp["Messaging Component"]:::messaging
        DepComp["Deployment Component"]:::deployment
    end
```
