Skip to content

Commit 44fd5cd

Browse files
authored
Backport improvements (#263)
* Replaces SQLite with MariaDB (tests still running on SQLite for now) * Uses docker as default to run the infrastructure (still keeping run configurations in PyCharm for easy debugging) * Add CORS middleware support * Support a test di_container in FastAPI and SocketIO applications (we'll want a common DI implementation for all applications, not the FastAPI one)
1 parent 418f132 commit 44fd5cd

23 files changed

+296
-135
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,4 @@ dmypy.json
284284
cython_debug/
285285

286286
credentials.env
287+
volumes/

.idea/dataSources.xml

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

.idea/runConfigurations/Dev_Dependencies.xml

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

.idea/runConfigurations/FastAPI_app.xml

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

.idea/runConfigurations/Socket_io_app.xml

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ containers:
44
docker compose build --build-arg UID=`id -u`
55

66
dev-http:
7-
uv run ./src/http_app/dev_server.py
7+
docker compose up dev-http
88

99
dev-socketio:
10-
uv run ./src/socketio_app/dev_server.py
10+
docker compose up dev-socketio
1111

12-
run:
13-
uv run uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory
12+
migrate:
13+
docker compose run --rm migrate
14+
15+
autogenerate-migration:
16+
docker compose run --rm autogenerate-migration
1417

1518
test:
1619
uv run pytest -n auto --cov
@@ -34,9 +37,6 @@ update-dependencies:
3437
uv lock --upgrade
3538
uv sync --all-groups --frozen
3639

37-
migrate:
38-
uv run alembic upgrade heads
39-
4040
format:
4141
uv run ruff format --check .
4242

@@ -53,8 +53,5 @@ check: lint format typing test
5353
docs:
5454
uv run mkdocs serve
5555

56-
adr:
57-
adr-viewer --serve --adr-path docs/adr
58-
5956
docs-build:
6057
uv run mkdocs build

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,31 @@ Create your GitHub repository using this template (The big green `Use this templ
4444
Optionally tweak name and authors in the `pyproject.toml` file, however the metadata
4545
are not used when building the application, nor are referenced anywhere in the code.
4646

47-
Before running any commands, install `uv`:
47+
Before running any commands, install `uv` and `Docker`:
4848

49-
- On Mac (using `brew`): `brew install uv`
49+
- You can install `uv` on Mac using `brew`: `brew install uv`
50+
- Download and install Docker: https://www.docker.com/products/docker-desktop/
5051

5152
Using Docker:
5253

5354
* `make containers`: Build containers
54-
* `docker compose run --rm dev make migrate`: Run database migrations
5555
* `docker compose up dev-http`: Run HTTP application with hot reload
5656
* `docker compose up dev-socketio`: Run HTTP application with hot reload
5757
* `docker compose up dramatiq-worker`: Run the dramatiq worker
5858
* `docker compose run --rm test`: Run test suite
59+
* `docker compose run --rm migrate`: Run database migrations
60+
* `docker compose run --rm autogenerate-migration`: Generate a new migration file
5961

60-
Locally:
62+
Using Make (you still need Docker for most of them):
6163

62-
* `make migrate`: Run database migrations
6364
* `make install-dependencies`: Install requirements
6465
* `make dev-dependencies`: Install dev requirements
6566
* `make update-dependencies`: Updates requirements
66-
* `make migrate`: Run database migrations
6767
* `make dev-http`: Run HTTP application with hot reload
6868
* `make dev-socketio`: Run HTTP application with hot reload
6969
* `make test`: Run test suite
70+
* `make migrate`: Run database migrations
71+
* `make autogenerate-migration`: Generate a new migration file
7072

7173
## Other commands for development
7274

alembic.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
6767
# black.type = console_scripts
6868
# black.entrypoint = black
6969
# black.options = -l 79 REVISION_SCRIPT_FILENAME
70+
hooks = ruff
71+
ruff.type = exec
72+
ruff.executable = %(here)s/.venv/bin/ruff
73+
ruff.options = format REVISION_SCRIPT_FILENAME

credentials.env.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GC_USERNAME: <GRAFANA_USERNAME>
2+
GC_PASSWORD: <GRAFANA_TOKEN>
3+
GC_ENDPOINT: <GRAFANA_OTLP_ENDPOINT>

docker-compose.yaml

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ services:
44
dockerfile: Dockerfile
55
context: .
66
target: dev
7-
env_file: local.env
7+
env_file: docker.env
88
environment:
9-
APP_NAME: "bootstrap-fastapi"
9+
APP_NAME: "backend-fastapi"
1010
ports:
1111
- '8000:8000'
1212
working_dir: "/app/src"
1313
volumes:
1414
- '.:/app'
1515
depends_on:
16-
- redis
17-
- otel-collector
16+
redis:
17+
condition: service_healthy
18+
otel-collector:
19+
condition: service_started
20+
mariadb:
21+
condition: service_healthy
1822
command:
1923
- python
2024
- ./http_app/dev_server.py
2125

2226
dev-socketio:
2327
<<: *dev
2428
environment:
25-
APP_NAME: "bootstrap-socketio"
29+
APP_NAME: "backend-socketio"
2630
ports:
2731
- '8001:8001'
2832
command:
@@ -52,6 +56,7 @@ services:
5256
- jaeger
5357
ports:
5458
- "12345:12345"
59+
- "4317:4317"
5560
volumes:
5661
- ./config.alloy:/etc/alloy/config.alloy
5762
command:
@@ -63,11 +68,34 @@ services:
6368

6469
redis:
6570
image: redis
71+
healthcheck:
72+
test: ["CMD", "redis-cli", "ping"]
73+
interval: 10s
74+
timeout: 5s
75+
retries: 5
76+
77+
mariadb:
78+
image: mariadb:11.4
79+
environment:
80+
MYSQL_ROOT_PASSWORD: "stanis"
81+
MYSQL_DATABASE: "backend"
82+
MYSQL_USER: "corinna"
83+
MYSQL_PASSWORD: "gioieiiere"
84+
volumes:
85+
- ./volumes/mariadb:/var/lib/mysql
86+
ports:
87+
- "3306:3306"
88+
healthcheck:
89+
test: [ "CMD", "healthcheck.sh", "--su-mysql", "--connect", "--innodb_initialized" ]
90+
interval: 10s
91+
timeout: 5s
92+
retries: 5
93+
start_period: 30s
6694

6795
dramatiq-worker:
6896
<<: *dev
6997
environment:
70-
APP_NAME: "bootstrap-dramatiq-worker"
98+
APP_NAME: "backend-dramatiq-worker"
7199
ports: []
72100
command:
73101
- dramatiq
@@ -151,12 +179,32 @@ services:
151179
#### One-off commands ####
152180
##########################
153181
test:
154-
build:
155-
dockerfile: Dockerfile
156-
context: .
157-
target: dev
158-
volumes:
159-
- '.:/app'
182+
<<: *dev
183+
environment:
184+
APP_NAME: "backend-test"
185+
ports: []
160186
command:
161187
- "make"
162188
- "test"
189+
190+
migrate:
191+
<<: *dev
192+
environment:
193+
APP_NAME: "backend-migrations"
194+
ports: []
195+
command:
196+
- "alembic"
197+
- "upgrade"
198+
- "heads"
199+
200+
autogenerate-migration:
201+
<<: *dev
202+
environment:
203+
APP_NAME: "backend-migration-generator"
204+
ports: []
205+
command:
206+
- "alembic"
207+
- "revision"
208+
- "--autogenerate"
209+
- "-m"
210+
- "Description message"

0 commit comments

Comments
 (0)