Skip to content

Commit 235ce93

Browse files
authored
Merge pull request #21 from rafsaf/docs-display-cors-allowed-hosts
Add 127.0.0.1 to ALLOWED HOSTS and update readme
2 parents e432812 + fca8ccc commit 235ce93

File tree

6 files changed

+48
-5
lines changed

6 files changed

+48
-5
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [4. Create endpoints](#4-create-endpoints)
3333
- [5. Write tests](#5-write-tests)
3434
- [Deployment strategies - via Docker image](#deployment-strategies---via-docker-image)
35+
- [Docs URL, CORS and Allowed Hosts](#docs-url-cors-and-allowed-hosts)
3536

3637
## Features
3738

@@ -92,8 +93,9 @@ bash init.sh
9293
### And this is it:
9394
uvicorn app.main:app --reload
9495

95-
# Then probably - use git init to initialize git repository
9696
```
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+
9799

98100
### Running tests
99101

@@ -379,3 +381,43 @@ This template has by default included `Dockerfile` with [Nginx Unit](https://uni
379381
`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/.
380382

381383
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)