A simple, high-performance URL shortener service built with Go and Gin web framework, featuring multiple storage backends and a clean architecture.
- URL Shortening: Convert long URLs to short, memorable links
- Automatic Redirection: Short URLs automatically redirect to their original destination
- Multiple Storage Backends:
- In-memory (default, non-persistent)
- Redis (persistent, recommended for production)
- RESTful API: Simple and intuitive API endpoints
- Health Check: Built-in health check endpoint
- Environment-based Configuration: Easy configuration through environment variables
- Concurrent-safe: Thread-safe implementation for high concurrency
Create a .env
file in the cmd/url-shortener
directory with the following variables:
PORT=8080
REDIS_ADDR=localhost:6379 # Only required if using Redis
- (Optional) Redis server if using Redis storage
-
Clone the repository:
git clone https://github.com/pj1527/url-shortner.git cd url-shortner
-
Install dependencies:
go mod download
go run cmd/url-shortener/main.go
- Make sure Redis is running
- Set up your
.env
file with Redis configuration - Run the application:
go run cmd/url-shortener/main.go
Request
curl -X POST http://localhost:8080/api/shorten \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/very/long/url"}'
Response
{
"short_url": "http://localhost:8080/abc123"
}
Request
curl -L http://localhost:8080/abc123
Request
curl http://localhost:8080/health
Response:
{
"status": "UP"
}
The application follows a clean architecture with clear separation of concerns:
- Handler: HTTP request handling and response formatting
- Service: Business logic and use cases
- Repository: Data access layer with support for multiple storage backends
- Config: Environment configuration management
- Utils: Helper functions and utilities
Environment Variable | Default | Description |
---|---|---|
PORT |
8080 |
Port to run the server on |
REDIS_ADDR |
localhost:6379 |
Redis server address (if using Redis) |