A pure-standard-library URL shortener written in Go:
- JWT-protected
POST /save - Custom
URLtype with JSON validation - In-memory store + TTL eviction + graceful dump/restore
- Rate-limit & metrics middlewares
- No external dependencies
- Complete unit tests & benchmarks
- Continuous Integration with GitHub Actions
Copy the repo, run
go run ./cmd/server, and you’ll have a working shortener in seconds.
├── cmd/server – main() entry-point
├── internal/ – private application code
│ ├── config/ – ENV + defaults
│ ├── httpserver/ – router & middlewares
│ ├── shortener/ – domain types + store
│ └── logger/ – thin wrapper over log.Logger
├── pkg/idgen/ – reusable random-ID generator
├── test/ – black-box integration tests
└── data/ – runtime dump lives here (git-ignored)git clone git@github.com:your-name/url-shortener.git
cd url-shortener
go mod tidy
# run tests & static analysis
go vet ./...
go test -race ./...
# launch the server (env var HMAC_KEY is required)
export HMAC_KEY=secret
go run ./cmd/server # listens on :8080Generate a one-hour HS256 token (replace the secret if you changed it):
TOKEN=$(./scripts/jwt-one-liner.sh)Save a URL:
curl -X POST http://localhost:8080/save \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"URL":"https://golang.org","TTL":60}'
# {"RandomID":"Gk80mxT1"}After TTL seconds the same request returns 404.
Authorization: Bearer <jwt>
Content-Type: application/json
{
"URL": "https://a.com/",
"TTL": 10 // seconds, optional (default 300)
}