A TinyURL-style URL shortening service built in Go with redirecting, analytics, and database storage.
- Go 1.21+
- Gin (HTTP router)
- GORM (ORM)
- SQLite (pure Go driver - no CGO required)
go-url-shortener/
├── cmd/
│ └── server/
│ └── main.go # Entry point
├── models/
│ └── url.go # URL database model
├── handlers/
│ └── handlers.go # HTTP handlers (POST /shorten, GET /:code)
├── database/
│ └── database.go # DB connection & initialization
├── utils/
│ └── code.go # Code generation utility
├── web/
│ └── index.html # Web frontend UI
└── go.mod # Go module file
- Install Go from golang.org
- Verify installation:
go version
-
Install dependencies:
go mod download
-
Run the server:
go run ./cmd/server/main.go
Or build and run:
go build ./cmd/server ./server
-
Access the web interface:
Open your browser and navigate to:
http://localhost:8080You'll see a beautiful web interface where you can:
- Enter a long URL
- Click "Shorten URL" to create a short link
- Copy the short URL with one click
- Test redirects by visiting the short URL
-
Test the API (alternative to web UI):
Create a short URL:
# Using curl curl -X POST http://localhost:8080/shorten \ -H "Content-Type: application/json" \ -d '{"url": "https://example.com/very/long/url"}' # Using PowerShell Invoke-RestMethod -Uri http://localhost:8080/shorten -Method Post -ContentType "application/json" -Body '{"url":"https://example.com"}'
Response:
{ "code": "Mu4DZ6", "short_url": "http://localhost:8080/Mu4DZ6" }Access the short URL:
# Open in browser or use curl curl http://localhost:8080/Mu4DZ6 # Browser will automatically redirect to the original URL
- Database setup with SQLite (pure Go driver)
- Code generation utility
- URL model with GORM
- Shorten handler (POST /shorten)
- Redirect handler (GET /:code) with visit tracking
- Main server setup and routing
- Web frontend UI (HTML/CSS/JavaScript)
- Testing and verification
- ✅ Web Interface — Beautiful, modern UI for shortening URLs
- ✅
POST /shorten— create a short code for a long URL - ✅
GET /:code— redirect to original URL and increment visits - ✅ Persistent storage of mappings and visit counts
- ✅ Automatic collision handling for short codes
- ✅ Visit counter tracking
- ✅ Pure Go implementation (no CGO required)
- ✅ No npm/build tools required — simple HTML/CSS/JS frontend
Creates a short URL from a long URL.
Request:
{
"url": "https://example.com/very/long/url"
}Response:
{
"code": "abc123",
"short_url": "http://localhost:8080/abc123"
}Redirects to the original URL and increments the visit counter.
Response: HTTP 302 Redirect to the original URL
Serves the web interface for easy URL shortening.
Response: HTML page with interactive UI
- Rate limiting
- Custom aliases
- Expiration dates
- Admin dashboard
- Analytics endpoint
- Dockerfile & compose for easy deployment