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.9
- FastAPI
- Uvicorn, an ASGI server to serve your application
- PynamoDB, a Pythonic interface for Amazon DynamoDB
- Pytest, for running tests
- Moto, for mocking DB connection for testing
- 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.
1.Authenticate to AWS via cli:
bash aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-number>.dkr.ecr.<region>.amazonaws.com
-
Build the image:
docker build . -t url_project -
Tag the image:
docker tag url_project:latest <aws-account-number>.dkr.ecr.us-east-2.amazonaws.com/<ecr-name>:latest
-
Push the image:
docker push <aws-account-number>.dkr.ecr.us-east-2.amazonaws.com/<ecr-name>:latest
-
Update App Runner with new image:
aws apprunner update-service \ --service-arn <apprunner-service-arn> \ --source-configuration '{ "ImageRepository": { "ImageIdentifier": "<aws-account-number>.dkr.ecr.us-east-2.amazonaws.com/<ecr-name>>:latest", "ImageRepositoryType": "ECR", "ImageConfiguration": { "Port": "80" } } }' \ --region us-east-2
You can run the tests for the application by navigating to url_shortener/app directory and running:
pytest url_tests.py -vv -sTests are also automatically ran via Github Workflows every time a commit is run.
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-urlsTo 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.