Skip to content

MartinCastroAlvarez/hexagonal-spring-boot

Repository files navigation

hexagonal-spring-boot

Hexagonal Architecture on Java Spring Boot for managing an e-commerce, and React with Tailwind on the Frontend.

wallpaper.jpg

What the demos!

Introduction

  • Read the paper here

Paper.png

This application relies on Java Spring Boot Hexagonal Architecture:

  • Domain Models: Defines the core domain models used throughout the application here.
  • Domain Out Ports: Includes repository interfaces which define the expected behaviors of data access mechanisms here.
  • Domain Exceptions: Custom exceptions that handle specific domain errors here.
  • Domain Service Interfaces: Interfaces for the domain services that encapsulate business logic here.
  • Unit Tests: Contains unit tests that verify the behavior of the application components here.
  • Configuration: Application configurations and properties here.
  • Web API Adapters (Controllers): Adapters that expose the application functionality over HTTP as web APIs here.
  • Database Adapters (JPA Repositories): Database adapters that implement the repository interfaces with JPA here.
  • Adapter Model Entities: Entity models used by JPA to map to the database tables here.
  • Mappers for Model to Entity Mapping and Vice Versa: Utility classes to convert between domain models and JPA entities here.
  • Models Application (Service Implementations): Implementation of the domain services, where the business logic is actually carried out, here.
  • Web application: The React application is implemented here.

Documentation

Installation

  • Install Java
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
java -version
  • Install Maven
brew install maven
mvn -v
  • Install the frontend dependencies
cd web/
pnpm i
  • Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo systemctl enable docker
sudo systemctl start docker

Development

  • Run the application
cd app/
mvn spring-boot:run
cd app/
mvn test
[...]
[INFO] Tests run: X, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.056 s -- in com.martincastroalvarez.hex.hex.RunCommandTests
[INFO] Running com.martincastroalvarez.hex.hex.adapters.web.ProductControllerTest
[INFO] Tests run: X, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.200 s -- in com.martincastroalvarez.hex.hex.adapters.web.ProductControllerTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: X, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.120 s
[INFO] Finished at: 2024-05-06T13:39:45-04:00
[INFO] ------------------------------------------------------------------------
  • Start the React app
cd web/
REACT_APP_API_URL=http://localhost:8080 npm start
  • Try logging in using the API.
RESPONSE=$(curl -s -X POST "http://localhost:8080/auth/login" -H "Content-Type: application/json" -d '{"email": "user@test.com", "password": "test"}')
TOKEN=$(echo $RESPONSE | jq -r '.Token')
echo $RESPONSE | jq
{
  "Token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyQHRlc3QuY29tIn0.vgHfw5gAI9avcrqH_Qt_yrMAroaD_ey0IxouNCkX8DpvBgW2_a_gyFFZ38tqlzchkcjGvoE5uuXc-KhXNgww3Q"
}
  • Try listing existing products.
curl -s -X GET -H "Authorization: Bearer $TOKEN" "http://localhost:8080/products?page=0&size=3&sort=id&asc=true" | jq
[
  {
    "id": 19,
    "name": "Product 1",
    "isActive": true,
    "price": 10,
  },
  {
    "id": 20,
    "name": "Product 2",
    "isActive": true,
    "price": 50,
  },
  {
    "id": 21,
    "name": "Product 3",
    "isActive": true,
    "price": 50,
  }
]

Deployment

  • Build the Java Spring Boot application
cd app/
mvn clean package
  • Build the React application
cd web/ 
REACT_APP_API_URL=http://54.189.152.54:8080 npm run build
  • Upload the React application to AWS S3.
cd web/build/
s3.post s3://com.martincastroalvarez.aec/
  • Deploy the changes to the EC2 instance.
ssh -i "hex.pem" ec2-user@ec2-54-189-152-54.us-west-2.compute.amazonaws.com
  • Start the application using Docker Compose
docker-compose up --build -d
  • Visit the login page

login.png

  • Visit the signup page

signup.png

  • Visit the users management page

users.png

  • Visit the homepage

homepage.png

  • Visit the schedule management page

schedule.png

  • Visit the meeting management page

meeting.png

  • Visit the product management page

product.png