|
5 | 5 | * [Docker](https://www.docker.com/). |
6 | 6 | * [uv](https://docs.astral.sh/uv/) for Python package and environment management. |
7 | 7 |
|
8 | | -## Docker Compose |
| 8 | +## Local Development |
9 | 9 |
|
10 | | -Start the local development environment with Docker Compose following the guide in [../development.md](../development.md). |
| 10 | +Run the backend locally and connect it to PostgreSQL in Docker Compose. |
| 11 | + |
| 12 | +From the project root, start PostgreSQL and Mailcatcher: |
| 13 | + |
| 14 | +```console |
| 15 | +$ docker compose up -d db mailcatcher |
| 16 | +``` |
| 17 | + |
| 18 | +Then, from `./backend/`, install the dependencies, prepare the database, and start the development server: |
| 19 | + |
| 20 | +```console |
| 21 | +$ uv sync |
| 22 | +$ uv run bash scripts/prestart.sh |
| 23 | +$ uv run fastapi dev |
| 24 | +``` |
| 25 | + |
| 26 | +The API is available at `http://localhost:8000`, with automatic interactive docs at `http://localhost:8000/docs`. |
11 | 27 |
|
12 | 28 | ## General Workflow |
13 | 29 |
|
@@ -35,66 +51,32 @@ There are already configurations in place to run the backend through the VS Code |
35 | 51 |
|
36 | 52 | The setup is also already configured so you can run the tests through the VS Code Python tests tab. |
37 | 53 |
|
38 | | -## Docker Compose Override |
| 54 | +## Full Stack with Docker Compose |
39 | 55 |
|
40 | | -During development, you can change Docker Compose settings that will only affect the local development environment in the file `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. |
43 | | - |
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: |
| 56 | +To run the backend and built frontend in Docker Compose: |
47 | 57 |
|
48 | 58 | ```console |
49 | 59 | $ docker compose watch |
50 | 60 | ``` |
51 | 61 |
|
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. |
| 62 | +The application is available at `http://localhost:8000`. |
53 | 63 |
|
54 | | -To get inside the container with a `bash` session you can start the stack with: |
| 64 | +### Docker Compose Override |
55 | 65 |
|
56 | | -```console |
57 | | -$ docker compose watch |
58 | | -``` |
| 66 | +The `compose.override.yml` file contains local settings for published ports, source synchronization, automatic image rebuilds, and backend reloads. Docker Compose applies it automatically when you run `docker compose` without an explicit file list. |
59 | 67 |
|
60 | | -and then in another terminal, `exec` inside the running container: |
| 68 | +To open a shell in the backend container: |
61 | 69 |
|
62 | 70 | ```console |
63 | 71 | $ docker compose exec backend bash |
64 | 72 | ``` |
65 | 73 |
|
66 | | -You should see an output like: |
67 | | - |
68 | | -```console |
69 | | -root@7f2607af31c3:/app# |
70 | | -``` |
71 | | - |
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. |
75 | | - |
76 | | -```console |
77 | | -$ fastapi run --reload app/main.py |
78 | | -``` |
79 | | - |
80 | | -...it will look like: |
81 | | - |
82 | | -```console |
83 | | -root@7f2607af31c3:/app# fastapi run --reload app/main.py |
84 | | -``` |
85 | | - |
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 | 74 | ## Backend tests |
93 | 75 |
|
94 | | -To test the backend run: |
| 76 | +To test the backend from the `backend` directory, run: |
95 | 77 |
|
96 | 78 | ```console |
97 | | -$ bash ./scripts/test.sh |
| 79 | +$ uv run bash ./scripts/test.sh |
98 | 80 | ``` |
99 | 81 |
|
100 | 82 | The tests run with Pytest, modify and add tests to `./backend/tests/`. |
@@ -123,30 +105,22 @@ When the tests are run, a file `htmlcov/index.html` is generated, you can open i |
123 | 105 |
|
124 | 106 | ## Migrations |
125 | 107 |
|
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 | | -``` |
| 108 | +Make sure you create a revision of your models and upgrade the database with that revision every time you change them. From the `backend` directory, use `uv` to run Alembic against the PostgreSQL container: |
135 | 109 |
|
136 | 110 | * Alembic is already configured to import your SQLModel models from `./backend/app/models.py`. |
137 | 111 |
|
138 | | -* After changing a model (for example, adding a column), inside the container, create a revision, e.g.: |
| 112 | +* After changing a model (for example, adding a column), create a revision: |
139 | 113 |
|
140 | 114 | ```console |
141 | | -$ alembic revision --autogenerate -m "Add column last_name to User model" |
| 115 | +$ uv run alembic revision --autogenerate -m "Add column last_name to User model" |
142 | 116 | ``` |
143 | 117 |
|
144 | 118 | * Commit to the git repository the files generated in the alembic directory. |
145 | 119 |
|
146 | 120 | * After creating the revision, run the migration in the database (this is what will actually change the database): |
147 | 121 |
|
148 | 122 | ```console |
149 | | -$ alembic upgrade head |
| 123 | +$ uv run alembic upgrade head |
150 | 124 | ``` |
151 | 125 |
|
152 | 126 | 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: |
|
0 commit comments