+
Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.
/ url-shortener Public archive

URL Shortener using FastAPI, based on a realpython.com tutorial, with additional functionality

Notifications You must be signed in to change notification settings

seapagan/url-shortener

Repository files navigation

FastAPI-based URL Shortener

This repository is DEPRECATED! Use url-shortener2 instead which is a re-written version of this repositry with Users and Authentication/Authorization baked in.

This is based on an original tutorial from Real Python which I definitely recommend checking out for a decent example of a non-trivial FastAPI app.

Added Features

  • Refactored the code so that each 'group' of Routes is in its own file, and moved some utility & error functions to a dedicated module. This makes the project much cleaner and easier to understand. Add tags to each group for better documentation.
  • /list (GET) route to return a list of all the URL's in the database along with their target.
  • /{url_key}/peek (GET) route to show the target url of the specified url_key, without actually redirecting there. Allows users or front-end client to check the URL before visiting.
  • /admin/{secret_key} (PATCH) route to change the target URL of a link identified by the secret_key. The body of the request needs to have the target_url property containing the new URL which must be a valid URL
  • The Root Path ("/") will return a short HTML template if viewed in a Web Browser, JSON otherwise.
  • Choose either the default SQLite database or Postgresql from the .env file.
  • The URL Key is case-sensitive, so added lowercase letters to the mix for extra availability. Now the key will randomly include upper, lower and numbers.

Planned Features

Non-exhaustive list of planned additions, in no specific order.

  • Option to add a delay to the redirect, showing the exact target URL and giving the option to Cancel.
  • User-friendly Front-end (probably in React) for adding and editing URLs.
  • Protected ability to purge all is_active: false URLs
  • Custom URL key - allow user to use their own key instead of the autogenerated one.
  • Add Users and Auth, replace the current 'SECRET_KEY' method of dealing with ownership.

Configuration

This is done using the .env file in the root folder. See .env.example for details:

ENV_NAME="Development"
BASE_URL="http://127.0.0.1:8000"

# Comment/Uncomment your choice of database. If Postgreql, the other 5 variables
# below need to be also filled. Will default to SQLite if none selected.
DB_BACKEND="sqlite"
# DB_BACKEND="postgresql"

# location and name of SQLite database, if used.
DB_URL="sqlite:///./shortener.db"

# Postgresql stuff if selected above.
DB_NAME="<YOUR_DB_NAME>"
DB_ADDRESS="<YOUR_DB_ADDRESS>"
DB_PORT="<YOUR_DB_PORT>"
DB_USER="<YOUR_DB_USER>"
DB_PW="<YOUR_DB_PASSWORD>"

Development

Install the required dependency packages :

pip install -r requirements.txt

Run a local development server from the project root using Uvicorn :

uvicorn shortener_app.main:app --reload

Access the API at http://localhost:8000

See the API Docs at http://localhost:8000/docs or http://localhost:8000/redoc for a list of the active endpoints

About

URL Shortener using FastAPI, based on a realpython.com tutorial, with additional functionality

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载