Skip to content

mithredate/url-shortener

Repository files navigation

URL Shortener Service

Overview

This is a simple URL shortener service built with Kotlin and Micronaut. It allows users to shorten long URLs and retrieve the original URLs using the shortened version.

The service generates a unique identifier for each URL. The ID is generated by DB. The current implementation stores the generated short URL in postgres.

Setup

  1. Clone the repository.
  2. Navigate to the project directory.
  3. Run the application:
    docker compose up -d
    ./gradlew run

NOTE: There is a deprecation warning when running the application with ./gradlew run. Due to having time constraints, I did not have time to fix it.

Endpoints

There are two endpoints available:

  • POST /api/v1/shortened-urls: Shorten a URL
  • GET /api/v1/shortened-urls/{shortUri}: Retrieve the original URL

See the API Docs for more details.

The implementation assumes that the API is being used by a custom frontend application, which will handle the redirection to the original URL.

The response type of the endpoints is JsonApi.

Shorten URL

curl --location 'http://localhost:8080/api/v1/shortened-urls' \
--header 'Content-Type: application/json' \
--data '{"url": "https://google.com/test"}'

Retrieve Original URL

Make sure to replace {shortUri} with the short URI generated by the service.

curl --location 'http://localhost:8080/api/v1/shortened-urls/{shortUri}'

Obsolete URLs

There's a scheduled job to delete URLS that have been created before a specific date, specified in app.prune.time-in-seconds configuration.

Testing

Tests use a postgres test container. Make sure to have docker installed.

  1. Run the tests:
    ./gradlew test

Cleanup

  1. Stop the application:
  2. Remove DB container:
    docker compose down -v

Disclaimer

This is a simple implementation and is not intended for production use.

Improvements

  • Add validation for the input URL.
  • Add error handling.
  • Make sure the generated short URL is URL safe.
  • Enhance short URL generation algorithm to be more efficient and generate more user-friendly URLs.
  • Enhance API docs with swagger-ui.
  • Add logging, metrics, and monitoring.
  • Add security features, including rate-limits, etc.

License

MIT License

About

A simple URL shortner with Kotlin and Micronaut

Topics

Resources

Stars

Watchers

Forks

Languages