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
- Clone the repository:
git clone https://github.com/yourusername/urlshortener.git
cd urlshortener
- Create a virtual environment:
python -m venv myenv
source myenv/bin/activate # On Windows use `myenv\Scripts\activate`
- Install dependencies:
pip install -r requirements.txt
- 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
- Apply migrations:
python manage.py makemigrations && python manage.py migrate
- Create a superuser:
python manage.py createsuperuser
- Run the development server:
python manage.py runserver
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
SQLite
DATABASE_URL=sqlite:///db.sqlite3
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
Usage
Endpoints
- Create Short URL:
POST /shorten/ Content-Type: application/json { "long_url": "https://www.example.com/some/long/url" }
- Retrieve Original URL:
GET /shorten/<short_url>/
- Update Short URL:
PUT /shorten/<short_url>/ Content-Type: application/json { "long_url": "https://www.example.com/some/updated/url" }
- Delete Short URL:
DELETE /shorten/<short_url>/
- Redirect to Original URL:
GET /<short_url>/
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.