Skip to content

Commit 674cde5

Browse files
authored
Example Socket.io application (#250)
1 parent a32ffd2 commit 674cde5

35 files changed

+703
-322
lines changed

.idea/runConfigurations/Dev_Stack.xml

Lines changed: 19 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: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/HTTP_App.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.idea/runConfigurations/Otel_Stack.xml

Lines changed: 19 additions & 0 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: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ FROM base_builder AS http_builder
6161
RUN --mount=type=cache,target=~/.cache/uv \
6262
uv sync --no-dev --group http --no-install-project --frozen --no-editable
6363

64+
# Installs requirements to run production http application
65+
FROM base_builder AS socketio_builder
66+
RUN --mount=type=cache,target=~/.cache/uv \
67+
uv sync --no-dev --group socketio --no-install-project --frozen --no-editable
68+
6469
# Create the base app with the common python packages
6570
FROM base AS base_app
6671
USER nonroot
@@ -75,7 +80,14 @@ FROM base_app AS http_app
7580
COPY --from=http_builder /venv /venv
7681
COPY --chown=nonroot:nonroot src/http_app ./http_app
7782
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
78-
CMD ["opentelemetry-instrument", "uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory"]
83+
CMD ["opentelemetry-instrument", "python", "-m", "http_app"]
84+
85+
# Copy the socketio python package and requirements from relevant builder
86+
FROM base_app AS socketio_app
87+
COPY --from=socketio_builder_builder /venv /venv
88+
COPY --chown=nonroot:nonroot src/socketio_app ./socketio_app
89+
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
90+
CMD ["opentelemetry-instrument", "python", "-m", "socketio_app"]
7991

8092
# Copy the dramatiq python package and requirements from relevant builder
8193
FROM base_app AS dramatiq_app

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ containers:
88
# To build shared container layers only once we build a single container before the other ones
99
docker compose build --build-arg UID=`id -u`
1010

11-
dev:
12-
uv run uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory --reload
11+
dev-http:
12+
uv run ./src/http_app/dev_server.py
13+
14+
dev-socketio:
15+
uv run ./src/socketio_app/dev_server.py
1316

1417
otel:
1518
OTEL_SERVICE_NAME=bootstrap-fastapi OTEL_TRACES_EXPORTER=none OTEL_METRICS_EXPORTER=none OTEL_LOGS_EXPORTER=none uv run opentelemetry-instrument uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This template provides out of the box some commonly used functionalities:
2222

2323
* Sync and Async API Documentation using [FastAPI](https://fastapi.tiangolo.com/) and [AsyncAPI](https://www.asyncapi.com/en)
2424
* Async tasks execution using [Dramatiq](https://dramatiq.io/index.html)
25+
* Websocket application using [Socket.io](https://python-socketio.readthedocs.io/en/stable/index.html)
2526
* Repository pattern for databases using [SQLAlchemy](https://www.sqlalchemy.org/) and [SQLAlchemy bind manager](https://febus982.github.io/sqlalchemy-bind-manager/stable/)
2627
* Database migrations using [Alembic](https://alembic.sqlalchemy.org/en/latest/) (configured supporting both sync and async SQLAlchemy engines)
2728
* Database fixtures support using customized [Alembic](https://alembic.sqlalchemy.org/en/latest/) configuration
@@ -50,7 +51,8 @@ Using Docker:
5051

5152
* `make containers`: Build containers
5253
* `docker compose run --rm dev make migrate`: Run database migrations
53-
* `docker compose up dev`: Run HTTP application with hot reload
54+
* `docker compose up dev-http`: Run HTTP application with hot reload
55+
* `docker compose up dev-socketio`: Run HTTP application with hot reload
5456
* `docker compose up dramatiq-worker`: Run the dramatiq worker
5557
* `docker compose run --rm test`: Run test suite
5658

@@ -61,7 +63,8 @@ Locally:
6163
* `make dev-dependencies`: Install dev requirements
6264
* `make update-dependencies`: Updates requirements
6365
* `make migrate`: Run database migrations
64-
* `make dev`: Run HTTP application with hot reload
66+
* `make dev-http`: Run HTTP application with hot reload
67+
* `make dev-socketio`: Run HTTP application with hot reload
6568
* `make test`: Run test suite
6669

6770
## Other commands for development

docker-compose.yaml

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,78 @@
11
services:
2-
dev:
2+
dev-http: &dev
33
build:
44
dockerfile: Dockerfile
55
context: .
66
target: dev
77
env_file: local.env
8-
environment:
9-
OTEL_SERVICE_NAME: "bootstrap-fastapi-dev"
108
ports:
119
- '8000:8000'
1210
working_dir: "/app/src"
1311
volumes:
1412
- '.:/app'
1513
depends_on:
14+
- redis
1615
- otel-collector
16+
command:
17+
- python
18+
- ./http_app/dev_server.py
19+
20+
dev-socketio:
21+
<<: *dev
22+
ports:
23+
- '8001:8001'
24+
command:
25+
- python
26+
- ./socketio_app/dev_server.py
27+
28+
otel-http:
29+
<<: *dev
30+
environment:
31+
OTEL_SERVICE_NAME: "bootstrap-fastapi-dev"
1732
command:
1833
- opentelemetry-instrument
19-
- uvicorn
20-
- http_app:create_app
21-
- --host
22-
- 0.0.0.0
23-
- --port
24-
- "8000"
25-
- --factory
26-
# Remember to disable the reloader in order to allow otel instrumentation
27-
- --reload
28-
29-
# Production image
30-
http:
31-
build:
32-
dockerfile: Dockerfile
33-
context: .
34-
target: http_app
35-
depends_on:
36-
- otel-collector
37-
env_file: local.env
34+
- python
35+
- -m
36+
- http_app
37+
38+
otel-socketio:
39+
<<: *dev
3840
environment:
39-
OTEL_SERVICE_NAME: "bootstrap-fastapi-http"
41+
OTEL_SERVICE_NAME: "bootstrap-socketio-dev"
4042
ports:
41-
- '8001:8000'
42-
volumes:
43-
- './src/sqlite.db:/app/sqlite.db'
43+
- '8001:8001'
44+
command:
45+
- opentelemetry-instrument
46+
- python
47+
- -m
48+
- socketio_app
4449

4550
#########################
4651
#### Helper services ####
4752
#########################
4853

54+
jaeger:
55+
image: jaegertracing/all-in-one:latest
56+
ports:
57+
- "6831:6831/udp" # UDP port for Jaeger agent
58+
- "16686:16686" # Web UI
59+
- "14268:14268" # HTTP port for spans
60+
4961
otel-collector:
5062
image: otel/opentelemetry-collector-contrib
63+
depends_on:
64+
- jaeger
5165
volumes:
5266
- ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml
5367

5468
redis:
5569
image: redis
5670

5771
dramatiq-worker:
58-
build:
59-
dockerfile: Dockerfile
60-
context: .
61-
target: dramatiq_app
62-
env_file: local.env
72+
<<: *dev
6373
environment:
6474
OTEL_SERVICE_NAME: "bootstrap-fastapi-dramatiq-worker"
65-
working_dir: "/app/src"
66-
volumes:
67-
- '.:/app'
68-
depends_on:
69-
- redis
70-
- otel-collector
75+
ports: []
7176
command:
7277
- opentelemetry-instrument
7378
- dramatiq
@@ -134,7 +139,7 @@ services:
134139
depends_on:
135140
- kratos
136141
- auth-ui
137-
- dev
142+
- dev-http
138143
ports:
139144
# Public traffic port
140145
- "8080:4455"
@@ -161,11 +166,3 @@ services:
161166
- "make"
162167
- "test"
163168

164-
ci-test:
165-
build:
166-
dockerfile: Dockerfile
167-
context: .
168-
target: dev
169-
command:
170-
- "make"
171-
- "ci-test"

docs/api-documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on `/docs` and `/redoc` paths using OpenAPI format.
55

66
AsyncAPI documentation is rendered using the
77
[AsyncAPI react components](https://github.com/asyncapi/asyncapi-react).
8-
It is available on `/docs/ws` path.
8+
It is available on `/asyncapi` path.
99

1010
## API versioning
1111

0 commit comments

Comments
 (0)