Skip to content

Commit 1cce9d3

Browse files
authored
Merge pull request #19 from rafsaf/release-3-1
Release 3.1, updated readme and dependencies. Fixed issue with allowed hosts.
2 parents e820c9b + 235ce93 commit 1cce9d3

File tree

10 files changed

+461
-415
lines changed

10 files changed

+461
-415
lines changed

README.md

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@
1616

1717
# Minimal async FastAPI + PostgreSQL template
1818

19-
- [Feauters](#features)
20-
- [Quickstart](#quickstart)
21-
- [About](#about)
22-
- [Step by step example - POST and GET endpoints](#step-by-step-example---post-and-get-endpoints)
23-
- [1. Create SQLAlchemy model](#1-create-sqlalchemy-model)
24-
- [2. Create and apply alembic migration](#2-create-and-apply-alembic-migration)
25-
- [3. Create request and response schemas](#3-create-request-and-response-schemas)
26-
- [4. Create endpoint](#4-create-endpoints)
27-
- [5. Write tests](#5-write-tests)
28-
- [Deployment strategies - via Docker image](#deployment-strategies---via-docker-image)
19+
- [Minimal async FastAPI + PostgreSQL template](#minimal-async-fastapi--postgresql-template)
20+
- [Features](#features)
21+
- [Quickstart](#quickstart)
22+
- [1. Install cookiecutter globally and cookiecutter this project](#1-install-cookiecutter-globally-and-cookiecutter-this-project)
23+
- [2. Install dependecies with poetry or without it](#2-install-dependecies-with-poetry-or-without-it)
24+
- [3. Setup databases](#3-setup-databases)
25+
- [4. Now you can run app](#4-now-you-can-run-app)
26+
- [Running tests](#running-tests)
27+
- [About](#about)
28+
- [Step by step example - POST and GET endpoints](#step-by-step-example---post-and-get-endpoints)
29+
- [1. Create SQLAlchemy model](#1-create-sqlalchemy-model)
30+
- [2. Create and apply alembic migration](#2-create-and-apply-alembic-migration)
31+
- [3. Create request and response schemas](#3-create-request-and-response-schemas)
32+
- [4. Create endpoints](#4-create-endpoints)
33+
- [5. Write tests](#5-write-tests)
34+
- [Deployment strategies - via Docker image](#deployment-strategies---via-docker-image)
35+
- [Docs URL, CORS and Allowed Hosts](#docs-url-cors-and-allowed-hosts)
2936

3037
## Features
3138

@@ -47,8 +54,9 @@ _Check out also online example: https://minimal-fastapi-postgres-template.rafsaf
4754

4855
## Quickstart
4956

57+
58+
### 1. Install cookiecutter globally and cookiecutter this project
5059
```bash
51-
# Install cookiecutter globally
5260
pip install cookiecutter
5361

5462
# And cookiecutter this project :)
@@ -57,21 +65,39 @@ cookiecutter https://github.com/rafsaf/minimal-fastapi-postgres-template
5765
# if you want experimental fastapi-users template
5866
# check "experimental_fastapi_users_template"
5967
# to True in cookiecutter option
68+
```
6069

70+
### 2. Install dependecies with poetry or without it
71+
```bash
6172
cd project_name
62-
# Poetry install (and activate environment!)
73+
### Poetry install (python3.10)
6374
poetry install
64-
# Setup two databases
75+
76+
### Optionally there are also requirements
77+
python3.10 -m venv venv
78+
source venv/bin/activate
79+
pip install -r requirements-dev.txt
80+
```
81+
Note, be sure to use `python3.10` 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 python3.10+ specific syntax for example `str | int`)
82+
83+
### 3. Setup databases
84+
```bash
85+
### Setup two databases
6586
docker-compose up -d
66-
# Alembic migrations upgrade and initial_data.py script
87+
88+
### Alembic migrations upgrade and initial_data.py script
6789
bash init.sh
68-
# And this is it:
90+
```
91+
### 4. Now you can run app
92+
```bash
93+
### And this is it:
6994
uvicorn app.main:app --reload
7095

71-
# Optionally - use git init to initialize git repository
7296
```
97+
You should then use `git init` to initialize git repository and access OpenAPI spec at http://localhost:8000/ by default. To customize docs url, cors and allowed hosts settings, read section about it.
98+
7399

74-
#### Running tests
100+
### Running tests
75101

76102
```bash
77103
# Note, it will use second database declared in docker-compose.yml, not default one
@@ -355,3 +381,43 @@ This template has by default included `Dockerfile` with [Nginx Unit](https://uni
355381
`nginx-unit-config.json` file included in main folder has some default configuration options, runs app in single process and thread. More info about config file here https://unit.nginx.org/configuration/#python and about also read howto for FastAPI: https://unit.nginx.org/howto/fastapi/.
356382

357383
If you prefer other webservers for FastAPI, check out [Daphne](https://github.com/django/daphne), [Hypercorn](https://pgjones.gitlab.io/hypercorn/index.html) or [Uvicorn](https://www.uvicorn.org/).
384+
385+
## Docs URL, CORS and Allowed Hosts
386+
387+
There are some **opinionated** default settings in `/app/main.py` for documentation, CORS and allowed hosts.
388+
389+
1. Docs
390+
391+
```python
392+
app = FastAPI(
393+
title=config.settings.PROJECT_NAME,
394+
version=config.settings.VERSION,
395+
description=config.settings.DESCRIPTION,
396+
openapi_url="/openapi.json",
397+
docs_url="/",
398+
)
399+
```
400+
Docs page is simpy `/` (by default in FastAPI it is `/docs`). Title, version and description are taken directly from `config` and then directly from `pyproject.toml` file. You can change it completely for the project, remove or use environment variables `PROJECT_NAME`, `VERSION`, `DESCRIPTION`.
401+
402+
2. CORS
403+
404+
```python
405+
app.add_middleware(
406+
CORSMiddleware,
407+
allow_origins=[str(origin) for origin in config.settings.BACKEND_CORS_ORIGINS],
408+
allow_credentials=True,
409+
allow_methods=["*"],
410+
allow_headers=["*"],
411+
)
412+
```
413+
414+
If you are not sure what are CORS for, follow https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS. React and most frontend frameworks nowadays operate on `localhost:3000` thats why it's included in `BACKEND_CORS_ORIGINS` in .env file, before going production be sure to include and frontend domain here, like `my-fontend-app.example.com`
415+
416+
3. Allowed Hosts
417+
418+
```python
419+
app.add_middleware(TrustedHostMiddleware, allowed_hosts=config.settings.ALLOWED_HOSTS)
420+
```
421+
422+
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"]`
423+

{{cookiecutter.project_name}}/template_minimal/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ENVIRONMENT=DEV
33
ACCESS_TOKEN_EXPIRE_MINUTES=11520
44
REFRESH_TOKEN_EXPIRE_MINUTES=40320
55
BACKEND_CORS_ORIGINS=["http://localhost:3000","http://localhost:8001"]
6-
ALLOWED_HOSTS=["localhost"]
6+
ALLOWED_HOSTS=["localhost", "127.0.0.1"]
77

88
DEFAULT_DATABASE_HOSTNAME=localhost
99
DEFAULT_DATABASE_USER=rDGJeEDqAz

{{cookiecutter.project_name}}/template_minimal/.env.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ENVIRONMENT=DEV
33
ACCESS_TOKEN_EXPIRE_MINUTES=11520
44
REFRESH_TOKEN_EXPIRE_MINUTES=40320
55
BACKEND_CORS_ORIGINS=["http://localhost:3000","http://localhost:8001"]
6-
ALLOWED_HOSTS=["localhost"]
6+
ALLOWED_HOSTS=["localhost", "127.0.0.1"]
77

88
DEFAULT_DATABASE_HOSTNAME=localhost
99
DEFAULT_DATABASE_USER=postgres

{{cookiecutter.project_name}}/template_minimal/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# See https://unit.nginx.org/installation/#docker-images
22

3-
FROM nginx/unit:1.26.1-python3.10
3+
FROM nginx/unit:1.28.0-python3.10
44

55
ENV PYTHONUNBUFFERED 1
66

{{cookiecutter.project_name}}/template_minimal/app/core/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Settings(BaseSettings):
3939
ACCESS_TOKEN_EXPIRE_MINUTES: int = 11520 # 8 days
4040
REFRESH_TOKEN_EXPIRE_MINUTES: int = 40320 # 28 days
4141
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] = []
42-
ALLOWED_HOSTS: list[str] = ["localhost"]
42+
ALLOWED_HOSTS: list[str] = ["localhost", "127.0.0.1"]
4343

4444
# PROJECT NAME, VERSION AND DESCRIPTION
4545
PROJECT_NAME: str = PYPROJECT_CONTENT["name"]

{{cookiecutter.project_name}}/template_minimal/docker-compose.dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ services:
2828
- .env
2929
environment:
3030
- DEFAULT_DATABASE_HOSTNAME=postgres
31+
- DEFAULT_DATABASE_PORT=5432
3132
ports:
3233
- 80:80
3334

0 commit comments

Comments
 (0)