Skip to content

Commit 6a75069

Browse files
author
Rafał Safin
committed
Readme- Quickstart
Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>
1 parent 6e4209a commit 6a75069

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

README.md

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
- [Minimal async FastAPI + PostgreSQL template](#minimal-async-fastapi--postgresql-template)
1010
- [Features](#features)
1111
- [Quickstart](#quickstart)
12-
- [1. Install cookiecutter globally and cookiecutter this project](#1-install-cookiecutter-globally-and-cookiecutter-this-project)
13-
- [2. Install dependecies with poetry or without it](#2-install-dependecies-with-poetry-or-without-it)
14-
- [3. Setup databases](#3-setup-databases)
12+
- [1. Create repository from a template](#1-create-repository-from-a-template)
13+
- [2. Install dependecies with Poetry](#2-install-dependecies-with-poetry)
14+
- [3. Setup database and migrations](#3-setup-database-and-migrations)
1515
- [4. Now you can run app](#4-now-you-can-run-app)
1616
- [5. Activate pre-commit](#5-activate-pre-commit)
1717
- [6. Running tests](#6-running-tests)
@@ -22,64 +22,56 @@
2222
- [3. Create request and response schemas](#3-create-request-and-response-schemas)
2323
- [4. Create endpoints](#4-create-endpoints)
2424
- [5. Write tests](#5-write-tests)
25-
- [Deployment strategies - via Docker image](#deployment-strategies---via-docker-image)
26-
- [Docs URL, CORS and Allowed Hosts](#docs-url-cors-and-allowed-hosts)
25+
- [Design](#design)
26+
- [Deployment strategies - via Docker image](#deployment-strategies---via-docker-image)
27+
- [Docs URL, CORS and Allowed Hosts](#docs-url-cors-and-allowed-hosts)
28+
- [Test setup](#test-setup)
29+
2730

2831
## Features
2932

33+
- [x] Template repository
3034
- [x] SQLAlchemy 2.0, async queries, best possible autocompletion support
31-
- [x] Postgresql database under `asyncpg`
32-
- [x] [Alembic](https://alembic.sqlalchemy.org/en/latest/) migrations
33-
- [x] Very minimal project structure yet ready for quick start building new apps
35+
- [x] PostgreSQL 16 database under `asyncpg`, docker-compose.yml
36+
- [x] Full [Alembic](https://alembic.sqlalchemy.org/en/latest/) migrations setup
3437
- [x] Refresh token endpoint (not only access like in official template)
35-
- [x] Database in docker-compose.yml and ready to go Dockerfile with [uvicorn](https://www.uvicorn.org/) webserver
36-
- [x] [Poetry](https://python-poetry.org/docs/) and Python 3.12 based
37-
- [x] `pre-commit` hooks with [ruff](https://github.com/astral-sh/ruff)
38+
- [x] Ready to go Dockerfile with [uvicorn](https://www.uvicorn.org/) webserver as an example
39+
- [x] [Poetry](https://python-poetry.org/docs/), `mypy`, `pre-commit` hooks with [ruff](https://github.com/astral-sh/ruff)
3840
- [x] **Perfect** pytest asynchronous test setup with +40 tests and full coverage
3941

4042
<br>
4143

42-
_Check out also online example: https://minimal-fastapi-postgres-template.rafsaf.pl, it's 100% code used in template (docker image) with added domain and https only._
44+
_Check out also online example: https://minimal-fastapi-postgres-template.rafsaf.pl, it's 100% code used in template (docker image) with added my domain and https only._
4345

4446
<kbd>![template-fastapi-minimal-openapi-example](https://drive.google.com/uc?export=view&id=1rIXFJK8VyVrV7v4qgtPFryDd5FQrb4gr)</kbd>
4547

4648

4749

4850
## Quickstart
4951

50-
### 1. Install cookiecutter globally and cookiecutter this project
51-
52-
```bash
53-
pip install cookiecutter
52+
### 1. Create repository from a template
5453

55-
# And cookiecutter this project :)
56-
cookiecutter https://github.com/rafsaf/minimal-fastapi-postgres-template
54+
See [docs](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template).
5755

58-
```
59-
60-
### 2. Install dependecies with poetry or without it
56+
### 2. Install dependecies with [Poetry](https://python-poetry.org/docs/)
6157

6258
```bash
63-
cd project_name
59+
cd your_project_name
60+
6461
### Poetry install (python3.12)
6562
poetry install
66-
67-
### Optionally there is also `requirements-dev.txt` file
68-
python3.12 -m venv venv
69-
source venv/bin/activate
70-
pip install -r requirements-dev.txt
7163
```
7264

7365
Note, be sure to use `python3.12` with this template with either poetry or standard venv & pip, if you need to stick to some earlier python version, you should adapt it yourself (remove new versions specific syntax for example `str | int` for python < 3.10 or `tomllib` for python < 3.11)
7466

75-
### 3. Setup databases
67+
### 3. Setup database and migrations
7668

7769
```bash
78-
### Setup two databases
70+
### Setup database
7971
docker-compose up -d
8072

81-
### Alembic migrations upgrade and initial_data.py script
82-
bash init.sh
73+
### Run Alembic migrations
74+
alembic upgrade head
8375
```
8476

8577
### 4. Now you can run app
@@ -94,35 +86,27 @@ You should then use `git init` to initialize git repository and access OpenAPI s
9486

9587
### 5. Activate pre-commit
9688

97-
[pre-commit](https://pre-commit.com/) is de facto standard now for pre push activities like isort or black.
89+
[pre-commit](https://pre-commit.com/) is de facto standard now for pre push activities like isort or black or its replacement ruff.
9890

99-
Refer to `.pre-commit-config.yaml` file to see my opinionated choices.
91+
Refer to `.pre-commit-config.yaml` file to see my current opinionated choices.
10092

10193
```bash
10294
# Install pre-commit
103-
pre-commit install
95+
pre-commit install --install-hooks
10496

105-
# First initialization and run on all files
97+
# Run on all files
10698
pre-commit run --all-files
10799
```
108100

109101
### 6. Running tests
110102

103+
Note, it will create databases during and run tests in many processes by default, based on how many CPU are available.
104+
105+
For more details, see [Test setup](#test-setup).
106+
111107
```bash
112-
# Note, it will use second database declared in docker-compose.yml, not default one
108+
# see pytest configuration flags in pyproject.toml
113109
pytest
114-
115-
# collected 7 items
116-
117-
# app/tests/test_auth.py::test_auth_access_token PASSED [ 14%]
118-
# app/tests/test_auth.py::test_auth_access_token_fail_no_user PASSED [ 28%]
119-
# app/tests/test_auth.py::test_auth_refresh_token PASSED [ 42%]
120-
# app/tests/test_users.py::test_read_current_user PASSED [ 57%]
121-
# app/tests/test_users.py::test_delete_current_user PASSED [ 71%]
122-
# app/tests/test_users.py::test_reset_current_user_password PASSED [ 85%]
123-
# app/tests/test_users.py::test_register_new_user PASSED [100%]
124-
#
125-
# ======================================================== 7 passed in 1.75s ========================================================
126110
```
127111

128112
<br>
@@ -368,13 +352,15 @@ async def test_get_all_my_pets(
368352

369353
```
370354

371-
## Deployment strategies - via Docker image
355+
## Design
356+
357+
### Deployment strategies - via Docker image
372358

373359
This template has by default included `Dockerfile` with [Uvicorn](https://www.uvicorn.org/) webserver, because it's simple and just for showcase purposes, with direct relation to FastAPI and great ease of configuration. You should be able to run container(s) (over :8000 port) and then need to setup the proxy, loadbalancer, with https enbaled, so the app stays behind it.
374360

375361
If you prefer other webservers for FastAPI, check out [Nginx Unit](https://unit.nginx.org/), [Daphne](https://github.com/django/daphne), [Hypercorn](https://pgjones.gitlab.io/hypercorn/index.html).
376362

377-
## Docs URL, CORS and Allowed Hosts
363+
### Docs URL, CORS and Allowed Hosts
378364

379365
There are some **opinionated** default settings in `/app/main.py` for documentation, CORS and allowed hosts.
380366

@@ -413,3 +399,5 @@ There are some **opinionated** default settings in `/app/main.py` for documentat
413399
```
414400

415401
Prevents HTTP Host Headers attack, you shoud put here you server IP or (preferably) full domain under it's accessible like `example.com`. By default in .env there are two most popular records: `ALLOWED_HOSTS=["localhost", "127.0.0.1"]`
402+
403+
### Test setup

0 commit comments

Comments
 (0)