This project is a URL shortener implemented using FastAPI and SQLite as the database. It provides endpoints to shorten URLs, redirect based on short URLs, track click counts, and reuse deleted URLs efficiently. The URL generator is optimized for low memory usage and ensures no duplicates by using a four-pointer system to introduce randomness and improve efficiency.
- Generate unique, short URLs from long URLs.
- Efficient URL reuse system using a pool of deleted URLs.
- Track the number of clicks each shortened URL receives.
- Optimized URL generation with low memory usage.
- Full API documentation available via FastAPI.
.
├── generator.py # Contains the logic for generating short URLs.
├── short_url_fastapi.py # The main FastAPI application file with all the endpoints.
├── database.py # Database configuration and setup using SQLAlchemy.
├── models.py # Defines the database models for URL mappings and reuse pool.
-
Short URL Generation:
- The
generate_short_url
function generates URLs using a base-26 system (A-Z). The system keeps track of four pointers (start
,end
,middle
, andmiddle_plus_one
) to add randomness to the generated URLs. - This allows for efficient URL generation without consuming large amounts of memory and ensures there are no duplicates.
- The
-
Reuse Pool:
- Deleted short URLs are added to a reuse pool. When generating a new short URL, the system first checks the pool for available URLs, ensuring efficient reuse of short URLs.
-
Database Persistence:
- All URLs and click counts are stored in an SQLite database, ensuring data persists across server restarts.
- Python 3.8+
- FastAPI and SQLAlchemy
- SQLite (bundled with Python)
Install the required dependencies using pip:
pip install fastapi uvicorn sqlalchemy
-
Clone the Repository:
git clone https://github.com/your-username/url-shortener.git cd url-shortener
-
Install Dependencies:
pip install -r requirements.txt
-
Run the Application:
You can run the FastAPI application with Uvicorn:
uvicorn short_url_fastapi:app --reload
This will start the application at
http://127.0.0.1:8000
.
You can use Postman or curl to interact with the API.
http://127.0.0.1:8000
-
POST /shorten
Description: Shorten a new URL.
-
Request:
- Method:
POST
- Body:
{ "url": "<original_url>" }
- Method:
-
Example Request:
{ "url": "https://www.netflix.com" }
-
Example Response:
{ "short_url": "AAAAA", "original_url": "https://www.netflix.com" }
Postman Example:
- Method: POST
- Body: JSON
{ "url": "https://www.netflix.com" }
- Response:
{ "short_url": "AAAAA", "original_url": "https://www.netflix.com" }
-
-
GET /{short_url}
Description: Redirect to the original URL using the short URL.
-
Request:
- Method:
GET
- URL:
/{short_url}
- Method:
-
Example Request:
GET /AAAAA
-
Example Response:
The response will redirect you to the original URL.
Postman Example:
- Method: GET
- URL:
http://127.0.0.1:8000/AAAAA
-
-
GET /clicks/{short_url}
Description: Get the number of times a short URL has been accessed.
-
Request:
- Method:
GET
- URL:
/clicks/{short_url}
- Method:
-
Example Request:
GET /clicks/AAAAA
-
Example Response:
{ "short_url": "AAAAA", "clicks": 1 }
Postman Example:
- Method: GET
- URL:
http://127.0.0.1:8000/clicks/AAAAA
-
-
DELETE /delete/{short_url}
Description: Delete a short URL and add it to the reuse pool.
-
Request:
- Method:
DELETE
- URL:
/delete/{short_url}
- Method:
-
Example Request:
DELETE /delete/AAAAA
-
Example Response:
{ "message": "Short URL deleted and returned to the pool" }
Postman Example:
- Method: DELETE
- URL:
http://127.0.0.1:8000/delete/AAAAA
-
-
GET /short_urls
Description: Retrieve all currently used short URLs.
-
Request:
- Method:
GET
- URL:
/short_urls
- Method:
-
Example Response:
{ "short_urls": ["AAAAA", "BBBBB"] }
Postman Example:
- Method: GET
- URL:
http://127.0.0.1:8000/short_urls
-
-
GET /reuse_pool
Description: Retrieve all URLs in the reuse pool.
-
Request:
- Method:
GET
- URL:
/reuse_pool
- Method:
-
Example Response:
{ "reuse_pool": ["CCCC", "DDDD"] }
Postman Example:
- Method: GET
- URL:
http://127.0.0.1:8000/reuse_pool
-
The short URL generator is optimized for low memory usage and avoids duplicates. Here's how it works:
- Base-26 Encoding: The generator uses a base-26 system (A-Z) to create 5-character short URLs.
- Four-Pointer System: To introduce randomness and avoid collisions:
- Four pointers (
start
,end
,middle
, andmiddle_plus_one
) are used to generate URLs from different parts of the range, ensuring uniform distribution.
- Four pointers (
- Efficient Memory Usage: The generator doesn't store all possible combinations in memory but generates URLs dynamically as needed, ensuring optimal memory usage for large-scale operations.
- Clone the repository and navigate to the project directory.
- Install dependencies:
pip install -r requirements.txt
- Start the FastAPI server:
uvicorn short_url_fastapi:app --reload
- Access the API documentation:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
Feel free to contribute or raise issues in this repository!