A simple and efficient URL shortening service built with Go, Gin web framework, and Redis as a storage backend.
It allows users to shorten long URLs, manage custom short codes, track click counts, and set expiry times for shortened links.
- Shorten URLs (auto-generated or with custom codes)
- Set expiry times (default 7 days, or custom expiry)
- Track click counts for each shortened link
- Get info about a shortened URL (created time, clicks, expiry)
- List all URLs stored
- Delete a shortened URL manually
- Automatic cleanup of expired links
- Graceful shutdown handling
- Simple rate limiting per IP address to prevent abuse
- Redis backend for fast storage
- Local memory (rate limiters) cleaned periodically
- Go (Golang)
- Gin Web Framework
- Redis (Storage backend)
- Gin handles HTTP routing and middleware
- Redis stores URL metadata (
LongURL,Clicks,CreatedAt,Expiry) - Memory (map) tracks client IP rate limits
- Goroutines and tickers handle background tasks:
- Cleaning expired Redis entries
- Cleaning old rate limiter data
- Graceful Shutdown ensures all processes stop safely when the server exits
Example request to shorten a URL:
POST /shorten
{
"url": "https://example.com",
"custom_code": "mycode", // optional
"expiry_seconds": 3600 // optional
}This project can run in two modes:
- Using local in-memory + JSON persistence.
- Using Redis for persistence (external Redis server required).
- Go 1.18+ installed.
- Redis installed and running (only if using Redis mode).
- (Optional) direnv or manually export environment variables.
-
Clone the repository:
git clone https://github.com/AumSahayata/URL-shortener-using-Go cd URL-shortener-using-Go/using-json -
Run the project:
go run main.go
-
Access the app:
Open your browser at http://localhost:8080
-
Clone the repository:
git clone https://github.com/AumSahayata/URL-shortener-using-Go cd URL-shortener-using-Go/using-redis -
Set up a Redis server (locally or cloud hosted like Redis Cloud).
-
Create a
.envfile inside your project directory with the following content:REDIS_ADDR=localhost:6379 (OR cloud database address) REDIS_USER=redis_user # leave empty if no username REDIS_PASSWORD=redis_password # set password if needed REDIS_DB=0 # Redis database number
-
Install dependencies (if not already):
go mod tidy
-
Run the project:
go run . -
Access the app:
Open your browser at http://localhost:8080
| Method | Endpoint | Description |
|---|---|---|
| POST | /shorten |
Shorten a new URL |
| GET | /:code |
Redirect to original URL |
| GET | /info/:code |
Get details about a short URL |
| GET | /list |
List all URLs |
| DELETE | /delete/:code |
Delete a shortened URL |
- Rate limiting: Max 5 requests/minute per IP.
- Expired links are automatically cleaned every 24 hours.
- Graceful shutdown is implemented (CTRL+C to terminate cleanly).
- Default short URL expiry is 7 days, customizable per link.
Pull requests are welcome!
Feel free to fork the project, open issues, and submit PRs!
Any suggestions to improve it further are appreciated.
Built with ❤️ by Aum Sahayata.