Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 127.0.0.1 to ALLOWED HOSTS and update readme #21

Merged
merged 3 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [4. Create endpoints](#4-create-endpoints)
- [5. Write tests](#5-write-tests)
- [Deployment strategies - via Docker image](#deployment-strategies---via-docker-image)
- [Docs URL, CORS and Allowed Hosts](#docs-url-cors-and-allowed-hosts)

## Features

Expand Down Expand Up @@ -92,8 +93,9 @@ bash init.sh
### And this is it:
uvicorn app.main:app --reload

# Then probably - use git init to initialize git repository
```
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.


### Running tests

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

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/).

## Docs URL, CORS and Allowed Hosts

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

1. Docs

```python
app = FastAPI(
title=config.settings.PROJECT_NAME,
version=config.settings.VERSION,
description=config.settings.DESCRIPTION,
openapi_url="/openapi.json",
docs_url="/",
)
```
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`.

2. CORS

```python
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in config.settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
```

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`

3. Allowed Hosts

```python
app.add_middleware(TrustedHostMiddleware, allowed_hosts=config.settings.ALLOWED_HOSTS)
```

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"]`

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ENVIRONMENT=DEV
ACCESS_TOKEN_EXPIRE_MINUTES=11520
REFRESH_TOKEN_EXPIRE_MINUTES=40320
BACKEND_CORS_ORIGINS=["http://localhost:3000","http://localhost:8001"]
ALLOWED_HOSTS=["localhost"]
ALLOWED_HOSTS=["localhost", "127.0.0.1"]

DEFAULT_DATABASE_HOSTNAME=localhost
DEFAULT_DATABASE_USER=rDGJeEDqAz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ENVIRONMENT=DEV
ACCESS_TOKEN_EXPIRE_MINUTES=11520
REFRESH_TOKEN_EXPIRE_MINUTES=40320
BACKEND_CORS_ORIGINS=["http://localhost:3000","http://localhost:8001"]
ALLOWED_HOSTS=["localhost"]
ALLOWED_HOSTS=["localhost", "127.0.0.1"]

DEFAULT_DATABASE_HOSTNAME=localhost
DEFAULT_DATABASE_USER=postgres
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_name}}/template_minimal/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# See https://unit.nginx.org/installation/#docker-images

FROM nginx/unit:1.26.1-python3.10
FROM nginx/unit:1.28.0-python3.10

ENV PYTHONUNBUFFERED 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Settings(BaseSettings):
ACCESS_TOKEN_EXPIRE_MINUTES: int = 11520 # 8 days
REFRESH_TOKEN_EXPIRE_MINUTES: int = 40320 # 28 days
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] = []
ALLOWED_HOSTS: list[str] = ["localhost"]
ALLOWED_HOSTS: list[str] = ["localhost", "127.0.0.1"]

# PROJECT NAME, VERSION AND DESCRIPTION
PROJECT_NAME: str = PYPROJECT_CONTENT["name"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
- .env
environment:
- DEFAULT_DATABASE_HOSTNAME=postgres
- DEFAULT_DATABASE_PORT=5432
ports:
- 80:80

Expand Down