This is a URL shortener application built with FastAPI and DynamoDB.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Java Runtime Environment (JRE) for running DynamoDB Local
- AWS account, for non-local Database Connections
- Python 3.6 or later
- FastAPI
- Uvicorn, an ASGI server to serve your application
- PynamoDB, a Pythonic interface for Amazon DynamoDB
- Pytest, for running tests
- Docker, for containerization
- Optional: Postman, for endpoint testing
You can install the Python dependencies by running:
pip install fastapi uvicorn pynamodb pytest
-
Start Local Database Connection
Navigate to the dynamodb_local_latest directory in your Documents folder and run the following command to start DynamoDB Local:
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
-
Start the Server
In a separate terminal, run the following command within the url_shortener/app directory to start the Uvicorn server:
uvicorn main:app --reload
-
Build Docker Image
If not already built, build the docker image by navigating to app directory within project and running:
docker build . -t url_project
-
Run Docker Container
After building your image, you can now run your Docker conainer by running:
docker run -e AWS_ACCESS_KEY_ID=your_access_key_id -e AWS_SECRET_ACCESS_KEY=your_secret_key --rm -it -p 80:80/tcp url_project:latest
Now, you can navigate to http://0.0.0.0:80/ in your web browser to see the application in action. Postman can be used to utilize endpoints.
You can run the tests for the application by navigating to url_shortener/app directory and running:
pytest url_tests.py -vv -s
Typer was used to build out the CLI for this project. Below are the available commands to use the app.
To display a list of all shortened URLs:
python3 cli.py list-urls
To shorten a url, a long url must always be provided. The short url is optional - if omitting a short url, just omit the 'short url' altogether from the command.
python3 cli.py shorten 'long url' 'short url'
To retrieve the long URL associated with a short URL:
python3 cli.py lookup 'short url'
To delete a short URL and its associated long URL:
python3 cli.py delete 'short url'
Ensure that the Docker container is running before using the CLI commands.
This project is licensed under the MIT License - see the LICENSE.md file for details.