A sleek, minimal URL shortener written in Rust using Axum, SQLx and PostgreSQL. Designed for fast redirects, click stats, and easy deployment on Shuttle.
Features
- Shorten any valid HTTP/HTTPS URL
- Permanent redirects (301)
- Click stats per short URL (/<id>/stats)
- Built with async Rust, SQLx migrations, and Shuttle-ready configuration
Quickstart (local)
- Install prerequisites:
- Rust + Cargo (stable)
- PostgreSQL
- sqlx-cli (optional, for migrations):
cargo install sqlx-cli --no-default-features --features postgres
- Set DATABASE_URL:
- export DATABASE_URL="postgres://user:password@localhost/dbname"
- Run migrations:
sqlx migrate run
- Run the app:
cargo run
- By default the service expects requests at localhost:8000 (used in generated slugs when Host header is provided).
API
-
POST /api/shorten
- Body: JSON
{ "url": "https://example.com" } - Response: 201 Created
{ "slug": "http://localhost:8000/abc123" }
- Body: JSON
-
GET /{id}
- Redirects (301) to the original URL if found, otherwise 404.
-
GET /{id}/stats
- Returns JSON
{ "clicks": 42 }or 404 if not found.
- Returns JSON
Examples
- Shorten
- curl:
curl -X POST -H "Content-Type: application/json" -d '{"url":"rust-lang.org"}' http://localhost:8000/api/shorten
- curl:
- Follow redirect
- Open
http://localhost:8000/<id>in a browser or:curl -I http://localhost:8000/<id>
- Open
- Get stats
curl http://localhost:8000/<id>/stats
Deployment
- Project is Shuttle-ready. To deploy:
- Install Shuttle CLI and follow Shuttle docs.
- Push with required Postgres shared DB configuration; the app uses
shuttle_shared_db::Postgres.
Notes
- URLs without a scheme will be normalized to
https://. - Only
httpandhttpsschemes are allowed. - Migrations are applied at startup (see
sqlx::migrate!()in main).
Contributing
- Open an issue or PR. Keep changes small and documented.