How krish3101/traffic-app works
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: SmartTrafficSystem.java Traffic processor · TrafficController.java MVC controller · ViolationRestController.java Violations API · TrafficViolationRepository.java Violation repository
Components 15
Entry Interfaces
- MVC controller · Serves the form and browser outcomes.
src/main/java/org/krish/traffic/TrafficController.java - Browser UI
src/main/resources/templates/index.html - Violations API · Exposes /api/v1/violations.
src/main/java/org/krish/traffic/ViolationRestController.java
Processing & Rules
- Spring Boot app
src/main/java/org/krish/traffic/TrafficApplication.java - Violation form · Shared Bean Validation contract for UI and API input.
src/main/java/org/krish/traffic/ViolationForm.java - Traffic processor · Detects qualifying violations and calculates tiered fines.
src/main/java/org/krish/traffic/SmartTrafficSystem.java - Traffic rules · Provides speed threshold and fine tiers.
src/main/java/org/krish/traffic/TrafficRulesProperties.java - Runtime configuration
src/main/resources/application.yml
Persistence
- Traffic violation · Stored vehicle event, computed fine, and creation time.
src/main/java/org/krish/traffic/TrafficViolation.java - Violation repository · Saves and queries detected violations.
src/main/java/org/krish/traffic/TrafficViolationRepository.java - H2 datastore · Runtime-local database configured with PostgreSQL compatibility.
src/main/resources/application.yml
Build & Deployment
- Maven build
pom.xml - Container image
Dockerfile - Local containers
docker-compose.yml - Render deployment
render.yaml
Connections 18
- 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