Skip to content

Commit f0c4cc8

Browse files
committed
Adding async backend example template
1 parent fd74a4d commit f0c4cc8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4639
-1
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ SMTP_PORT=587
3535
POSTGRES_SERVER=localhost
3636
POSTGRES_PORT=5432
3737
POSTGRES_DB=app
38+
POSTGRES_DB_ASYNC=app_async #create separate db for backend_async using psycopg 3.12 and pwdlib[argon2]
39+
POSTGRES_TEST_DB=app_async_test #demonstrate separate test db setup
3840
POSTGRES_USER=postgres
3941
POSTGRES_PASSWORD=changethis
4042

backend_async/.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Python
2+
__pycache__
3+
app.egg-info
4+
*.pyc
5+
.mypy_cache
6+
.coverage
7+
htmlcov
8+
.venv

backend_async/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
__pycache__
2+
app.egg-info
3+
*.pyc
4+
.mypy_cache
5+
.coverage
6+
htmlcov
7+
.cache
8+
.venv

backend_async/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

backend_async/Dockerfile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
FROM python:3.13-slim
2+
3+
ENV PYTHONUNBUFFERED=1
4+
5+
WORKDIR /app/
6+
7+
# Install uv
8+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
9+
COPY --from=ghcr.io/astral-sh/uv:0.5.11 /uv /uvx /bin/
10+
11+
# Place executables in the environment at the front of the path
12+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment
13+
ENV PATH="/app/.venv/bin:$PATH"
14+
15+
# Compile bytecode
16+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode
17+
ENV UV_COMPILE_BYTECODE=1
18+
19+
# uv Cache
20+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching
21+
ENV UV_LINK_MODE=copy
22+
23+
# Install dependencies
24+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
25+
RUN --mount=type=cache,target=/root/.cache/uv \
26+
--mount=type=bind,source=uv.lock,target=uv.lock \
27+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
28+
uv sync --frozen --no-install-project
29+
30+
ENV PYTHONPATH=/app
31+
32+
COPY ./scripts /app/scripts
33+
34+
COPY ./pyproject.toml ./uv.lock ./alembic.ini /app/
35+
36+
COPY ./app /app/app
37+
COPY ./tests /app/tests
38+
39+
# Sync the project
40+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
41+
RUN --mount=type=cache,target=/root/.cache/uv \
42+
uv sync
43+
44+
CMD ["fastapi", "run", "--workers", "4", "app/main.py"]

