Skip to content

rodd-oss/shurl

Repository files navigation

rust-url-shortner (quick view)

Small Rust URL shortener service built with Axum + SQLite.

It supports:

  • HTML form (/) and JSON API (/api/shorten) for creating short links
  • Redirects via /{slug}
  • SQLite migrations and file-backed DB tuning (WAL + busy timeout)
  • In-memory rate limiting (per client/IP)
  • Health and Prometheus-style metrics endpoints
  • Buffered asynchronous hit-count persistence for redirects
  • Tests and Criterion benchmarks for SQLite contention / hit-count buffering

Quick start

Local (Cargo)

cargo run

Defaults:

  • App: http://localhost:3000
  • DB: sqlite://data/shortener.db?mode=rwc

Docker Compose

docker compose up --build

Endpoints

  • GET / - simple HTML form UI
  • POST /shorten - form submit
  • POST /api/shorten - JSON API ({"url":"https://example.com"})
  • GET /{slug} - 302 redirect
  • GET /health - health check JSON
  • GET /metrics - Prometheus text metrics

Example API call

curl -s http://localhost:3000/api/shorten \
  -H 'content-type: application/json' \
  -d '{"url":"https://example.com"}'

Configuration (env)

Common vars (see src/config.rs):

  • HOST (default 0.0.0.0)
  • PORT (default 3000)
  • BASE_URL (default http://localhost:<PORT>)
  • DATABASE_URL (default sqlite://data/shortener.db?mode=rwc)
  • RATE_LIMIT_RPS / RATE_LIMIT_BURST
  • TRUST_PROXY_HEADERS (use x-forwarded-for for client identity)
  • SQLITE_BUSY_TIMEOUT_MS

Hit-count writer tuning:

  • HIT_COUNT_QUEUE_SIZE
  • HIT_COUNT_OVERFLOW_MAX_EVENTS
  • HIT_COUNT_FLUSH_INTERVAL_MS
  • HIT_COUNT_MAX_BATCH_EVENTS
  • HIT_COUNT_ENQUEUE_TIMEOUT_MS

Project layout (quick map)

  • src/main.rs - binary entrypoint
  • src/lib.rs - crate wiring and public exports
  • src/app.rs - app startup, router, middleware, graceful shutdown
  • src/handlers.rs - HTTP handlers (UI/API/redirect/health/metrics)
  • src/service.rs - URL normalization, slug generation, DB service logic
  • src/db.rs - SQLite pool setup, migrations, WAL/busy-timeout tuning
  • src/hit_counter.rs - buffered async hit-count queue + worker
  • src/metrics.rs - in-memory counters/histogram + Prometheus rendering
  • src/rate_limiter.rs - in-memory token-bucket limiter
  • src/views.rs - HTML rendering for the home form
  • src/models.rs - API DTOs and DB row conversion
  • src/tests.rs - integration-style tests (Axum router + SQLite)
  • migrations/0001_create_links.sql - schema for links
  • benches/sqlite_busy.rs - SQLite busy/lock contention benchmark
  • benches/hit_count_buffered.rs - redirect hit-count buffering benchmark
  • Dockerfile / compose.yaml - containerized local run

Dev commands

cargo test
cargo bench
cargo check
cargo clippy --all-targets --all-features -- -D warnings

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors