|
| 1 | +# Using this example |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +Clone the repository and install the dependencies. This project uses |
| 6 | +[Poetry][poetry]{:target="_blank"} for dependency management which should be |
| 7 | +installed on your system first. |
| 8 | + |
| 9 | +```console |
| 10 | +poetry install |
| 11 | +``` |
| 12 | + |
| 13 | +Then switch to the virtual environment: |
| 14 | + |
| 15 | +```console |
| 16 | +poetry shell |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +Run the server using `Uvicorn`: |
| 22 | + |
| 23 | +```console |
| 24 | +uvicorn main:app --reload |
| 25 | +``` |
| 26 | + |
| 27 | +> You can also run the server by just executing the `main.py` file: |
| 28 | +> |
| 29 | +> ```console |
| 30 | +> python main.py |
| 31 | +> ``` |
| 32 | +
|
| 33 | +Then open your browser at [http://localhost:8000](http://localhost:8000). |
| 34 | +
|
| 35 | +There is only one endpoint available: `/users`. It returns a list of all users |
| 36 | +for a `GET` request and creates a new user for a `POST` request. |
| 37 | +
|
| 38 | +### Local Postgres server using Docker |
| 39 | +
|
| 40 | +This example uses [PostgreSQL][postgres]{:target="_blank"} as the database. If |
| 41 | +you dont have a local PostgreSQL database running, you can start one with |
| 42 | +[Docker][docker]{:target="_blank"} using the following command: |
| 43 | +
|
| 44 | +```console |
| 45 | +docker run \ |
| 46 | + --rm \ |
| 47 | + --name postgres \ |
| 48 | + -p 5432:5432 \ |
| 49 | + -e POSTGRES_USER=postgres \ |
| 50 | + -e POSTGRES_PASSWORD=postgres \ |
| 51 | + -e POSTGRES_DB=postgres \ |
| 52 | + -d postgres |
| 53 | +``` |
| 54 | +
|
| 55 | +This will run a PostgreSQL database in a Docker container in the background. |
| 56 | +When you are finished and want to stop the database, run: |
| 57 | + |
| 58 | +```console |
| 59 | +docker stop postgres |
| 60 | +``` |
| 61 | + |
| 62 | +If needed, you can connect to the database managment by : |
| 63 | + |
| 64 | +```console |
| 65 | +docker exec -it postgres psql -U postgres |
| 66 | +``` |
| 67 | + |
| 68 | +This will allow you to edit or delete the database or records. |
| 69 | + |
| 70 | +### Use SQLite instead of PostgreSQL |
| 71 | + |
| 72 | +For testing purposes, you can also use [SQLite][sqlite]{:target="_blank"} |
| 73 | +instead of PostgreSQL. To do so, open the `db.py` file and comment out the |
| 74 | +PostgreSQL database in the `DATABASE_URL` environment variable and uncomment the |
| 75 | +SQLite database. |
| 76 | + |
| 77 | +```python |
| 78 | +# DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres" |
| 79 | +DATABASE_URL = "sqlite+aiosqlite:///./test.db" |
| 80 | +``` |
| 81 | + |
| 82 | +[poetry]: https://python-poetry.org/ |
| 83 | +[postgres]:https://www.postgresql.org/ |
| 84 | +[docker]:https://www.docker.com/ |
| 85 | +[sqlite]:https://www.sqlite.org/ |
0 commit comments