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.5.tar.gz (13.5 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.5-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bh_urlshortener-0.0.5.tar.gz
  • Upload date:
  • Size: 13.5 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.5.tar.gz
Algorithm Hash digest
SHA256 d5ec06482a22d088bda4fd1d7cf0941ef6638a0d3e29808d4bc4aee4ea9c140a
MD5 61287a2a95f543864f20151ed61859fd
BLAKE2b-256 ca998297932626bbf393ff4af26ca3f977932823e160ff624f5c1b8beef5f1f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for bh_urlshortener-0.0.5.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.5-py3-none-any.whl.

File metadata

File hashes

Hashes for bh_urlshortener-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 574bbcda10c9a39566b4295190d872296832881531da23362b02aa16aeb87e3a
MD5 e10bd03d946dad780827b6abb9ae6916
BLAKE2b-256 eac26924dc23878ed5890fb199d78355afbe5781f9407a103f3d940817c49673

See more details on using hashes here.

Provenance

The following attestation bundles were made for bh_urlshortener-0.0.5-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