Skip to content

G-coding/URLShortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Flask URL Shortener

A simple URL shortener built with Flask, MySQL, and SQLAlchemy.
This project lets you shorten long URLs into short codes and redirect users back to the original links.


🚀 Features

  • Shorten long URLs into unique codes
  • Redirect to original URLs when visiting short links
  • Avoid duplicate entries (reuse code for the same long URL)
  • Health-check endpoint
  • Database migrations using Flask-Migrate

🛠️ Tech Stack

  • Flask (Web framework)
  • MySQL (Database)
  • SQLAlchemy (ORM)
  • Flask-Migrate (Database migrations)
  • dotenv (Environment variable management)

📂 Project Structure

url-shortener/
│── app.py # Main Flask application
│── config.py # Configuration file (DB credentials etc.)
│── requirements.txt # Python dependencies
│── templates/
    └── index.html # Basic UI
│── migrations/ # Auto-generated by Flask-Migrate
└── README.md

⚙️ Setup Instructions

1. Clone the Repository

git clone https://github.com/G-coding/CodeAlpha_URLShortener.git
cd url-shortener

2. Create and Activate Virtual Environment

python -m venv venv
source venv/bin/activate   # Mac/Linux
venv\Scripts\activate      # Windows

3. Install Dependencies

pip install -r requirements.txt

4. Configure Environment Variables

Create a .env file in the project root:

DB_USER=root
DB_PASSWORD=Password@1234
DB_HOST=localhost
DB_NAME=url_shortener

Update config.py to use these environment variables.

🗄️ Database Setup

1. Create MySQL Database
CREATE DATABASE url_shortener;

2. Initialize Migrations
flask db init

3. Generate Migration
flask db migrate -m "Initial migration"

4. Apply Migration
flask db upgrade

▶️ Running the App

Option 1: Flask Run
flask run

Option 2: Python Run
python app.py

By default, the app runs on:
👉 http://127.0.0.1:5000/

📡 API Endpoints

  1. Shorten a URL

POST /api/shorten

Request JSON

{
  "url": "https://www.amazon.in/s?k=wireless+headphones&crid=2TQHPFJ3N1G44&sprefix=wireless+headphones%2Caps%2C256&ref=nb_sb_noss_1"
}

Response JSON

{
  "short_url": "http://127.0.0.1:5000/BI6ZIS7",
  "code": "BI6ZIS7"
}
  1. Redirect

GET/

Redirects the user to the original long URL.

  1. Health Check

GET /health

Response:

{
  "status": "ok"
}

🧑‍💻 Example Usage

curl -X POST http://127.0.0.1:5000/api/shorten \
     -H "Content-Type: application/json" \
     -d '{"url":"https://www.wikipedia.org/"}'

Response:

{
  "short_url": "http://127.0.0.1:5000/aB3xYz1",
  "code": "aB3xYz1"
}

👩‍💻 Author: Developed by Geetika Agarwal as part of an internship project.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors