This is a url shortening service implemented in Python with the FastAPI framework. It runs locally with supprt from Uvicorn to act as a web server.
The app is a simplified version of the one implemented by Philipp Acsany in his tutorial, most notably because it does not use a database.
To run the app, please install fastapi
, uvicorn
, and validators
.
This project was developed in a conda environment. If you are also using conda, install the packages by running
conda install -c conda-forge fastapi
conda install -c conda-forge uvicorn
conda install -c conda-forge validators
If you are not using conda, run
pip install fastapi
pip install uvicorn
pip install validators
To use the app, start the Uvicorn server by navigating to the url-shortener-project
directory via the command line. In url-shortener-project
run
uvicorn url_shortener_app.main:app --reload
If the start-up was successful, you should see Application startup complete
printed in the command line. You can check that Uvicorn is running by entering http://localhost:8000/ into your browser.
You can use the app via the command line, an API like Postman, or your web browser by navigating to http://localhost:8000/docs.
- Click on the /encode dropdown
- Select
Try it out
- Replace
"string"
in theRequest body
with your desired URL - Click
Execute
In the Responses
section below, you will see the generated shortened URL. Paste the shortened URL in the address bar of your web browser to navigate to the target URL.
- Create a new
POST
request - Enter the request URL: http://localhost:8000/encode
- In the
Body
tab of theRequest
section, select theraw
radio button and thenJSON
instead ofText
from the dropdown menu - Enter a URL to be shortened in the
Body
in JSON format, e.g.
{
"url": "https://www.devtopics.com/best-programming-jokes/"
}
- Click
Send
In the Response
section below, under the Body
tab, you will see the shortened URL. Paste the shortened URL in your web browser to navigate to the target URL.
Note: If the URL you are trying to shorten is invalid (e.g. malformed), you will see an error message.
The easiest way is to copy the key
from the shortened URL
http://localhost:8000/{key}
and enter the following into your browser's address bar:
http://localhost:8000/decode/{key}
The target URL will appear in the browser window.
- Create a new
GET
request - Copy the
key
from the shortened URL (http://localhost:8000/{key}) - Enter the request URL: http://localhost:8000/decode/{key}
- Click
Send
In the Response
section below, under the Body
tab, you will see the original URL.
Note: If the key doesn't exist (e.g. typo) or has expired because the Uvicorn server was restarted, you will see an error message.
To run endpoint unit tests, navigate to the url-shortener-project
directory via the command line. In url-shortener-project
run
python test_endpoints.py