This project creates an asynchronous RESTful API built with Python, FastAPI, and Docker. It allows users to create, retrieve, update, and delete text summaries. The app integrates with a PostgreSQL database and is containerized for both local development and deployment.
Credit: This project is based on TestDriven.io's FastAPI TDD project.
Project Workflow Documentation
- Docker: Containerized application for local development and deployment.
- FastAPI: Provides asynchronous API endpoints.
- PostgreSQL: Used as the database for storing summaries.
- Tortoise ORM & Aerich: ORM for database models, and Aerich for handling migrations.
- Redis: Database store used for rate limiting.
- GitHub Actions CI: Automated testing, Docker builds, and pushing images to GitHub Packages.
- Heroku Deployment: FastAPI app and PostgreSQL running in Docker containers.
├── compose.yml # Docker compose configuration for local development
└── project
├── app/ # Fastapi app, routes, tortoise-orm & pydantic models, rate limiting
├── docker/ # Dockerfiles and ignore files for prod and dev environments
├── migrations/ # Database migration files
├── scripts/ # Shell scripts for building and deploying
└── tests/ # Unit and integration tests
-
Create a summary:
POST /summaries/
(Rate-limited to 5 requests per minute)curl -X POST "https://textsummarizer.app/summaries/" \ -H "Content-Type: application/json" \ -d '{"url": "https://realpython.com/pointers-in-python/"}' | jq
The payload also supports two additional optional parameters:
summarization_method
: Specifies the summarization algorithm. Supported values arelsa
,lex_rank
,text_rank
, andedmundson
. If not provided, the default islsa
.sentence_count
: Specifies the number of sentences in the generated summary, with a range of 5 to 30. If not provided, the default is 10.
Example request with all parameters:
curl -X POST "https://textsummarizer.app/summaries/" \ -H "Content-Type: application/json" \ -d '{"url": "https://realpython.com/pointers-in-python/", "summarization_method": "lex_rank", "sentence_count": 15}' | jq
For more information on the supported summarization algorithms, see the sumy documentation.
-
Get a summary:
GET /summaries/{id}/
(Rate-limited to 3 requests per minute)# Format the response using jq curl "https://textsummarizer.app/summaries/{id}/" | jq
-
Get all summaries:
GET /summaries/
(Rate-limited to 3 requests per minute)# Format the response using jq curl "https://textsummarizer.app/summaries/" | jq
-
Update a summary:
PUT /summaries/{id}/
curl -X PUT "https://textsummarizer.app/summaries/{id}/" \ -H "Content-Type: application/json" \ -d '{"url": "https://realpython.com/pointers-in-python/", "update_summary": "Updated summary text"}' | jq
-
Delete a summary:
DELETE /summaries/{id}/
curl -X DELETE "https://textsummarizer.app/summaries/{id}/" | jq
The application is deployed on Heroku using Docker. The scripts/
directory contains shell scripts for automating the deployment process:
-
build_and_push_image_github.sh
: Builds a Docker image using a specified Dockerfile and build context, then pushes it to GitHub Packages. It prompts for GitHub credentials and a target platform (amd64, arm64, or both). The image is tagged and pushed to the GitHub container registry (ghcr.io
). -
build_prod_image_heroku.sh
: Builds a production Docker image for a Heroku app using a specified Dockerfile and build context. It also allows platform selection and tags the image for Heroku's container registry. -
deploy_heroku.sh
: Automates deployment to Heroku. Logs into the Heroku container registry, provisions a PostgreSQL database if needed, pushes the Docker image, releases it, and applies database migrations using Aerich. This is only used for the very first deployment, as subsequent deployments are automated via the CI-CD pipeline. -
entrypoint.sh
: Waits for PostgreSQL and Redis to be ready before starting the FastAPI application. Used in the development container to ensure the app connects to the database only after it is fully initialized. This also ensure proper teardown of the database and the Redis instance. -
platform_selection.sh
: Prompts the user to select a target platform for Docker image builds:amd64
,arm64
, or both. Sourced by other build scripts to standardize platform selection. -
release.sh
: Releases the Docker image to Heroku by updating the app's formation via the Heroku API. Fetches the image ID and automates deployment. Used in CI pipelines for automated releases. -
run_black_isort.sh
: Runsblack
andisort
on the project files to format code and sort imports.