ShortLink-go is a Gin framework based URL shortening service, designed to be performant, horizontally scalable, and to work alongside PostgreSQL (or compatible databases e.g., CockroachDB, Aurora, Spanner) and Redis. See How it Works for more details.
- Docker and Docker Compose
- Golang 1.22+
Clone the Repository
git clone https://github.com/victortv7/shortlink-go.git
cd shortlink-go
Using Docker Compose
docker-compose up
Using Makefile
-
Start the PostgreSQL, run DB migrations, and start Redis:
make db-up make db-migrate make redis-up
-
Configure the environment variables (see .env.example) or use the default values.
-
Run the application:
make run
Run unit tests and generate a coverage report:
make test
make test-coverage
make lint
make fmt
http://localhost:8080/docs
curl -X 'POST' \
'http://localhost:8080/create' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"long_url": "https://www.example.com"
}'
The response has the following format:
{
"short_link": "a4BhE"
}
To test the redirection functionality, simply navigate to the short link URL in your web browser or use a curl
command like this:
curl -L 'http://localhost:8080/{short_link}'
curl 'http://localhost:8080/stats/{short_link}'
ShortLink-go generates short links from long URLs and tracks their usage. Here's a brief overview of its core functionality:
-
Short Link Creation: When a long URL is submitted, the application creates a new entry in the database with the URL (
long_url
) and an access count (access_count
) set to zero. It then encodes the database entry's ID using Base62 to generate a unique short link. This short link is also stored in Redis for quick access. -
URL Redirection: To redirect a short link to its original long URL, the application first checks Redis. If the short link is not found in Redis, it decodes the short link to retrieve the database ID, queries the database for the long URL, and updates Redis. This ensures subsequent accesses are faster.
-
Access Count: Each time a short link is accessed, its access count is incremented in the database to track how many times the short link has been used. This database write is done asynchronously in a background task to improve the latency of redirects.
make db-down
make redis-down
make clean
ShortLink-go is licensed under the MIT License. See LICENSE for details.