Skip to content

XERXES-MOUSAVI/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL-Shortener

CI Go 1.22+ License

A pure-standard-library URL shortener written in Go:

  • JWT-protected POST /save
  • Custom URL type 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.


Project layout

├── 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)

Quick start

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 :8080

Generate 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.

REST API

Authorization: Bearer <jwt>
Content-Type: application/json

{
  "URL": "https://a.com/",
  "TTL": 10                 // seconds, optional (default 300)
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages