This application is a simple, lightweight Book Management API built with FastAPI and SQLAlchemy. It offers CRUD operations to manage a collection of books. Each book has a unique id, a title, an author, and a description. It is designed with a focus on simplicity, flexibility, and performance, and is ready for containerization with Docker.
- 📦 This app has been refactored for containerization, promoting consistent operating environments and seamless deployments using Docker.
- 🚀 This app uses the FastAPI framework. FastAPI is a high-performance web framework for building APIs with Python 3.6+ that provides built-in support for asynchronous code. It is highly extensible and compatible with REST, OpenAPI, GraphQL, gRPC, and other protocols.
- ✅ This app uses Pydantic data validation. Pydantic provides a declarative way to define data schemas and models that can be used across different protocols and API styles. The use of Pydantic models also allows FastAPI to automatically generate API documentation, perform data validation, and serialize/deserialize request and response data, making it easier to build and maintain high-performance APIs.
- 💾 This app leverages SQLAlchemy for data persistence. A comprehensive ORM implementation is included to automate the interaction with the PostgreSQL database. The SQLAlchemy ORM provides a high-level, Pythonic interface to the database, while still allowing for complex, lower-level SQL constructs. This integration of SQLAlchemy with our Docker containerization process ensures the app is fully equipped to handle complex data structures and queries, making it a powerful, ready-to-run, fully containerized app.
- 🗃️ This app leverages PostgreSQL for data persistence. A comprehensive initialization script is included to automate the setup of the PostgreSQL database. The script creates a database user, database, and the database table. This integration of the script with our Docker containerization process means the app is fully prepared for container-based deployment, complete with its database setup, epitomizing a ready-to-run, fully containerized app.
- Clone the repository.
git clone https://github.com/aws-samples/python-fastapi-demo-docker.git
- Navigate into the project directory and make a copy of the example environment variables file.
cd python-fastapi-demo-docker
cp .env.example .env
The .env file contains the database credentials. You can modify these credentials if necessary.
These steps show how to set up the database and the application using Docker.
- In the project root directory, build the Docker images.
docker-compose build
- Run the Docker containers.
docker-compose up
At this point, the application should be running at http://localhost:8000/. To stop the application, you can run:
docker-compose down
- To restart or rebuild the application, you can run:
docker-compose up --build
FastAPI autogenerates an OpenAPI specification, which allows you to test this application directly from an interactive console in your browser. It uses the Pydantic model to validate user input (as shown in the models section of the specification, below). Go to http://0.0.0.0:8000/docs to use the automatic interactive API documentation for this application (provided by Swagger UI) to send requests.
The root endpoint that returns a welcome greeting.
This endpoint displays the form for creating a new book.
This endpoint creates a new book with the provided title, author, and description. On success, it redirects to the /books endpoint.
This endpoint lists all the books in the collection.
This endpoint displays the details of a single book specified by the book_id.
This endpoint displays a form for updating the details of a single book specified by the book_id.
This endpoint accepts form data and updates the details of the specified book. On success, it redirects to the book detail view of the updated book.
This endpoint deletes the specified book from the collection.
See CONTRIBUTING for more information.
This project is licensed under the MIT-0 License. See the LICENSE file.