Skip to content

A FastAPI-based application that shortens long URLs into short, shareable links and handles redirection. Features include logging, error handling, and URL management.

Notifications You must be signed in to change notification settings

vaheedsk36/url-shortner-backend

Repository files navigation

URL Shortener

A simple URL shortener service built with FastAPI and MongoDB. This application allows users to shorten URLs and retrieve the original URL using a short identifier.

Features

  • Shorten URLs
  • Redirect to original URL using a short identifier
  • Health check endpoint

Badges

Python Version FastAPI Version MongoDB Docker

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/url-shortener.git
    cd url-shortener
  2. Create a virtual environment and activate it:

    python3 -m venv venv
    source venv/bin/activate  # On Windows use: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Create a .env file in the root directory and add your environment variables:

    DATABASE_URL=<your-db-url>
    DATABASE_NAME=url_shortener_db
    PORT=8000

Usage

  1. Run the FastAPI application:

    python main.py
  2. Access the API:

    • Health Check: GET /health-check/ - Check the status of the service.
    • Shorten URL: POST /shorten/ - Request body should include original_url.
    • Redirect URL: GET /{short_url} - Redirects to the original URL.
  3. Docker:

    Build and run the Docker container:

    docker build -t url-shortener .
    docker run -p 8000:8000 --env-file .env url-shortener

Logging

Logs are configured to be written to a file named with the current date and rotated daily. Older logs are archived in zip files.

Contributing

Feel free to open issues and pull requests to contribute to this project.

License

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

About

A FastAPI-based application that shortens long URLs into short, shareable links and handles redirection. Features include logging, error handling, and URL management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published