Skip to content

simple URL shortener using FastAPI; tracks short url usage and can report stats via an admin key

License

Notifications You must be signed in to change notification settings

timoteostewart/fastapi-demo-url-shortener

Repository files navigation

Background

Writing a simple URL shortening service has been on my mind for months, and since I've been learning about FastAPI recently, I had the perfect opportunity to turn this small dream into a reality.

Key Features

  • Create shortlinks via API or via command line curl
  • Maintains an access count of how many times the short URL has been accessed
  • Stores when the short URL was generated
  • Provides security by requiring a 16-character admin key to be included to see stats
  • Allows deletion of a shortlink (if requester provides the associated admin key)
  • Uses a super simple SQLite database

Ideas for Improvement

  • Add testing via pytest
  • Add functionality to update the full URL associated with a short url (e.g., PUT /{short_url}/{admin_key}?full_url={full_url})
  • Use a central data store and multiple shortlink servers (e.g., an autoscaling group)
  • Set up multiple read replicas of the data store to speed up redirections
  • Refactor the updating of access_count for each shortlink via messaging or a queue instead of immediate DB access
  • Store the HTTP referrers as an additional stat for each shortlink

Installation and Usage

  1. Clone the repo. Here's a pretty typical way on most platforms.
mkdir fastapi-demo-url-shortener
gh auth login
gh repo clone timoteostewart/fastapi-demo-url-shortener ./fastapi-demo-url-shortener
  1. Create a virtual environment.
cd fastapi-demo-url-shortener
python -m venv .venv
. ./.venv/bin/activate # (or & ./.venv/Scripts/Activate.ps1 on Windows)
  1. Install Python requirements.

requirements.txt is up to date in the repo, but here is the pip install method too:

pip install wheel fastapi[all]
  1. Start the test server.
uvicorn main:app --reload --host 0.0.0.0 --port 8000
  1. When the test server is up and running, you can read the nicely autogenerated API docs.

  2. To create a shortlink, use curl to send a POST request. You'll get back some JSON with the short_url and some other info.

C:\> curl -X POST localhost:8000?full_url=https://www.nytimes.com

{"full_url":"https://www.nytimes.com","short_url":"WAGLf","admin_key":"WafR7whH7ORFN2Vp","when_created_unix":1675316357,"access_count":0}
  1. You can use the shortlink using curl or a local Web browser.
curl -X GET localhost:8000/WAGLf
  1. Statistics about the shortlink are available via a GET method, so you can use either curl or a Web browser. (Notice that the access_count has gone up; I accessed the shortlink a bunch of times in a browser to spice things up!)
C:\> curl -X GET localhost:8000/WAGLf/WafR7whH7ORFN2Vp

{"full_url":"https://www.nytimes.com","short_url":"WAGLf","admin_key":"WafR7whH7ORFN2Vp","when_created_unix":1675316357,"access_count":6}

Screenshots

Using curl on Windows PowerShell to create a shortlink to nytimes.com

Getting an "invalid short URL" error because I mistyped the last character of the short URL

Successful redirection once I type the short URL correctly

License

This repo is MIT licensed, as found in the LICENSE file.

About

simple URL shortener using FastAPI; tracks short url usage and can report stats via an admin key

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published