# studyprojectsthb/guessingaverage architecture

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

- Interactive diagram: https://gitdiagram.com/studyprojectsthb/guessingaverage
- Repository: https://github.com/studyprojectsthb/guessingaverage

## Overview

##### Main Components
- Application Layer:
- Web Frontend: Represent the HTML templates and static resources (CSS, images) that render the game’s interface.
- Controller: Highlight the NumberController that handles GET (displaying the game page) and POST (processing user guesses). Also note that this controller uses GuessingAverageProperties (configuration) and interacts with the core logic.
- Application Runner: Show MyApplicationRunner, which pre-populates data when the application starts.

- Cloud and Deployment Infrastructure:
- Cloud Infrastructure Components (defined in the terraform directory):
▪ Application Load Balancer (ALB/ELB): Receives HTTP requests from end users.
▪ EC2 Instances: Represent the servers running the Dockerized Spring Boot app.
▪ Auto Scaling Group (ASG) & Launch Templates: Manage scaling/updating of instances.
▪ Database (secured by Security Groups): Although not detailed in the file tree, the README mentions a DB secured with specific rules.
▪ Security Groups and Routing: Illustrate the SG for ELB, EC2, and DB as described.
- Continuous Deployment Pipeline:
▪ GitHub Actions: Illustrate the workflow starting with unit/integration tests (using Maven) and building the Docker image.
▪ Webhook and AWS API Gateway: Show that GitHub pushes trigger a webhook.
▪ Lambda Functions & SQS: Detail the sequence where Lambda functions update the EC2 instance, create an AMI, refresh the ASG, and handle retries via SQS in case of concurrent deployments.

##### Relationships and Data Flow
- User HTTP requests (labeled in green) originating from a standard user’s browser to the ALB and then being routed to EC2 instances.
- Admin SSH access (labeled in red) directly to EC2 instances from the THB network.
- Outgoing internet access from EC2 instances (labeled in orange) when they connect externally.
- In the application layer, connect the components as follows:
- The Controller interacts with the Service Layer.
- The Service Layer communicates with the Data Access Layer to fetch/store NumberEntries.
- The Application Runner initializes data using the same service.
- Configuration properties from application.properties and the GuessingAverageProperties are used across multiple components.

##### Architectural Patterns and Technologies
- MVC Pattern: Clearly separate the responsibilities of rendering the interface (views/templates), handling user requests (controllers), and processing business logic (services).
- CI/CD Pipeline: Illustrate the role of GitHub Actions, Docker image creation, webhook to AWS API Gateway, subsequent Lambda functions, and the AMI/ASG update process.

## Diagram (Mermaid)

```mermaid
graph TD
    %% User & External Entities
    User["User Browser"]:::user
    Admin["THB Network Admin (SSH)"]:::user
    Internet["Internet"]:::user

    %% Application Layer
    subgraph "Application Layer"
        A1["Web Frontend: HTML Templates"]:::web
        A2["Web Frontend: Static Assets"]:::web
        C1["NumberController"]:::controller
        FM["Form Model"]:::controller
        CP["GuessingAverageProperties"]:::controller
        S1["NumberEntryService"]:::service
        S2["NumberEntryCalculationService"]:::service
        R1["NumberEntryRepository"]:::repository
        AR["MyApplicationRunner"]:::runner
    end

    %% Cloud Infrastructure
    subgraph "Cloud Infrastructure"
        ALB["Application Load Balancer (ALB/ELB)"]:::infra
        EC2["EC2 Instances (Dockerized App)"]:::infra
        ASG["Auto Scaling Group"]:::infra
        DB["Database"]:::infra
        SG["Security Groups & Routing"]:::infra
    end

    %% Continuous Deployment Pipeline
    subgraph "Continuous Deployment Pipeline"
        GA["GitHub Actions Workflow"]:::pipeline
        DC["Docker Configuration"]:::pipeline
        WH["Webhook & API Gateway"]:::pipeline
        LF["Lambda Functions"]:::pipeline
        SQS["SQS (Retries)"]:::pipeline
        AMI["AMI/ASG Refresh"]:::pipeline
    end

    %% Relationships & Data Flow
    User -->|"HTTP"| ALB
    ALB -->|"Routes Traffic"| EC2
    Admin -->|"SSH"| EC2
    EC2 -->|"Outgoing"| Internet

    %% EC2 hosts Application Layer components
    EC2 -->|"Hosts"| A1
    EC2 -->|"Hosts"| C1

    %% Internal Application Flow (MVC)
    A1 -->|"Loads"| C1
    C1 -->|"Uses"| FM
    C1 -->|"Uses"| CP
    C1 -->|"Calls"| S1
    C1 -->|"Calls"| S2
    S1 -->|"Accesses"| R1
    S2 -->|"Accesses"| R1
    AR -->|"InitializesDataVia"| S1

    %% Continuous Deployment Flow
    GA -->|"RunsTests&Build"| DC
    DC -->|"Triggers"| WH
    WH -->|"Invokes"| LF
    LF -->|"SendsMsgTo"| SQS
    SQS -->|"Updates"| AMI
    AMI -->|"Refreshes"| ASG

    %% Click Events for Component Mapping
    click A1 "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/resources/templates/guessing-average.html"
    click A2 "https://github.com/studyprojectsthb/guessingaverage/tree/master/src/main/resources/static"
    click C1 "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/java/de/thb/guessingaverage/controller/NumberController.java"
    click FM "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/java/de/thb/guessingaverage/controller/form/NumberEntryFormModel.java"
    click CP "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/java/de/thb/guessingaverage/configuration/GuessingAverageProperties.java"
    click S1 "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/java/de/thb/guessingaverage/services/NumberEntryService.java"
    click S2 "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/java/de/thb/guessingaverage/services/NumberEntryCalculationService.java"
    click R1 "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/java/de/thb/guessingaverage/repositories/NumberEntryRepository.java"
    click AR "https://github.com/studyprojectsthb/guessingaverage/blob/master/src/main/java/de/thb/guessingaverage/MyApplicationRunner.java"
    click ALB "https://github.com/studyprojectsthb/guessingaverage/blob/master/terraform/load_balancer.tf"
    click EC2 "https://github.com/studyprojectsthb/guessingaverage/blob/master/terraform/ec2.tf"
    click SG "https://github.com/studyprojectsthb/guessingaverage/blob/master/terraform/security_group.tf"
    click ASG "https://github.com/studyprojectsthb/guessingaverage/blob/master/terraform/update_asg.tf"
    click GA "https://github.com/studyprojectsthb/guessingaverage/blob/master/.github/workflows/maven.yml"
    click DC "https://github.com/studyprojectsthb/guessingaverage/tree/master/Dockerfile"

    %% Style Classes
    classDef web fill:#fdebd0,stroke:#d35400,stroke-width:2px;
    classDef controller fill:#d6eaf8,stroke:#3498db,stroke-width:2px;
    classDef service fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef repository fill:#e8daef,stroke:#8e44ad,stroke-width:2px;
    classDef runner fill:#f9e79f,stroke:#f1c40f,stroke-width:2px;
    classDef infra fill:#fcf3cf,stroke:#f39c12,stroke-width:2px;
    classDef pipeline fill:#fbeee6,stroke:#e67e22,stroke-width:2px;
    classDef user fill:#d3d3d3,stroke:#696969,stroke-width:2px;
```
