Skip to content

Using this example

Installation

Clone this repository and install the dependencies. This project uses Poetry for dependency management which should be installed on your system first.

Install the dependencies:

1
poetry install

Then switch to the virtual environment:

1
poetry shell

Usage

Run the server using Uvicorn:

1
uvicorn main:app --reload

Note

You can also run the server by just executing the main.py file:

1
python main.py

or using the included POE alias:

1
poe serve

Then open your browser at http://localhost:8000.

There is only one endpoint available: /users. It returns a list of all users for a GET request and creates a new user for a POST request.

Local Postgres server using Docker

This example uses PostgreSQL as the database. If you dont have a local PostgreSQL database running, you can start one with Docker using the following command:

1
2
3
4
5
6
7
8
docker run \
  --rm   \
  --name  postgres \
  -p 5432:5432 \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=postgres \
  -e POSTGRES_DB=postgres \
  -d postgres

This will run a PostgreSQL database in a Docker container in the background. When you are finished and want to stop the database, run:

1
docker stop postgres

If needed, you can connect to the database managment by :

1
docker exec -it postgres psql -U postgres

This will allow you to edit or delete the database or records.

Use SQLite instead of PostgreSQL

For testing purposes, you can also use SQLite instead of PostgreSQL. To do so, open the db.py file and comment out the PostgreSQL database in the DATABASE_URL environment variable and uncomment the SQLite database.

1
2
# DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres"
DATABASE_URL = "sqlite+aiosqlite:///./test.db"