# kelsonthony/algafood-api architecture

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

- Interactive diagram: https://gitdiagram.com/kelsonthony/algafood-api
- Repository: https://github.com/kelsonthony/algafood-api

## Overview

##### Project type and purpose
- AlgaFood API is a Java Spring Boot microservice exposing a REST API for food ordering.
- It follows Domain-Driven Design, uses OAuth2 for security, persists to MySQL, caches in Redis, migrates schema with Flyway, sends email notifications and product‐photo storage, and is deployed via Docker/ECS on AWS.

##### File structure for logical layers
- src/main/java/com/kelsonthony/algafood/api ⇒ HTTP Controllers & DTO assemblers (API layer).
- .core & .domain ⇒ configuration, security, email, validation, domain model, repositories, services (Business & Domain layer).
- .infrastructure ⇒ JPA repository implementations, email storage & template processors, report/pdf services, storage adapters (Persistence & Integration layer).
- resources/db/migration ⇒ Flyway migrations.
- resources/templates ⇒ email & OAuth login pages.
- Dockerfile, docker-compose.yml, nginx/ ⇒ containerization & proxy.

##### Relationships & interactions
- Client → (HTTPS) → NGINX → Spring Boot API
- API → Auth server (token validation/grant)
- API → MySQL via Spring Data JPA
- API → Redis cache for read-heavy endpoints
- API → S3 (via StorageService) to upload/download images
- Domain events in API → EmailService → SMTP or mock adapter
- Business/report requests → ReportService → Jasper → returns PDF
- API logs → Loggly via Spring Cloud/Logback

##### Architectural patterns & principles
- Layered (Controller → Service → Repository → External)
- Domain-Driven Design (Entities, Repositories, Services, Events)
- Hexagonal-ish ports/adapters in infrastructure
- DTO/Assembler pattern for API-domain mapping
- OAuth2 Authorization & Resource Server in same Spring Boot app

##### Final checklist
- Labels: technology + role (e.g., “Redis Cache”, “Flyway Migration”).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    Clients["Web/REST Clients"]:::external
    click Clients "https://github.com/kelsonthony/algafood-api/tree/main/src/main/resources/templates/pages"

    %% Docker Compose Environment
    subgraph "Docker Compose" 
        direction TB
        NGINX["NGINX Proxy"]:::infra
        click NGINX "https://github.com/kelsonthony/algafood-api/blob/main/nginx/nginx.conf"
        
        subgraph "API Service (Spring Boot)" 
            direction TB
            APIRoot["AlgaFood API"]:::service
            click APIRoot "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/AlgafoodApiApplication.java"
            
            subgraph "API Layer" 
                direction TB
                Controllers["REST Controllers & DTO Assemblers"]:::service
                click Controllers "https://github.com/kelsonthony/algafood-api/tree/main/src/main/java/com/kelsonthony/algafood/api"
            end

            subgraph "Security (OAuth2)" 
                direction TB
                ResourceServer["ResourceServerConfig"]:::security
                click ResourceServer "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/core/security/ResourceServerConfig.java"
                AuthServer["AuthorizationServerConfig"]:::security
                click AuthServer "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/core/security/authorizationserver/AuthorizationServerConfig.java"
                JWKController["JWKSetController"]:::security
                click JWKController "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/core/security/authorizationserver/JWKSetController.java"
            end

            subgraph "Domain Services & Events" 
                direction TB
                DomainServices["Domain Services"]:::domain
                click DomainServices "https://github.com/kelsonthony/algafood-api/tree/main/src/main/java/com/kelsonthony/algafood/domain/service"
                DomainEvents["Domain Events"]:::domain
                click DomainEvents "https://github.com/kelsonthony/algafood-api/tree/main/src/main/java/com/kelsonthony/algafood/domain/event"
                EventListeners["Event Listeners"]:::domain
                click EventListeners "https://github.com/kelsonthony/algafood-api/tree/main/src/main/java/com/kelsonthony/algafood/domain/listener"
            end

            subgraph "Persistence & Cache" 
                direction TB
                JPARepos["JPA Repositories"]:::db
                click JPARepos "https://github.com/kelsonthony/algafood-api/tree/main/src/main/java/com/kelsonthony/algafood/domain/repository"
                InfraRepos["Infrastructure Repo Impl"]:::infra
                click InfraRepos "https://github.com/kelsonthony/algafood-api/tree/main/src/main/java/com/kelsonthony/algafood/infrastructure/repository"
                RedisCache["Redis Cache"]:::cache
                click RedisCache "https://github.com/kelsonthony/algafood-api/blob/main/src/main/resources/application.properties"
            end

            subgraph "Integration Adapters" 
                direction TB
                EmailConfig["EmailConfig"]:::config
                click EmailConfig "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/core/email/EmailConfig.java"
                EmailAdapter["SMTP/Sandbox Email Adapter"]:::infra
                click EmailAdapter "https://github.com/kelsonthony/algafood-api/tree/main/src/main/java/com/kelsonthony/algafood/infrastructure/service/email"
                StorageConfig["StorageConfig"]:::config
                click StorageConfig "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/core/storage/StorageConfig.java"
                LocalStorage["LocalFotoStorageService"]:::infra
                click LocalStorage "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/infrastructure/service/storage/LocalFotoStorageService.java"
                S3Storage["S3FotoStorageService"]:::infra
                click S3Storage "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/infrastructure/service/storage/S3FotoStorageService.java"
                ReportServiceIface["VendaReportService"]:::domain
                click ReportServiceIface "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/domain/service/VendaReportService.java"
                PdfReport["PdfVendaReportService"]:::infra
                click PdfReport "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/infrastructure/service/report/PdfVendaReportService.java"
                JasperTpl["Jasper Templates"]:::config
                click JasperTpl "https://github.com/kelsonthony/algafood-api/blob/main/src/main/resources/relatorios/vendas-diarias.jasper"
                SwaggerUI["SpringFoxConfig"]:::config
                click SwaggerUI "https://github.com/kelsonthony/algafood-api/blob/main/src/main/java/com/kelsonthony/algafood/core/springfox/SpringFoxConfig.java"
                Flyway["Flyway Migrations"]:::config
                click Flyway "https://github.com/kelsonthony/algafood-api/tree/main/src/main/resources/db/migration"
            end
        end

        MySQL["MySQL Database"]:::db
        Redis["Redis"]:::cache
    end

    %% AWS Cloud Deployment
    subgraph "AWS Cloud" 
        direction TB
        ECS["AWS ECS/ECR"]:::external
        S3["S3 Bucket"]:::external
        SMTP["SMTP/Email Service"]:::external
        Loggly["Loggly Logging"]:::external
    end

    %% Relationships
    Clients -->|"HTTPS"| NGINX
    NGINX -->|"Forward HTTP"| APIRoot
    APIRoot -->|"JDBC/JPA"| MySQL
    APIRoot -->|"Redis Protocol"| Redis
    APIRoot -->|"S3 API"| S3
    APIRoot -->|"Publish Events"| DomainEvents
    DomainEvents -->|"Trigger"| EmailAdapter
    EmailAdapter -->|"SMTP"| SMTP
    APIRoot -->|"Report Request"| PdfReport
    PdfReport -->|"Loads Template"| JasperTpl
    APIRoot -->|"Logs via Logback"| Loggly
    APIRoot -->|"Deployed to"| ECS

    %% Styles
    classDef external fill:#d9e6f2,stroke:#2d648d,color:#2d648d
    classDef service fill:#e2f0d9,stroke:#6b8e23,color:#6b8e23
    classDef security fill:#fde2e1,stroke:#d9534f,color:#d9534f
    classDef domain fill:#fff2cc,stroke:#e69138,color:#e69138
    classDef infra fill:#e7e6ff,stroke:#6f42c1,color:#6f42c1
    classDef db fill:#ffe6cc,stroke:#d2691e,color:#d2691e
    classDef cache fill:#d0ece7,stroke:#17a2b8,color:#17a2b8
    classDef config fill:#f0f0f0,stroke:#999999,color:#333333
```
