A simple FastAPI app that shortens a URL and redirects browser to the original URL.
Demo available at https://ay-us.herokuapp.com/
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
- 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
- The default is
MONGODB_URI
is your connection string to MongoDB for real database. Get this from your MongoDB dashboardMONGODB_URI_TESTS
is your connection string to MongoDB for test database. Get this from your MongoDB dashboardRNG_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
- Activate virtual environment and execute
uvicorn app.main:app
- Add
--reload
to make the server restart after code changes [Only use for development]
- Add
- Open your
BASE_URL
in a browser to view the home page
- Execute
pytest
in terminal
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"
}
Check the original URL if it's already in database, and return the existing short URL instead of creating a new one- Will reduce duplicate entries
- Added in commit 2c7ff7c
- Remove unvisited link after a certain length of time has elapsed
- Will reduce size of database
- Less hoarding of custom names
- Use Docker to containerize this app
- Easier to deploy
- A page to show statistics
- Top 10 URLs, etc
Prettier home page with mobile version- Added in commit 6022791
- Blog post by Similoluwa
Okunowo
- The main inspiration for this project. I reused many of their codes and added my own flavor where I see fit
- FastAPI documentation
- Bootstrap documentation