Projects

URL Shortener

A simple and efficient URL shortening service built with Django and Django REST Framework. This service allows users to shorten long URLs, track the number of times they are accessed, and manage their URLs through a RESTful API.

Features

  • Shorten long URLs
  • Retrieve original URLs from shortened URLs
  • Track the number of times a shortened URL has been accessed
  • Create, retrieve, update, and delete shortened URLs
  • Use Redis as cache for improved performance

Installation

  1. Clone the repository:
    git clone https://github.com/yourusername/urlshortener.git
    bash
    cd urlshortener
    bash
  2. Create a virtual environment:
    python -m venv myenv
    bash
    source myenv/bin/activate # On Windows use `myenv\Scripts\activate`
    bash
  3. Install dependencies:
    pip install -r requirements.txt
    bash
  4. Configure environment variables:
    DATABASE_URL=postgres://user:password@host:port/dbname
    DJANGO_SECRET_KEY=your_secret_key
    REDIS_URL=redis://your_redis_host:your_redis_port/1
    bash
  5. Apply migrations:
    python manage.py makemigrations && python manage.py migrate
    bash
  6. Create a superuser:
    python manage.py createsuperuser
    bash
  7. Run the development server:
    python manage.py runserver
    bash

Database Configuration

You can configure your project to use different databases by setting the DATABASE_URL environment variable in your .env file.

PostgreSQL

DATABASE_URL=postgres://user:password@host:port/dbname
bash

SQLite

DATABASE_URL=sqlite:///db.sqlite3
bash

But by default Django uses SQLite...

Cache Configuration

Redis is used as the cache for this project. Make sure to set the REDIS_URL environment variable in your .env file:

REDIS_URL=redis://your_redis_host:your_redis_port/1
bash

Usage

Endpoints

  • Create Short URL:
    POST /shorten/
    Content-Type: application/json
    { "long_url": "https://www.example.com/some/long/url" }
    bash
  • Retrieve Original URL:
    GET /shorten/<short_url>/
    bash
  • Update Short URL:
    PUT /shorten/<short_url>/
    Content-Type: application/json
    { "long_url": "https://www.example.com/some/updated/url" }
    bash
  • Delete Short URL:
    DELETE /shorten/<short_url>/
    bash
  • Redirect to Original URL:
    GET /<short_url>/
    bash

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

This project was inspired and guided by the URL Shortening Service Project from roadmap.sh. Their comprehensive guide provided valuable insights and structure for developing this URL shortening service.