Create and manage short links for your website.
- Show a table of short urls and actual urls mapping: http://localhost:8088/
- Generate a new short url from an url: http://localhost:8088/generate-link
- Revert and redirect to the actual url from a short url: http://localhost:8088/redirect
- Allow to custom short url's domain, for example: https://bit.ly, http://localhost:8088/settings
There are two separate apps client and server:
- The client is a ReactJS app. Built with
create-react-app
and usingnode-fetch
to make REST requests to the server. - The server is a Spring Boot app. Implementing Spring MVC, Spring REST, Spring Data JPA. Building with maven and testing with Spring Boot Test and JUnit.
- Database: PostgreSQL 13 via JDBC driver
For the spring boot source code, I did split the app into multiple packages and each of them handling different logic:
- The
com.urlshortener.app.config
package containsConstants
,LoadDatabase
at the first server start up, and aWebConfig
bean to allow CORS access via REST api. - The
com.urlshortener.app.model
package contains domain model entities and mapped with the PostgreSQL schema. - The
com.urlshortener.app.repository
package contains Spring Data JPA repositories. - The
com.urlshortener.app.service
package contains service classes to handle all business logic processing, for example: generate a short url from user input. - The
com.urlshortener.app.web.rest
package contains REST controller methods, exposing API endpoints.
Exceptions are caught and thrown in the services during handling client requests.
If there is any exception raised, it will be handled by a custom handler com.urlshortener.app.service.exception.RestResponseEntityExceptionHandler
,
which is essentially a using Spring's @ControllerAdvice
annotation to return a BAD_REQUEST
(200) code together with error message to the client.
The database schema is located at src/main/resources/schema.sql
. It will be run the first time when the server boots up,
to create table schema for the app.
Run test:
./mvnw test
Please note that: there are only three test cases for LinkController
to demonstrate how to write integration tests with spring boot.
The project is configured to build and deploy with docker
and docker-compose
. The project is built with single command and is a production ready deployment.
- Prerequisite: Install
docker
on your PC from https://docs.docker.com/get-docker/. Normally, that the only need to getdocker
ready (with Docker Desktop) from the above link. It will get docker engine and docker CLI installed and we are good to go. - Unzip the project zip file
url-shortener.zip
andcd url-shortener
- Run
./deploy.sh
script in the project root folder to get everything up and running. - Wait for few seconds when deployment finished, access http://localhost:8088 to use the app.
- Done
The deploy.sh
basically does two important things:
- Package the Spring Boot app with:
./mvnw clean package
, and - Run
docker compose up
to build the client, server app, downloadpostgres
docker image, init the database, and start all servers.
Built from scratch by Yen Nguyen in May 2021