backend_async/README.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# FastAPI Project - Backend
2+
3+
## Requirements
4+
5+
* [Docker](https://www.docker.com/).
6+
* [uv](https://docs.astral.sh/uv/) for Python package and environment management.
7+
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
13+
14+
By default, the dependencies are managed with [uv](https://docs.astral.sh/uv/), go there and install it.
15+
16+
From `./backend/` you can install all the dependencies with:
17+
18+
```console
19+
$ uv sync
20+
```
21+
22+
Then you can activate the virtual environment with:
23+
24+
```console
25+
$ source .venv/bin/activate
26+
```
27+
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.
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:
47+
48+
```console
49+
$ docker compose watch
50+
```
51+
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:
55+
56+
```console
57+
$ docker compose watch
58+
```
59+
60+
and then in another terminal, `exec` inside the running container:
61+
62+
```console
63+
$ docker compose exec backend bash
64+
```
65+
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+
## Backend tests
93+
94+
To test the backend run:
95+
96+
```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
118+
```
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.

backend_async/alembic.ini

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts.
5+
# this is typically a path given in POSIX (e.g. forward slashes)
6+
# format, relative to the token %(here)s which refers to the location of this
7+
# ini file
8+
script_location = %(here)s/app/alembic
9+
10+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
11+
# Uncomment the line below if you want the files to be prepended with date and time
12+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
13+
# for all available tokens
14+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
15+
16+
# sys.path path, will be prepended to sys.path if present.
17+
# defaults to the current working directory. for multiple paths, the path separator
18+
# is defined by "path_separator" below.
19+
prepend_sys_path = .
20+
21+
# timezone to use when rendering the date within the migration file
22+
# as well as the filename.
23+
# If specified, requires the tzdata library which can be installed by adding
24+
# `alembic[tz]` to the pip requirements.
25+
# string value is passed to ZoneInfo()
26+
# leave blank for localtime
27+
# timezone =
28+
29+
# max length of characters to apply to the "slug" field
30+
# truncate_slug_length = 40
31+
32+
# set to 'true' to run the environment during
33+
# the 'revision' command, regardless of autogenerate
34+
# revision_environment = false
35+
36+
# set to 'true' to allow .pyc and .pyo files without
37+
# a source .py file to be detected as revisions in the
38+
# versions/ directory
39+
# sourceless = false
40+
41+
# version location specification; This defaults
42+
# to <script_location>/versions. When using multiple version
43+
# directories, initial revisions must be specified with --version-path.
44+
# The path separator used here should be the separator specified by "path_separator"
45+
# below.
46+
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
47+
48+
# path_separator; This indicates what character is used to split lists of file
49+
# paths, including version_locations and prepend_sys_path within configparser
50+
# files such as alembic.ini.
51+
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
52+
# to provide os-dependent path splitting.
53+
#
54+
# Note that in order to support legacy alembic.ini files, this default does NOT
55+
# take place if path_separator is not present in alembic.ini. If this
56+
# option is omitted entirely, fallback logic is as follows:
57+
#
58+
# 1. Parsing of the version_locations option falls back to using the legacy
59+
# "version_path_separator" key, which if absent then falls back to the legacy
60+
# behavior of splitting on spaces and/or commas.
61+
# 2. Parsing of the prepend_sys_path option falls back to the legacy
62+
# behavior of splitting on spaces, commas, or colons.
63+
#
64+
# Valid values for path_separator are:
65+
#
66+
# path_separator = :
67+
# path_separator = ;
68+
# path_separator = space
69+
# path_separator = newline
70+
#
71+
# Use os.pathsep. Default configuration used for new projects.
72+
path_separator = os
73+
74+
75+
# set to 'true' to search source files recursively
76+
# in each "version_locations" directory
77+
# new in Alembic version 1.10
78+
# recursive_version_locations = false
79+
80+
# the output encoding used when revision files
81+
# are written from script.py.mako
82+
# output_encoding = utf-8
83+
84+
# database URL. This is consumed by the user-maintained env.py script only.
85+
# other means of configuring database URLs may be customized within the env.py
86+
# file.
87+
sqlalchemy.url = driver://user:pass@localhost/dbname
88+
89+
90+
[post_write_hooks]
91+
# post_write_hooks defines scripts or Python functions that are run
92+
# on newly generated revision scripts. See the documentation for further
93+
# detail and examples
94+
95+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
96+
# hooks = black
97+
# black.type = console_scripts
98+
# black.entrypoint = black
99+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
100+
101+
# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module
102+
# hooks = ruff
103+
# ruff.type = module
104+
# ruff.module = ruff
105+
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
106+
107+
# Alternatively, use the exec runner to execute a binary found on your PATH
108+
# hooks = ruff
109+
# ruff.type = exec
110+
# ruff.executable = ruff
111+
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
112+
113+
# Logging configuration. This is also consumed by the user-maintained
114+
# env.py script only.
115+
[loggers]
116+
keys = root,sqlalchemy,alembic
117+
118+
[handlers]
119+
keys = console
120+
121+
[formatters]
122+
keys = generic
123+
124+
[logger_root]
125+
level = WARNING
126+
handlers = console
127+
qualname =
128+
129+
[logger_sqlalchemy]
130+
level = WARNING
131+
handlers =
132+
qualname = sqlalchemy.engine
133+
134+
[logger_alembic]
135+
level = INFO
136+
handlers =
137+
qualname = alembic
138+
139+
[handler_console]
140+
class = StreamHandler
141+
args = (sys.stderr,)
142+
level = NOTSET
143+
formatter = generic
144+
145+
[formatter_generic]
146+
format = %(levelname)-5.5s [%(name)s] %(message)s
147+
datefmt = %H:%M:%S

backend_async/app/__init__.py

Whitespace-only changes.

backend_async/app/alembic/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration with an async dbapi.

0 commit comments

Comments
 (0)