Skip to content

Commit a9859e4

Browse files
committed
Updating instructions for backend_async
1 parent 210b0b6 commit a9859e4

File tree

1 file changed

+18
-129
lines changed

1 file changed

+18
-129
lines changed

backend_async/README.md

Lines changed: 18 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
# FastAPI Project - Backend
1+
# FastAPI Project - Backend Async
22

33
## Requirements
44

55
* [Docker](https://www.docker.com/).
66
* [uv](https://docs.astral.sh/uv/) for Python package and environment management.
77

8-
## Docker Compose
9-
10-
Start the local development environment with Docker Compose following the guide in [../development.md](../development.md).
11-
12-
## General Workflow
8+
## Quick Start
139

1410
By default, the dependencies are managed with [uv](https://docs.astral.sh/uv/), go there and install it.
1511

16-
From `./backend/` you can install all the dependencies with:
12+
From `./backend_async/` you can install all the dependencies with:
1713

1814
```console
1915
$ uv sync
@@ -25,148 +21,41 @@ Then you can activate the virtual environment with:
2521
$ source .venv/bin/activate
2622
```
2723

28-
Make sure your editor is using the correct Python virtual environment, with the interpreter at `backend/.venv/bin/python`.
29-
30-
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`.
31-
32-
## VS Code
33-
34-
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.
35-
36-
The setup is also already configured so you can run the tests through the VS Code Python tests tab.
37-
38-
## Docker Compose Override
39-
40-
During development, you can change Docker Compose settings that will only affect the local development environment in the file `docker-compose.override.yml`.
41-
42-
The changes to that file only affect the local development environment, not the production environment. So, you can add "temporary" changes that help the development workflow.
24+
Make sure your editor is using the correct Python virtual environment, with the interpreter at `backend_async/.venv/bin/python`.
4325

44-
For example, the directory with the backend code is synchronized in the Docker container, copying the code you change live to the directory inside the container. That allows you to test your changes right away, without having to build the Docker image again. It should only be done during development, for production, you should build the Docker image with a recent version of the backend code. But during development, it allows you to iterate very fast.
45-
46-
There is also a command override that runs `fastapi run --reload` instead of the default `fastapi run`. It starts a single server process (instead of multiple, as would be for production) and reloads the process whenever the code changes. Have in mind that if you have a syntax error and save the Python file, it will break and exit, and the container will stop. After that, you can restart the container by fixing the error and running again:
26+
Start database containers and migrate:
4727

4828
```console
49-
$ docker compose watch
29+
$ docker compose up -d db
30+
$ alembic upgrade head
5031
```
5132

52-
There is also a commented out `command` override, you can uncomment it and comment the default one. It makes the backend container run a process that does "nothing", but keeps the container alive. That allows you to get inside your running container and execute commands inside, for example a Python interpreter to test installed dependencies, or start the development server that reloads when it detects changes.
53-
54-
To get inside the container with a `bash` session you can start the stack with:
33+
Start development server:
5534

5635
```console
57-
$ docker compose watch
36+
$ fastapi dev
5837
```
5938

60-
and then in another terminal, `exec` inside the running container:
61-
62-
```console
63-
$ docker compose exec backend bash
64-
```
39+
Modify or add SQLModel models for data and SQL tables in `./backend_async/app/models.py`, API endpoints in `./backend_async/app/api/`, CRUD (Create, Read, Update, Delete) utils in `./backend_async/app/crud.py`.
6540

66-
You should see an output like:
41+
## Tests
6742

68-
```console
69-
root@7f2607af31c3:/app#
70-
```
43+
Async tests are run using anyio backend. Here we also demonstrate setup using a separate test database, so ensure that the database container is up and running (see Quick Start).
7144

72-
that means that you are in a `bash` session inside your container, as a `root` user, under the `/app` directory, this directory has another directory called "app" inside, that's where your code lives inside the container: `/app/app`.
73-
74-
There you can use the `fastapi run --reload` command to run the debug live reloading server.
45+
From `./backend_async/` you can run:
7546

7647
```console
77-
$ fastapi run --reload app/main.py
48+
$ pytest
7849
```
7950

80-
...it will look like:
51+
For coverage:
8152

8253
```console
83-
root@7f2607af31c3:/app# fastapi run --reload app/main.py
54+
$ coverage run -m pytest
8455
```
8556

86-
and then hit enter. That runs the live reloading server that auto reloads when it detects code changes.
87-
88-
Nevertheless, if it doesn't detect a change but a syntax error, it will just stop with an error. But as the container is still alive and you are in a Bash session, you can quickly restart it after fixing the error, running the same command ("up arrow" and "Enter").
89-
90-
...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.
91-
92-
## Backend tests
93-
94-
To test the backend run:
57+
To view coverage report:
9558

9659
```console
97-
$ bash ./scripts/test.sh
98-
```
99-
100-
The tests run with Pytest, modify and add tests to `./backend/tests/`.
101-
102-
If you use GitHub Actions the tests will run automatically.
103-
104-
### Test running stack
105-
106-
If your stack is already up and you just want to run the tests, you can use:
107-
108-
```bash
109-
docker compose exec backend bash scripts/tests-start.sh
110-
```
111-
112-
That `/app/scripts/tests-start.sh` script just calls `pytest` after making sure that the rest of the stack is running. If you need to pass extra arguments to `pytest`, you can pass them to that command and they will be forwarded.
113-
114-
For example, to stop on first error:
115-
116-
```bash
117-
docker compose exec backend bash scripts/tests-start.sh -x
60+
$ coverage report -m
11861
```
119-
120-
### Test Coverage
121-
122-
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.
123-
124-
## Migrations
125-
126-
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.
127-
128-
Make sure you create a "revision" of your models and that you "upgrade" your database with that revision every time you change them. As this is what will update the tables in your database. Otherwise, your application will have errors.
129-
130-
* Start an interactive session in the backend container:
131-
132-
```console
133-
$ docker compose exec backend bash
134-
```
135-
136-
* Alembic is already configured to import your SQLModel models from `./backend/app/models.py`.
137-
138-
* After changing a model (for example, adding a column), inside the container, create a revision, e.g.:
139-
140-
```console
141-
$ alembic revision --autogenerate -m "Add column last_name to User model"
142-
```
143-
144-
* Commit to the git repository the files generated in the alembic directory.
145-
146-
* After creating the revision, run the migration in the database (this is what will actually change the database):
147-
148-
```console
149-
$ alembic upgrade head
150-
```
151-
152-
If you don't want to use migrations at all, uncomment the lines in the file at `./backend/app/core/db.py` that end in:
153-
154-
```python
155-
SQLModel.metadata.create_all(engine)
156-
```
157-
158-
and comment the line in the file `scripts/prestart.sh` that contains:
159-
160-
```console
161-
$ alembic upgrade head
162-
```
163-
164-
If you don't want to start with the default models and want to remove them / modify them, from the beginning, without having any previous revision, you can remove the revision files (`.py` Python files) under `./backend/app/alembic/versions/`. And then create a first migration as described above.
165-
166-
## Email Templates
167-
168-
The email templates are in `./backend/app/email-templates/`. Here, there are two directories: `build` and `src`. The `src` directory contains the source files that are used to build the final email templates. The `build` directory contains the final email templates that are used by the application.
169-
170-
Before continuing, ensure you have the [MJML extension](https://marketplace.visualstudio.com/items?itemName=attilabuti.vscode-mjml) installed in your VS Code.
171-
172-
Once you have the MJML extension installed, you can create a new email template in the `src` directory. After creating the new email template and with the `.mjml` file open in your editor, open the command palette with `Ctrl+Shift+P` and search for `MJML: Export to HTML`. This will convert the `.mjml` file to a `.html` file and now you can save it in the build directory.

0 commit comments

Comments
 (0)