# hoangtien2k3/web-app-backend-springboot architecture

> Architecture diagram of the GitHub repository hoangtien2k3/web-app-backend-springboot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hoangtien2k3/web-app-backend-springboot
- Repository: https://github.com/hoangtien2k3/web-app-backend-springboot

## Overview

##### Project type and purpose
- This is a monolithic Java Spring Boot backend for an e-commerce (“shopapp”) system. It exposes RESTful APIs for Web or Mobile clients. It uses layered architecture, integrates with MySQL for persistence, Redis for caching, Kafka for messaging, and Flyway for DB migrations. It’s containerized via Docker and orchestrated with docker-compose.

##### Main components and boundaries
- Clients: Web UI, Mobile App, API testing tools (Postman/Swagger UI).
- Spring Boot Application (single deployable JAR):
- Controllers (BrandController, ProductController, UserController, etc.) – handle HTTP/REST requests.
- Services (UserService, OrderService, CouponService, etc.) – contain business logic.
- Repositories (JPA/Hibernate-based) – data-access layer to MySQL.
- Components & Filters (CorsFilter, JwtTokenFilter) – security, CORS, JWT management.
- Aspects (PerformanceAspect, UserActivityLogger) – cross-cutting concerns via AOP.
- Configurations (SecurityConfig, RedisConfig, FlywayConfig, OpenApiConfig, etc.) – bootstrap beans, externalized settings.
- Utils, DTOs, Mappers – support classes for mapping, validation, message translation.
- Datastores & Infrastructure services (all inside Docker Compose):
- MySQL8 container – persistent relational DB + Flyway migrations at startup.
- Redis container – caching layer.
- Kafka + Zookeeper containers – async messaging bus for inter-component events (e.g., order events).
- Other: file uploads directory, SQL scripts, Dockerfile, CI/CD pipeline (CircleCI), code quality (SonarCloud).

##### Relationships & data flow
- Clients → (HTTP/HTTPS) → Spring Boot Controllers
- Controllers → Services → Repositories → (JDBC/Hibernate) → MySQL
- Services ↔ Redis (cache read/write via RedisTemplate)
- Services ↔ Kafka (publish/subscribe events)
- On startup: FlywayConfig runs DB migrations against MySQL
- Security filters (CorsFilter, JwtTokenFilter) sit on the inbound HTTP path
- Aspects wrap service calls for logging and performance metrics

##### Architectural patterns & design principles
- Layered architecture (Presentation → Business → Data).
- AOP for logging, performance, exception handling.
- JWT + Spring Security filter chain for stateless auth.
- Externalized configuration for profiles (dev, prod) and secrets (.env files).
- Containerization: each infra component runs in its own Docker container.
- DB migration versioning with Flyway.
- Redis for caching hot data and improving read performance.
- Kafka for decoupled, event-driven communication.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    subgraph "Clients" 
        direction TB
        WebUI["Web UI"]:::client
        MobileApp["Mobile App"]:::client
        Swagger["Swagger/Postman"]:::client
    end

    %% Docker Compose Boundary
    subgraph "Docker Compose" 
        style DockerCompose stroke-dasharray: 5 5
        direction TB

        %% Spring Boot Application Container
        subgraph "shopapp-backend (Spring Boot)" 
            style Shopapp fill:#eef,stroke:#339,stroke-width:2px
            direction TB

            %% Filters
            CorsFilter["CorsFilter"]:::app
            JwtTokenFilter["JwtTokenFilter"]:::app

            %% Controllers Layer
            Controllers["Controllers Layer"]:::app

            %% Services & Cross-cutting
            subgraph "Services & AOP & Components"
                direction TB
                ServicesInt["Services Interfaces"]:::app
                ServicesImpl["Services Implementations"]:::app
                PerformanceAspect["PerformanceAspect"]:::app
                UserActivityLogger["UserActivityLogger"]:::app
                JwtTokenUtil["JwtTokenUtil"]:::app
                TranslateMessages["TranslateMessages"]:::app
                CustomHealthCheck["CustomHealthCheck"]:::app
            end

            %% Repositories Layer
            Repositories["Repositories Layer"]:::app
        end

        %% Datastores & Messaging
        MySQL["MySQL (3306)"]:::datastore
        Redis["Redis (6379)"]:::datastore
        subgraph "Kafka Cluster" 
            direction TB
            Kafka["Kafka Broker (9092)"]:::messaging
            Zookeeper["Zookeeper"]:::messaging
        end
    end

    %% Migration Scripts
    Flyway["Flyway Migrations"]:::infra

    %% Connections
    WebUI -->|HTTP/REST| CorsFilter
    MobileApp -->|HTTP/REST| CorsFilter
    Swagger -->|HTTP/REST| CorsFilter

    CorsFilter -->|passes to| JwtTokenFilter
    JwtTokenFilter -->|passes to| Controllers

    Controllers -->|"calls"| ServicesInt
    ServicesInt -->|"implements"| ServicesImpl

    ServicesImpl -->|"uses"| Repositories
    ServicesImpl -->|"reads/writes"| Redis
    ServicesImpl -->|"publishes/subscribes"| Kafka

    Repositories -->|"JDBC/3306"| MySQL
    Flyway -->|"migrates"| MySQL

    Redis <-->|"Redis protocol"| ServicesImpl
    Kafka <-->|"Kafka"| ServicesImpl

    %% Click Events
    click CorsFilter "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/components/CorsFilter.java"
    click JwtTokenFilter "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/filters/JwtTokenFilter.java"
    click Controllers "https://github.com/hoangtien2k3/web-app-backend-springboot/tree/main/src/main/java/com/hoangtien2k3/shopappbackend/controllers"
    click ServicesInt "https://github.com/hoangtien2k3/web-app-backend-springboot/tree/main/src/main/java/com/hoangtien2k3/shopappbackend/services"
    click ServicesImpl "https://github.com/hoangtien2k3/web-app-backend-springboot/tree/main/src/main/java/com/hoangtien2k3/shopappbackend/services/impl"
    click Repositories "https://github.com/hoangtien2k3/web-app-backend-springboot/tree/main/src/main/java/com/hoangtien2k3/shopappbackend/repositories"
    click JwtTokenUtil "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/components/JwtTokenUtil.java"
    click TranslateMessages "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/components/TranslateMessages.java"
    click CustomHealthCheck "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/components/CustomHealthCheck.java"
    click PerformanceAspect "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/components/aspect/PerformanceAspect.java"
    click UserActivityLogger "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/components/aspect/UserActivityLogger.java"
    click Flyway "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/src/main/java/com/hoangtien2k3/shopappbackend/configurations/FlywayConfig.java"
    click Redis "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/docker-compose.yml"
    click MySQL "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/docker-compose.yml"
    click Kafka "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/docker-compose.yml"
    click Zookeeper "https://github.com/hoangtien2k3/web-app-backend-springboot/blob/main/docker-compose.yml"

    %% Styles
    classDef app fill:#bbdefb,stroke:#1e88e5,stroke-width:1px
    classDef datastore fill:#ffe0b2,stroke:#fb8c00,stroke-width:1px
    classDef messaging fill:#c8e6c9,stroke:#43a047,stroke-width:1px
    classDef client fill:#f5f5f5,stroke:#9e9e9e,stroke-width:1px
    classDef infra fill:#eeeeee,stroke:#757575,stroke-width:1px
    class DockerCompose,DockerCompose fill:none,stroke:#757575
    class Shopapp fill:#e3f2fd,stroke:#0d47a1,stroke-width:2px
```
