How ssaisriharsha/url-shortener works
Project type and purpose
• This is a backend Java Spring Boot microservice whose sole purpose is to shorten long URLs and track click analytics.
• It exposes a REST API for generating and resolving short URLs and includes a simple auth mechanism for user-specific operations.
File structure
• Top level: Maven wrapper files, pom.xml, README.
• src/main/java/com/ssaisriharsha/UrlShortener organizes code by layer:
• Config (security)
• Controller (AuthController, URLRestController)
• Service (AuthService, URLService, AppUserDetailsService)
• Repository (AppUserRepo, URLRepo)
• Entities (AppUser, URLEntity)
• DTOs (AppUserDTO, URLEntityDTO)
• Utilities (ShortURLGenerator, DomainProtocolExtractor)
• src/main/resources/application.properties holds DB connection and CORS.
• Tests are under src/test.
README for additional insights
• Uses Spring MVC, Spring Data JPA, PostgreSQL.
• Features: GET /api/{shortURL}, POST /api/shorten
• Tracks clicks, every 5th click redirects to google.com (extensible to ads).
• CORS enabled; standalone JAR deployable.
a. Main components
• Client (e.g., Web UI, Postman, mobile app)
• Spring Boot Application
• Security Layer (Spring Security config, AppUserDetails)
• API Layer (AuthController, URLRestController)
• Business Layer (AuthService, URLService)
• Data Layer (Spring Data JPA Repositories)
• Utilities (ShortURLGenerator, DomainProtocolExtractor)
• PostgreSQL Database
• External Redirect/Ad Service (e.g., google.com or ad server)
b. Relationships & interactions
1. Client → API Layer: HTTP(s) requests
2. Requests pass through Security Layer
3. Controller dispatches to Service
4. Service uses Utilities for URL processing
5. Service ↔ Repository for DB CRUD
6. Redirect flow: URLService checks click count → either direct DB URL or invokes external redirect
7. Responses flow back to client
c. Architectural patterns & design principles
• Layered Architecture (Controller → Service → Repository)
• DTO Pattern for request/response decoupling
• Separation of Concerns (Security config isolated under Config)
• Utility components for cross-cutting concerns (URL generation, protocol handling)
• Spring Data JPA abstracts persistence layer
d. Relevant technologies & frameworks
• Spring Boot (application framework)
• Spring MVC (REST controllers)
• Spring Security (auth & route protection)
• Spring Data JPA (ORM)
• PostgreSQL (relational storage)
• Java’s SecureRandom & java.net.http for short-URL generation & optional HTTP calls
Detail
• Layer explicitly—do not lump all server code into one block.
• Security filter chain before controllers.
• Where DTOs translate to Entities.
• Click-count logic branch to external redirect.