Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

upendrakumbham/code-exercise-java

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener Coding Exercise

Task

Build a simple URL shortener in a preferably JVM-based language (e.g. Java, Kotlin).

It should:

  • Accept a full URL and return a shortened URL.
  • A shortened URL should have a randomly generated alias.
  • Allow a user to customise the shortened URL if they want to (e.g. user provides my-custom-alias instead of a random string).
  • Persist the shortened URLs across restarts.
  • Expose a decoupled web frontend built with a modern framework (e.g., React, Next.js, Vue.js, Angular, Flask with templates). This can be lightweight form/output just to demonstrate interaction with the API. Feel free to use UI frameworks like Bootstrap, Material-UI, Tailwind CSS, GOV.UK design system, etc. to speed up development.
  • Expose a RESTful API to perform create/read/delete operations on URLs.
    → Refer to the provided openapi.yaml for API structure and expected behaviour.
  • Include the ability to delete a shortened URL via the API.
  • Have tests.
  • Be containerised (e.g. Docker).
  • Include instructions for running locally.

Rules

  • Fork the repository and work in your fork. Do not push directly to the main repository.
  • There is no time limit, we want to see something you are proud of. We would like to understand roughly how long you spent on it though.
  • Commit often with meaningful messages.
  • Write tests.
  • The API should validate inputs and handle errors gracefully.
  • The Frontend should show errors from the API appropriately.
  • Use the provided openapi.yaml as the API contract.
  • Focus on clean, maintainable code.
  • AI tools (e.g., GitHub Copilot, ChatGPT) are allowed, but please do not copy-paste large chunks of code. Use them as assistants, not as a replacement for your own work. We will be asking.

Deliverables

  • Working software.
  • Decoupled web frontend (using a modern framework like React, Next.js, Vue.js, Angular, or Flask with templates).
  • RESTful API matching the OpenAPI spec.
  • Tests.
  • A git commit history that shows your thought process.
  • Dockerfile.
  • README with:
    • How to build and run locally.
    • Example usage (frontend and API).
    • Any notes or assumptions.


URL Shortener API (Spring Boot)

A RESTful API for shortening URLs, retrieving them, redirecting, and deleting entries.


🚀 Features

  • Create short URLs (with optional custom alias)
  • Redirect to original URL
  • List all shortened URLs
  • Delete URLs
  • H2 (dev/test)
  • Integration & unit tests
  • Docker support

🛠️ Tech Stack

  • Java 17
  • Spring Boot 3.5
  • Spring Web (REST APIs)
  • Spring Data JPA
  • H2 Filebased mode
  • Maven
  • JaCoCo (code coverage)

📦 Prerequisites

  • Java 17+
  • Maven (or use Maven Wrapper ./mvnw)
  • Node.js (if running frontend)
  • Docker (optional)

⚙️ Getting Started

1. Clone the repository

git clone <your-repo-url>
cd url-shortener

2. Build the project

./mvnw clean install

3. Run the application

Set SpringBoot Profile as an environment variable

export SPRING_PROFILES_ACTIVE=dev
./mvnw spring-boot:run

or

./mvnw spring-boot:run -Dspring-boot.run.profiles=dev

4. Access API

http://localhost:8080

🔗 API Endpoints

➕ Shorten URL

POST /shorten

Request:

{
  "fullUrl": "https://example.com",
  "customAlias": "my-alias"
}

Response (201):

{
  "shortUrl": "http://localhost:8080/my-alias"
}

🔁 Redirect to Original URL

GET /{alias}

Response:

  • 302 Found → Redirects to original URL
  • 404 Not Found → Alias does not exist

📋 Get All URLs

GET /urls

Response:

[
  {
    "alias": "my-alias",
    "fullUrl": "https://example.com",
    "shortUrl": "http://localhost:8080/my-alias"
  }
]

❌ Delete URL

DELETE /{alias}

Response:

  • 204 No Content → Successfully deleted
  • 404 Not Found → Alias not found

Access H2 console:

http://localhost:8080/h2-console

⚙️ Profiles

Run with different profiles:

java -jar app.jar --spring.profiles.active=dev

Available profiles:

  • dev
  • test

🧪 Running Tests

Unit tests

./mvnw test

Integration tests

./mvnw verify

📊 Code Coverage (JaCoCo)

Generate report:

./mvnw clean verify

Open report:

target/site/jacoco/index.html

🐳 Docker

Build image

docker build -t url-shortener .

Run the Container by passing spring profile dev

docker run -p 8080:8080 \
  -e SPRING_PROFILES_ACTIVE=dev \
  url-shortener

Note: I'm not pusing docker image to DockerHub. Please build image from the Dockerfile and create a container from the image

⚠️ Common Issues

❌ CORS errors

Fix backend CORS configuration

❌ Port already in use

Change port:

server:
  port: 8081

❌ DB connection issues

Check credentials and DB availability


💡 Future Improvements

  • URL expiration
  • Analytics (click tracking)
  • Reslience 4J
  • Authentication (JWT)
  • API Documentation
  • Redis cache integration
  • DB Migration
  • DB indexing

Run frontend application

  1. Clone the repository
    git clone git@github.com:upendrakumbham/url-shortener-ui.git
    cd url-shortener-ui
  2. Install dependencies npm install
  3. Start the development server npm run dev
  4. Open the application http://localhost:5173 🔗 Backend Configuration

The frontend expects the backend API to be running at: http://localhost:8080

If your backend runs on a different port, update: src/api/urlApi.js const API_BASE = "http://localhost:8080";

🌐 CORS Configuration (IMPORTANT)

Update your Spring Boot backend:

@CrossOrigin(origins = "http://localhost:5173")
Public class UrlController
Or configure globally via WebMvcConfigurer.
Rerun backend API, make sure it is running.

🧪 Available Scripts

npm run dev       # Start dev server
npm run build     # Build production bundle
npm run preview   # Preview production build

🧑‍💻 Author

Upendra Kumbham


📄 License

This project is implemented as part TpxImpact assignment.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.