- The project can be used as an example to learn how to build a web API using FastAPI and PostgreSQL.
- To learn how to write unit and integration tests for a web API.
- This project has 100% unit and 90% integration test coverage. (does that mean it is bug-free?)
- As an Exploratory tester, understand what is covered in automated checks and do exploratory tests.
- Python 3.8
- FastAPI
- Pytest
- SQLAlchemy
- Poetry
- Pydantic
- PostgreSQL
- Docker
- Docker Compose
- Nginx
- JS
- nodejs
- Babel
- graphql-yoga
- babel-plugin-transform-object-rest-spread
- graphql-subscriptions
The app contains the following endpoints:
The easiest way to run the project is to use docker-compose. To do so, you will need to have docker and docker-compose installed on your machine.
- Clone the project's main branch.
- Create a
.env.docker
file in the/app/secrets
directory of the project and add the variables looking at the.env.example
file. - Ensure to set
POSTGRES_HOST=postgres
. - Run the following to execute permissions on the setup-env.sh file:
chmod +x setup-env.sh
- Run the following command to build the project:
docker-compose build
- Run the following command to run the project:
docker-compose --env-file app/secrets/.env.docker up
- Visit http://localhost/docs to view the (FastAPI auto-generated) OpenAPI documentation
The docker-compose.yml file contains the following services:
- app: This is the main service that runs the FastAPI application
- postgres: This is the database service
- Nginx: This is the web server service
Nginx acts as a reverse proxy to the FastAPI application. This allows the application to be served on port 80 and create a JS-based graphql endpoint on the same port. (coming soon...)
Unlike Docker, local setup requires more work to get the project running.
- Clone the project's main branch.
- Install Poetry on your machine. Project dependencies are managed using Poetry.( https://python-poetry.org/docs/#installation )
- Run the following command to install the project dependencies:
poetry install
- Create a
.env.local
file in the/app/secrets
directory of the project and add the variables looking at the.env.example
file. Ensure to setPOSTGRES_HOST=localhost
. - Set
MY_ENVIRONMENT=local
bash export MY_ENVIRONMENT=local
- The CRUD app needs a PostgreSQL database to run. You can install PostgreSQL on your machine or use docker to run a PostgreSQL container.
- The app needs a username and password to connect to the database.
- The app creates the database on startup. Ensure to set the database name in the
.env.local
file. - Create a user with the name specified in the
.env.local
file. - The app looks for a username and password in the
.env.local
file. Please set the password for the user you created in step 2. - Look at Makefile to see the commands to run to tests and run the project.
- Run the following command to run the project:
cd app && poetry run python main.py
Unlike the Docker setup, the local setup steps were not tested. You may need to do some debugging to get the project running.
The project's lightweight no database version is available at simple-crud repo. This app uses a JSON to store data.
- Admin endpoints to manage users, roles and data
- Head, Options, Patch and Trace endpoints
- JS GraphQL endpoint
- JS GraphQL endpoint tests