Skip to content

Gourav-79/URL-Shortner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔗 URL Shortener

A simple, beginner-friendly URL shortener built with Python, Flask, and SQLite.


Features

Feature Description
Shorten URLs Paste any http:// or https:// URL → get a 6-char short link
Redirect Visit /<short_id> → automatically redirected to the original URL
Duplicate detection Same URL submitted twice? Reuses the existing short code
URL validation Rejects empty or malformed input with clear error messages
Recent links list Homepage shows all previously shortened URLs
SQLite storage Zero-config, single-file database (database.db)

Project Structure

URL shortener/
├── app.py              # Main Flask application
├── database.db         # SQLite database (auto-created on first run)
├── requirements.txt    # Python dependencies
├── test_app.py         # Quick smoke tests
├── README.md
├── static/
│   └── style.css       # Dark glassmorphism UI styles
└── templates/
    └── index.html      # Jinja2 homepage template

How to Run Locally

1. Install dependencies

pip install -r requirements.txt

2. Start the development server

python app.py

3. Open in your browser

http://127.0.0.1:5000

Sample Test URLs

URL Purpose
https://www.google.com/search?q=flask+tutorial Long query string
https://github.com/pallets/flask Clean GitHub URL
https://en.wikipedia.org/wiki/URL_shortening Wikipedia article
https://docs.python.org/3/library/sqlite3.html Python docs

How It Works (Key Logic)

  1. User submits a URL via the form on the homepage.
  2. Validation — a regex checks the URL starts with http:// or https://.
  3. Duplicate check — if the URL was shortened before, return the existing short code.
  4. Generate short ID — pick 6 random alphanumeric characters; retry on collision.
  5. Store the mapping (short_id → original_url) in SQLite.
  6. Redirect — when someone visits /<short_id>, look it up and issue a 302 redirect.

Running Tests

With the server running in another terminal:

python test_app.py

Expected output:

[PASS] Redirect test: status=302, location=https://www.google.com/search?q=flask+tutorial
[PASS] Invalid ID test: status=302 (redirect to homepage)
[PASS] Duplicate URL reuse test: 2 rows in DB (expected 2)

All tests passed!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors