Skip to content

Commit 972f57c

Browse files
Merge branch 'fastapi:master' into refactor-status-code
2 parents 18ed7a1 + d87b639 commit 972f57c

File tree

15 files changed

+267
-133
lines changed

15 files changed

+267
-133
lines changed

.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Domain
22
# This would be set to the production domain with an env var on deployment
3+
# used by Traefik to transmit traffic and aqcuire TLS certificates
34
DOMAIN=localhost
5+
# To test the local Traefik config
6+
# DOMAIN=localhost.tiangolo.com
7+
8+
# Used by the backend to generate links in emails to the frontend
9+
FRONTEND_HOST=http://localhost:5173
10+
# In staging and production, set this env var to the frontend host, e.g.
11+
# FRONTEND_HOST=https://dashboard.example.com
412

513
# Environment: local, staging, production
614
ENVIRONMENT=local

.github/workflows/playwright.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
working-directory: frontend
4242
- run: docker compose build
4343
- run: docker compose down -v --remove-orphans
44-
- run: docker compose up -d --wait
44+
- run: docker compose up -d --wait backend mailcatcher
4545
- name: Run Playwright tests
4646
run: npx playwright test --fail-on-flaky-tests --trace=retain-on-failure
4747
working-directory: frontend

backend/README.md

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,11 @@
55
* [Docker](https://www.docker.com/).
66
* [Poetry](https://python-poetry.org/) for Python package and environment management.
77

8-
## Local Development
8+
## Docker Compose
99

10-
* Start the stack with Docker Compose:
10+
Start the local development environment with Docker Compose following the guide in [../development.md](../development.md).
1111

12-
```bash
13-
docker compose up -d
14-
```
15-
16-
* Now you can open your browser and interact with these URLs:
17-
18-
Frontend, built with Docker, with routes handled based on the path: http://localhost
19-
20-
Backend, JSON based web API based on OpenAPI: http://localhost/api/
21-
22-
Automatic interactive documentation with Swagger UI (from the OpenAPI backend): http://localhost/docs
23-
24-
Adminer, database web administration: http://localhost:8080
25-
26-
Traefik UI, to see how the routes are being handled by the proxy: http://localhost:8090
27-
28-
**Note**: The first time you start your stack, it might take a minute for it to be ready. While the backend waits for the database to be ready and configures everything. You can check the logs to monitor it.
29-
30-
To check the logs, run:
31-
32-
```bash
33-
docker compose logs
34-
```
35-
36-
To check the logs of a specific service, add the name of the service, e.g.:
37-
38-
```bash
39-
docker compose logs backend
40-
```
41-
42-
If your Docker is not running in `localhost` (the URLs above wouldn't work) you would need to use the IP or domain where your Docker is running.
43-
44-
## Backend local development, additional details
45-
46-
### General workflow
12+
## General Workflow
4713

4814
By default, the dependencies are managed with [Poetry](https://python-poetry.org/), go there and install it.
4915

@@ -63,13 +29,13 @@ Make sure your editor is using the correct Python virtual environment.
6329

6430
Modify or add SQLModel models for data and SQL tables in `./backend/app/models.py`, API endpoints in `./backend/app/api/`, CRUD (Create, Read, Update, Delete) utils in `./backend/app/crud.py`.
6531

66-
### VS Code
32+
## VS Code
6733

6834
There are already configurations in place to run the backend through the VS Code debugger, so that you can use breakpoints, pause and explore variables, etc.
6935

7036
The setup is also already configured so you can run the tests through the VS Code Python tests tab.
7137

72-
### Docker Compose Override
38+
## Docker Compose Override
7339

7440
During development, you can change Docker Compose settings that will only affect the local development environment in the file `docker-compose.override.yml`.
7541

@@ -123,7 +89,7 @@ Nevertheless, if it doesn't detect a change but a syntax error, it will just sto
12389

12490
...this previous detail is what makes it useful to have the container alive doing nothing and then, in a Bash session, make it run the live reload server.
12591

126-
### Backend tests
92+
## Backend tests
12793

12894
To test the backend run:
12995

@@ -135,7 +101,7 @@ The tests run with Pytest, modify and add tests to `./backend/app/tests/`.
135101

136102
If you use GitHub Actions the tests will run automatically.
137103

138-
#### Test running stack
104+
### Test running stack
139105

140106
If your stack is already up and you just want to run the tests, you can use:
141107

@@ -151,11 +117,11 @@ For example, to stop on first error:
151117
docker compose exec backend bash /app/tests-start.sh -x
152118
```
153119

154-
#### Test Coverage
120+
### Test Coverage
155121

156122
When the tests are run, a file `htmlcov/index.html` is generated, you can open it in your browser to see the coverage of the tests.
157123

158-
### Migrations
124+
## Migrations
159125

160126
As during local development your app directory is mounted as a volume inside the container, you can also run the migrations with `alembic` commands inside the container and the migration code will be in your app directory (instead of being only inside the container). So you can add it to your git repository.
161127

backend/app/core/config.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,20 @@ class Settings(BaseSettings):
3131
SECRET_KEY: str = secrets.token_urlsafe(32)
3232
# 60 minutes * 24 hours * 8 days = 8 days
3333
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
34-
DOMAIN: str = "localhost"
34+
FRONTEND_HOST: str = "http://localhost:5173"
3535
ENVIRONMENT: Literal["local", "staging", "production"] = "local"
3636

37-
@computed_field # type: ignore[prop-decorator]
38-
@property
39-
def server_host(self) -> str:
40-
# Use HTTPS for anything other than local development
41-
if self.ENVIRONMENT == "local":
42-
return f"http://{self.DOMAIN}"
43-
return f"https://{self.DOMAIN}"
44-
4537
BACKEND_CORS_ORIGINS: Annotated[
4638
list[AnyUrl] | str, BeforeValidator(parse_cors)
4739
] = []
4840

41+
@computed_field # type: ignore[prop-decorator]
42+
@property
43+
def all_cors_origins(self) -> list[str]:
44+
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
45+
self.FRONTEND_HOST
46+
]
47+
4948
PROJECT_NAME: str
5049
SENTRY_DSN: HttpUrl | None = None
5150
POSTGRES_SERVER: str

backend/app/main.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ def custom_generate_unique_id(route: APIRoute) -> str:
2121
)
2222

2323
# Set all CORS enabled origins
24-
if settings.BACKEND_CORS_ORIGINS:
24+
if settings.all_cors_origins:
2525
app.add_middleware(
2626
CORSMiddleware,
27-
allow_origins=[
28-
str(origin).strip("/") for origin in settings.BACKEND_CORS_ORIGINS
29-
],
27+
allow_origins=settings.all_cors_origins,
3028
allow_credentials=True,
3129
allow_methods=["*"],
3230
allow_headers=["*"],

backend/app/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def generate_test_email(email_to: str) -> EmailData:
6464
def generate_reset_password_email(email_to: str, email: str, token: str) -> EmailData:
6565
project_name = settings.PROJECT_NAME
6666
subject = f"{project_name} - Password recovery for user {email}"
67-
link = f"{settings.server_host}/reset-password?token={token}"
67+
link = f"{settings.FRONTEND_HOST}/reset-password?token={token}"
6868
html_content = render_email_template(
6969
template_name="reset_password.html",
7070
context={
@@ -90,7 +90,7 @@ def generate_new_account_email(
9090
"username": username,
9191
"password": password,
9292
"email": email_to,
93-
"link": settings.server_host,
93+
"link": settings.FRONTEND_HOST,
9494
},
9595
)
9696
return EmailData(html_content=html_content, subject=subject)

backend/poetry.lock

Lines changed: 129 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)