Fastapi application to show typical Create, Read, Update, Delete (CRUD) operations. Features include async database, migrations, documentation, automated unit tests, CI/CD with GitHub Actions, authentication and pagination.
- Python 3.11
- Fastapi
- Postgres database
- Pytest
- Docker
git clone https://github.com/taiyeoguns/fastapi-trial.git
With a virtualenv already set-up, install the requirements with pip:
make install
Create .env
file from example file and maintain necessary details in it.
cp .env.example .env
SQLAlchemy is used to model the data and alembic is used to keep the database up-to-date with migrations.
To setup a local database, fill in database details in .env
file from earlier or set up environment variables. Ensure the database and user defined in .env
is already created in Postgres.
For initial database setup, run the following commands:
make db-upgrade
Subsequently, after making any changes to the database models, run the following commands:
make db-migrate
make db-upgrade
To seed the database with initial data, run:
make seed
Activate the virtual environment and start the application by running:
make run
To authenticate API requests, pass a valid API key as a header with key X-Api-Key
.
In command prompt, run:
make test
Ensure database details are added to .env
file from earlier.
The following environment variables should be set in the .env
file even if they do not 'exist', the docker postgres image will use them for setting up the container -
POSTGRES_USER
, POSTGRES_PASSWORD
, POSTGRES_DB
With Docker and Docker Compose set up, run:
make docker-run
Thereafter, application should be available at http://localhost:8000
OpenAPI documentation should be available at http://localhost:8000/docs