A high-performance URL Shortener API built with Golang, designed using Clean Architecture, and powered by Fiber, GORM, PostgreSQL, and Swagger for API documentation. This service lets users shorten long URLs and redirect them through a unique short code.
| Layer | Technology |
|---|---|
| Language | Go (Golang) |
| Framework | Fiber |
| ORM | GORM |
| DB | PostgreSQL |
| Config | Viper |
| Logger | Logrus |
| Docs | Swagger (swaggo/swag) |
| Container | Docker, Docker Compose |
- Clone the repo
https://github.com/FJericho/url-shortener.git- Navigate to the root directory
cd url-shortener- Create a .env file in the root directory
touch .env
# or
echo > .env- Add environment variable
APP_PORT=Application port(example: 3000)
DB_HOST= Database host (example: localhost)
DB_PORT= Database port (example: 5432)
DB_USER= Database username (example: postgres)
DB_PASSWORD= Database user password (example: postgres)
DB_NAME=Name of the database to be used (example: shortener_db)- Add Docker Compose
services:
postgres:
image: postgres:alpine
restart: always
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
ports:
- "${DB_PORT}:5432"
volumes:
- db:/var/lib/postgresql/data
volumes:
db:
driver: local- Run Docker Compose
docker-compose up -d- Run the App Locally
go run cmd/main.goThe API documentation can be found
http://localhost:3000/swagger/index.htmlEndpoint:
POST /shorten
Description:
Shortens a given long URL by generating a unique short code.
{
"original": "https://example.com/very/long/url"
}Status: 200 OK
{
"message": "Short url successfully",
"data": {
"original": "https://example.com/very/long/url",
"short_code": "a1B2c3"
}
}Status: 400 Bad Request
{
"message": "Invalid request. Please check your input."
}Endpoint:
GET /:short_code
Description:
Redirects the user to the original URL based on the short code.
GET localhost:3000/a1B2c3Status: 301 Moved Permanently (redirects to the original URL)
Status: 404 Not Found
{
"message": "Short URL not found"
}
Endpoint:
GET /api/url/:short_code
Description:
Returns the original URL and its short code without redirection.
GET localhost:3000/api/url/a1B2c3Status: 200 OK
{
"message": "Get original url successfully",
"data": {
"original": "https://example.com/very/long/url",
"short_code": "a1B2c3"
}
}Status: 400 Bad Request
{
"message": "Short URL not found"
}
- Redis is NOT used in this project.
- Short codes are automatically generated using a random mix of letters and numbers.