You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### 2. Install dependecies with poetry or without it
71
+
```bash
61
72
cd project_name
62
-
# Poetry install (and activate environment!)
73
+
### Poetry install (python3.10)
63
74
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
65
86
docker-compose up -d
66
-
# Alembic migrations upgrade and initial_data.py script
87
+
88
+
### Alembic migrations upgrade and initial_data.py script
67
89
bash init.sh
68
-
# And this is it:
90
+
```
91
+
### 4. Now you can run app
92
+
```bash
93
+
### And this is it:
69
94
uvicorn app.main:app --reload
70
95
71
-
# Optionally - use git init to initialize git repository
72
96
```
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
+
73
99
74
-
####Running tests
100
+
### Running tests
75
101
76
102
```bash
77
103
# 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
355
381
`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/.
356
382
357
383
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 CORSfor, 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`
Prevents HTTP Host Headers attack, you shoud put here you server IPor (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"]`
0 commit comments