# krish3101/traffic-app architecture

> Architecture diagram of the GitHub repository krish3101/traffic-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/krish3101/traffic-app
- Repository: https://github.com/krish3101/traffic-app

## Overview

#### Purpose and Runtime
Smart Traffic System is a Java 21/Spring Boot 3 application that evaluates vehicle speed events, exempts emergency vehicles, persists qualifying violations, and assigns configurable fines. It exposes both a server-rendered Thymeleaf interface and JSON REST API. Spring Boot starts from `src/main/java/org/krish/traffic/TrafficApplication.java`; `src/main/java/org/krish/traffic/SmartTrafficSystem.java` contains the core traffic-processing logic.

#### Entry Interfaces
- MVC UI: `src/main/java/org/krish/traffic/TrafficController.java` serves the index and processes form submissions. It renders `src/main/resources/templates/index.html`, with browser assets in `src/main/resources/static/js/main.js` and `src/main/resources/static/css/style.css`.
- REST API: `src/main/java/org/krish/traffic/ViolationRestController.java` provides `/api/v1/violations` endpoints for submitting events and retrieving saved violations.
- Both interfaces accept the same validated request/form contract, `src/main/java/org/krish/traffic/ViolationForm.java`, keeping event rules consistent across browser and programmatic clients.

#### Processing and Rule Boundary
The central flow is: receive event → validate fields → determine whether it exceeds the configured speed threshold and is non-emergency → calculate a tiered fine → persist and return/display the violation. `src/main/java/org/krish/traffic/SmartTrafficSystem.java` owns detection and fine calculation, separating business decisions from HTTP controllers.
Rules are externalized through `src/main/java/org/krish/traffic/TrafficRulesProperties.java`, bound from `src/main/resources/application.yml` under `traffic.rules`. This makes the speed threshold and fine tiers deployment configuration rather than hard-coded controller behavior.

#### Validation and Outcomes
`src/main/java/org/krish/traffic/ViolationForm.java` uses Jakarta Bean Validation for vehicle ID, speed, and zone constraints. MVC binding errors return the UI view; REST binding errors are transformed into HTTP 400 error responses by `src/main/java/org/krish/traffic/ViolationRestController.java`.
A valid event that is not speeding, or is emergency, stops at the decision boundary with a no-violation result. Only detected violations proceed to persistence.

#### Persistence Model
`src/main/java/org/krish/traffic/TrafficViolation.java` is the JPA entity representing stored violation records, including vehicle/event data, computed fine, and creation time. `src/main/java/org/krish/traffic/TrafficViolationRepository.java` is the Spring Data JPA boundary used to save and query records. The configured datastore is H2 in-memory with PostgreSQL-compatible behavior, so persisted data is runtime-local unless configuration is changed.

#### Deployment and Infrastructure
The application is Maven-built (`pom.xml`) and containerized via `Dockerfile`. `docker-compose.yml` supports local container execution, while `render.yaml` indicates deployment configuration for Render. Runtime configuration is centralized in `src/main/resources/application.yml`; `.env.example` documents environment-variable inputs for deployment.

Main entry points: [`src/main/java/org/krish/traffic/SmartTrafficSystem.java`](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/SmartTrafficSystem.java) (Traffic processor), [`src/main/java/org/krish/traffic/TrafficController.java`](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficController.java) (MVC controller), [`src/main/java/org/krish/traffic/ViolationRestController.java`](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/ViolationRestController.java) (Violations API), [`src/main/java/org/krish/traffic/TrafficViolationRepository.java`](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficViolationRepository.java) (Violation repository)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Entry Interfaces"]
  node_mvc["MVC controller<br/>Spring MVC"]
  node_web["Browser UI<br/>Thymeleaf and assets<br/>[index.html]"]
  node_rest["Violations API<br/>REST controller"]
end

subgraph group_processing["Processing &amp; Rules"]
  node_app{{"Spring Boot app<br/>application entry"}}
  node_form["Violation form<br/>validated request contract<br/>[ViolationForm.java]"]
  node_traffic["Traffic processor<br/>business service"]
  node_rules["Traffic rules<br/>configuration properties"]
  node_config["Runtime configuration<br/>YAML configuration<br/>[application.yml]"]
end

subgraph group_persistence["Persistence"]
  node_violation["Traffic violation<br/>JPA entity"]
  node_repository["Violation repository<br/>Spring Data JPA"]
  node_h2[("H2 datastore<br/>in-memory database<br/>[application.yml]")]
end

subgraph group_delivery["Build &amp; Deployment"]
  node_maven["Maven build<br/>build definition<br/>[pom.xml]"]
  node_docker["Container image<br/>Docker build"]
  node_compose["Local containers<br/>Docker Compose<br/>[docker-compose.yml]"]
  node_render["Render deployment<br/>deployment manifest<br/>[render.yaml]"]
end

