Skip to main content

A simple URL shortener service.

Project description

urlshortener

A self-hosted URL shortener. Shorten a URL, get a redirect, track visit counts. That's it.

API

Method Path Description
POST /shorten Create a short link
GET /{code} Redirect to original URL (307)
GET /stats/{code} Visit count + original URL
GET /healthz Liveness + DB connectivity check
# shorten
curl -X POST http://localhost:8000/shorten \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/some/long/path"}'
# {"code": "aB3kR7z", "short_url": "http://localhost:8000/aB3kR7z"}

# stats
curl http://localhost:8000/stats/aB3kR7z
# {"code": "aB3kR7z", "url": "https://example.com/some/long/path", "visits": 4}

Running locally

Prerequisites: Python 3.11, a running PostgreSQL instance.

pip install -e .

# point at your DB (or rely on the default below)
export URLSHORTENER_DATABASE_URL="postgresql+psycopg://user:pass@localhost:5432/urlshortener"

urlshortener serve
# listening on http://127.0.0.1:8000

Docker Compose

Spins up Postgres + app together:

docker compose up -d 

Configuration

All settings are environment variables with the URLSHORTENER_ prefix.

Variable Default Description
URLSHORTENER_DATABASE_URL postgresql+psycopg://urlshortener:urlshortener@localhost:5432/urlshortener SQLAlchemy connection string
URLSHORTENER_BASE_URL http://localhost:8000 Public base URL used when building short links
URLSHORTENER_HOST 127.0.0.1 Bind address
URLSHORTENER_PORT 8000 Bind port
URLSHORTENER_CODE_LENGTH 7 Length of generated short codes (4–32)
URLSHORTENER_LOG_LEVEL INFO Root log level

Development

pip install -e ".[test]"

pytest          # runs tests + coverage
mypy src        # strict type checking

Tests use an in-memory fake store

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bh_urlshortener-0.0.4.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bh_urlshortener-0.0.4-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file bh_urlshortener-0.0.4.tar.gz.

File metadata

  • Download URL: bh_urlshortener-0.0.4.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bh_urlshortener-0.0.4.tar.gz
Algorithm Hash digest
SHA256 483030edc7828eab5648b32bc686b5e2d918da2b73c4aee7867b7f953155a4b6
MD5 6fae2d4eb4af8e931d09450241b8a532
BLAKE2b-256 7e57759053090d694e6856ddc95b6c347888bea35c4178fdc64e827c3605a316

See more details on using hashes here.

Provenance

The following attestation bundles were made for bh_urlshortener-0.0.4.tar.gz:

Publisher: release.yml on abin-m/urlshortener

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bh_urlshortener-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for bh_urlshortener-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d4a97c3c586f282f7ff9ec6f9d60c42259d6cb804605880afbeff22d4837d3fc
MD5 cf5516577c570658d5b8e6beb8baa837
BLAKE2b-256 088993c5a51a4cd0c8d9d44bb0856acef00e340fff4c6a0057701ef7a82dab93

See more details on using hashes here.

Provenance

The following attestation bundles were made for bh_urlshortener-0.0.4-py3-none-any.whl:

Publisher: release.yml on abin-m/urlshortener

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page