A simple URL shortener API built with Go, featuring PostgreSQL storage, rate limiting, and API key authentication.
- Shorten long URLs to 6-character codes
- PostgreSQL database storage
- Rate limiting (10 req/sec, burst of 20)
- Simple API key authentication
- Docker deployment
- Health check endpoint
POST /shorten
Headers: X-API-Key: your-secret-api-key
Body: {"url": "https://example.com"}
Response: {"short_url": "http://localhost:8080/abc123", "code": "abc123"}
GET /{code}
Response: 301 redirect to original URL
GET /health
Response: {"status": "healthy"}
-
Run with Docker Compose:
docker-compose up --build
-
Test the API:
# Shorten a URL curl -X POST http://localhost:8080/shorten \ -H "X-API-Key: your-secret-api-key" \ -H "Content-Type: application/json" \ -d '{"url": "https://google.com"}' # Use the short URL (replace abc123 with actual code) curl http://localhost:8080/abc123
DATABASE_URL: PostgreSQL connection stringAPI_KEY: Authentication key for /shorten endpointBASE_URL: Base URL for generated short linksPORT: Server port (default: 8080)
# Install dependencies
go mod tidy
# Run locally (requires PostgreSQL)
go run main.go