Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔗 URL Shortener Service

A simple URL shortening service like bit.ly or tinyurl.com, built using Python and Flask.


🚀 Features

  • Shorten long URLs to a short 6-character alphanumeric code.
  • Redirect using the short code.
  • Track and view analytics (click count, original URL, creation timestamp).
  • In-memory storage (no database).
  • REST API with JSON responses.

🛠 Prerequisites

  • Python 3.8+
  • pip (Python package manager)
  • 3 hours of focused development time (for challenge)

📁 Project Setup

  1. Clone the repository
    git clone https://github.com/Nikhilks2002/URL-Shortner.git
    cd url-shortener

``

  1. Install dependencies

    pip install -r requirements.txt

▶️ Run the Flask Server

In a new terminal window:

On Windows PowerShell:

$env:FLASK_APP = "app.main"
python -m flask run

On macOS/Linux (or Git Bash):

export FLASK_APP=app.main
python -m flask run

You should see:

 Running on http://127.0.0.1:5000

✅ Running the Tests

In a separate terminal while the Flask server is running:

pytest

Alternatively, if you have a custom test file (e.g., test_api.py):

python test_api.py

✅ Make sure the Flask server is running before you run tests — otherwise requests will fail with connection errors.


🔄 Example API Usage

1. Shorten a URL

curl -X POST http://localhost:5000/api/shorten \
  -H "Content-Type: application/json" \
  -d '{"url": "https://www.example.com/very/long/url"}'

Response:

{
  "short_code": "abc123",
  "short_url": "http://localhost:5000/abc123"
}

2. Redirect to Original URL

curl -L http://localhost:5000/abc123

3. Get Analytics

curl http://localhost:5000/api/stats/abc123

Response:

{
  "url": "https://www.example.com/very/long/url",
  "clicks": 5,
  "created_at": "2024-01-01T10:00:00"
}

🧪 Testing Notes

  • At least 5 tests cover:

    • Shorten logic
    • Redirection
    • Analytics
    • Error cases (e.g., invalid URL, missing short code)
    • Concurrent behavior

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages