How psinalberth/algafood-api works
Project Type and Purpose
• This is a monolithic Spring Boot REST API (“algafood-api”) that models a food-delivery platform (inspired by iFood/Uber Eats).
• It exposes JSON endpoints for managing entities like Restaurante, Cozinha (kitchen), Cidade, Pedido (order), Usuário, Permissões, estatísticas de vendas, etc.
High-Level Layered Architecture
The code is organized into four macro-layers inside one JVM process:
• Presentation/API layer
• Domain layer
• Application/Service layer
• Infrastructure/Persistence layer
Plus cross-cutting modules (security, email, storage, documentation).
Main Components
A. Clients & Entry Points
– External consumers (Web/Mobile apps, Postman, Swagger UI)
– Swagger/OpenAPI UI
B. API Layer
– Controllers (packages under api/controller & api/openapi/controller)
– Exception handlers (api/exceptionhandler)
– HATEOAS support (api/AlgaLinks)
C. Application Services
– Spring-managed services under domain/service (e.g., CadastroPedidoService, RestauranteService)
– Event publishers (domain events & listeners under domain/event + domain/listener)
D. Domain Model
– Entities (domain/model)
– Business rules & validators (core/validation)
– DTOs for reports (domain/model/dto)
E. Persistence
– Repositories (Spring Data JPA interfaces under domain/repository)
– Custom implementations (infrastructure/repository)
– Flyway migrations (src/main/resources/db/migration)
– ORM metadata (META-INF/orm.xml)
– MySQL database
F. Infrastructure Services
– Email (SMTP/Sandbox/Fake implementations under infrastructure/service/email + config in core/email)
– File storage (local & S3 under infrastructure/service/storage + config in core/storage)
– Reporting (Jasper under infrastructure/service/report)
G. Security & OAuth2
– Resource server (core/security/ResourceServerConfig, CorsConfig)
– Authorization server (core/security/authorizationserver), JWT signing keystore
H. Cross-cutting & Tooling
– MapStruct mappers (api/model/mapper)
– Jackson customization (core/jackson)
– Swagger/SpringFox integration (core/springfox)
– Application config (YAML, logging, message bundles)
Relationships and Data Flow
• Clients → Controllers → Services → Domain → Repositories → Database
• Controllers use Mappers to convert between domain objects and API DTOs
• Services publish domain events → Event listeners trigger email notifications or other side-effects
• Security filters intercept requests → validate JWTs via ResourceServerConfig → load users via JPA
• Storage calls go from Service → Local file system or AWS S3
• Reporting service queries data, hands off to Jasper, returns PDF to the client
• Flyway migrates schema before startup & loads test data in CI
Architectural Patterns and Principles
• Layered (hexagonal-inspired) architecture
• Dependency inversion: Controllers depend on Service interfaces; Services depend on Repository interfaces; infrastructure binds implementations
• Event-driven for decoupling side-effects (email notifications)
• DTO/mapper pattern for decoupling API model from domain
• Externalize configuration (YAML, properties) and secret material (keystore, Flyway)