node_app -->|"starts"| node_mvc
node_app -->|"starts"| node_rest
node_mvc -->|"renders"| node_web
node_mvc -->|"binds and validates"| node_form
node_rest -->|"binds and validates"| node_form
node_mvc -->|"submits valid events"| node_traffic
node_rest -->|"submits valid events"| node_traffic
node_traffic -->|"evaluates with"| node_rules
node_config -->|"binds traffic.rules"| node_rules
node_traffic -->|"creates when qualifying"| node_violation
node_traffic -->|"persists and queries"| node_repository
node_repository -->|"maps"| node_violation
node_repository -->|"reads and writes"| node_h2
node_config -->|"configures"| node_h2
node_maven -->|"builds"| node_app
node_docker -->|"packages with"| node_maven
node_compose -->|"runs"| node_docker
node_render -->|"deploys"| node_docker

click node_app "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficApplication.java"
click node_mvc "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficController.java"
click node_web "https://github.com/krish3101/traffic-app/blob/main/src/main/resources/templates/index.html"
click node_rest "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/ViolationRestController.java"
click node_form "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/ViolationForm.java"
click node_traffic "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/SmartTrafficSystem.java"
click node_rules "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficRulesProperties.java"
click node_config "https://github.com/krish3101/traffic-app/blob/main/src/main/resources/application.yml"
click node_violation "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficViolation.java"
click node_repository "https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficViolationRepository.java"
click node_h2 "https://github.com/krish3101/traffic-app/blob/main/src/main/resources/application.yml"
click node_maven "https://github.com/krish3101/traffic-app/blob/main/pom.xml"
click node_docker "https://github.com/krish3101/traffic-app/blob/main/Dockerfile"
click node_compose "https://github.com/krish3101/traffic-app/blob/main/docker-compose.yml"
click node_render "https://github.com/krish3101/traffic-app/blob/main/render.yaml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mvc,node_web,node_rest toneBlue
class node_app,node_form,node_traffic,node_rules,node_config toneAmber
class node_violation,node_repository,node_h2 toneMint
class node_maven,node_docker,node_compose,node_render toneRose
```

## Components

### Entry Interfaces

- [MVC controller](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficController.java): Spring MVC · Serves the form and browser outcomes. · `src/main/java/org/krish/traffic/TrafficController.java`
- [Browser UI](https://github.com/krish3101/traffic-app/blob/main/src/main/resources/templates/index.html): Thymeleaf and assets · `src/main/resources/templates/index.html`
- [Violations API](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/ViolationRestController.java): REST controller · Exposes /api/v1/violations. · `src/main/java/org/krish/traffic/ViolationRestController.java`

### Processing & Rules

- [Spring Boot app](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficApplication.java): application entry · `src/main/java/org/krish/traffic/TrafficApplication.java`
- [Violation form](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/ViolationForm.java): validated request contract · Shared Bean Validation contract for UI and API input. · `src/main/java/org/krish/traffic/ViolationForm.java`
- [Traffic processor](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/SmartTrafficSystem.java): business service · Detects qualifying violations and calculates tiered fines. · `src/main/java/org/krish/traffic/SmartTrafficSystem.java`
- [Traffic rules](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficRulesProperties.java): configuration properties · Provides speed threshold and fine tiers. · `src/main/java/org/krish/traffic/TrafficRulesProperties.java`
- [Runtime configuration](https://github.com/krish3101/traffic-app/blob/main/src/main/resources/application.yml): YAML configuration · `src/main/resources/application.yml`

### Persistence

- [Traffic violation](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficViolation.java): JPA entity · Stored vehicle event, computed fine, and creation time. · `src/main/java/org/krish/traffic/TrafficViolation.java`
- [Violation repository](https://github.com/krish3101/traffic-app/blob/main/src/main/java/org/krish/traffic/TrafficViolationRepository.java): Spring Data JPA · Saves and queries detected violations. · `src/main/java/org/krish/traffic/TrafficViolationRepository.java`
- [H2 datastore](https://github.com/krish3101/traffic-app/blob/main/src/main/resources/application.yml): in-memory database · Runtime-local database configured with PostgreSQL compatibility. · `src/main/resources/application.yml`

### Build & Deployment

- [Maven build](https://github.com/krish3101/traffic-app/blob/main/pom.xml): build definition · `pom.xml`
- [Container image](https://github.com/krish3101/traffic-app/blob/main/Dockerfile): Docker build · `Dockerfile`
- [Local containers](https://github.com/krish3101/traffic-app/blob/main/docker-compose.yml): Docker Compose · `docker-compose.yml`
- [Render deployment](https://github.com/krish3101/traffic-app/blob/main/render.yaml): deployment manifest · `render.yaml`

## Connections

- Spring Boot app → MVC controller: starts
- Spring Boot app → Violations API: starts
- MVC controller → Browser UI: renders
- MVC controller → Violation form: binds and validates
- Violations API → Violation form: binds and validates
- MVC controller → Traffic processor: submits valid events
- Violations API → Traffic processor: submits valid events
- Traffic processor → Traffic rules: evaluates with
- Runtime configuration → Traffic rules: binds traffic.rules
- Traffic processor → Traffic violation: creates when qualifying · Non-speeding and emergency events produce no stored violation.
- Traffic processor → Violation repository: persists and queries
- Violation repository → Traffic violation: maps
- Violation repository → H2 datastore: reads and writes
- Runtime configuration → H2 datastore: configures
- Maven build → Spring Boot app: builds
- Container image → Maven build: packages with
- Local containers → Container image: runs
- Render deployment → Container image: deploys
