Coder Social home page Coder Social logo

url-shortener-fastapi's Introduction

URL Shortener Web App / API using Python, FastAPI, and MongoDB

A simple FastAPI app that shortens a URL and redirects browser to the original URL.

Demo available at https://ay-us.herokuapp.com/

Requirements

Developed and tested with:

  • Windows 11
    • Should work with other versions of Windows and Linux
  • Firefox 94
  • Python 3.9
  • Miniconda3
  • Python packages listed in requirements.txt
  • Bootstrap 5.1.3

How to Run

  1. Create a file named app.env and insert the environment variables below:
    BASE_URL=http://127.0.0.1:8000
    MONGODB_URI=mongodb+srv://user:<password>@cluster0.abc.mongodb.net/url-shortener?retryWrites=true&w=majority
    MONGODB_URI_TESTS=mongodb+srv://user:<password>@cluster0.abc.mongodb.net/url-shortener-tests?retryWrites=true&w=majority
    RNG_LENGTH=5
    • BASE_URL is the domain name of your server
      • The default is http://127.0.0.1:8000 if running locally
    • MONGODB_URI is your connection string to MongoDB for real database. Get this from your MongoDB dashboard
    • MONGODB_URI_TESTS is your connection string to MongoDB for test database. Get this from your MongoDB dashboard
    • RNG_LENGTH is the number of characters the short URL has
    • To actually set these variables, make your IDE automatically export them via a .env file (PyCharm has a plugin for this), or export them manually
  2. Activate virtual environment and execute uvicorn app.main:app
    1. Add --reload to make the server restart after code changes [Only use for development]
  3. Open your BASE_URL in a browser to view the home page

How to Run Tests

  1. Execute pytest in terminal

List of API

[1] POST: /api/shorten

Input

Request body:

{
  "url": "string",
  "custom_name": "string"
}

Example request body:

{
  "url": "https://duckduckgo.com",
  "custom_name": "ddg"
}

Output

Response:

{
  "url": "string",
  "short_url": "string"
}

Example response:

{
  "url": "https://duckduckgo.com",
  "short_url": "https://me.com/ddg"
}

Future Improvements / Features

  1. Check the original URL if it's already in database, and return the existing short URL instead of creating a new one
    1. Will reduce duplicate entries
    2. Added in commit 2c7ff7c
  2. Remove unvisited link after a certain length of time has elapsed
    1. Will reduce size of database
    2. Less hoarding of custom names
  3. Use Docker to containerize this app
    1. Easier to deploy
  4. A page to show statistics
    1. Top 10 URLs, etc
  5. Prettier home page with mobile version
    1. Added in commit 6022791

References & Credits

  1. Blog post by Similoluwa Okunowo
    1. The main inspiration for this project. I reused many of their codes and added my own flavor where I see fit
  2. FastAPI documentation
  3. Bootstrap documentation

url-shortener-fastapi's People

Contributors

adibyhy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.