Skip to content

Example Socket.io application #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .idea/runConfigurations/Dev_Stack.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions .idea/runConfigurations/FastAPI_app.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions .idea/runConfigurations/HTTP_App.xml

This file was deleted.

19 changes: 19 additions & 0 deletions .idea/runConfigurations/Otel_Stack.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions .idea/runConfigurations/Socket_io_app.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ FROM base_builder AS http_builder
RUN --mount=type=cache,target=~/.cache/uv \
uv sync --no-dev --group http --no-install-project --frozen --no-editable

# Installs requirements to run production http application
FROM base_builder AS socketio_builder
RUN --mount=type=cache,target=~/.cache/uv \
uv sync --no-dev --group socketio --no-install-project --frozen --no-editable

# Create the base app with the common python packages
FROM base AS base_app
USER nonroot
Expand All @@ -75,7 +80,14 @@ FROM base_app AS http_app
COPY --from=http_builder /venv /venv
COPY --chown=nonroot:nonroot src/http_app ./http_app
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
CMD ["opentelemetry-instrument", "uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory"]
CMD ["opentelemetry-instrument", "python", "-m", "http_app"]

# Copy the socketio python package and requirements from relevant builder
FROM base_app AS socketio_app
COPY --from=socketio_builder_builder /venv /venv
COPY --chown=nonroot:nonroot src/socketio_app ./socketio_app
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
CMD ["opentelemetry-instrument", "python", "-m", "socketio_app"]

# Copy the dramatiq python package and requirements from relevant builder
FROM base_app AS dramatiq_app
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ containers:
# To build shared container layers only once we build a single container before the other ones
docker compose build --build-arg UID=`id -u`

dev:
uv run uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory --reload
dev-http:
uv run ./src/http_app/dev_server.py

dev-socketio:
uv run ./src/socketio_app/dev_server.py

otel:
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
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This template provides out of the box some commonly used functionalities:

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

* `make containers`: Build containers
* `docker compose run --rm dev make migrate`: Run database migrations
* `docker compose up dev`: Run HTTP application with hot reload
* `docker compose up dev-http`: Run HTTP application with hot reload
* `docker compose up dev-socketio`: Run HTTP application with hot reload
* `docker compose up dramatiq-worker`: Run the dramatiq worker
* `docker compose run --rm test`: Run test suite

Expand All @@ -61,7 +63,8 @@ Locally:
* `make dev-dependencies`: Install dev requirements
* `make update-dependencies`: Updates requirements
* `make migrate`: Run database migrations
* `make dev`: Run HTTP application with hot reload
* `make dev-http`: Run HTTP application with hot reload
* `make dev-socketio`: Run HTTP application with hot reload
* `make test`: Run test suite

## Other commands for development
Expand Down
89 changes: 43 additions & 46 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
services:
dev:
dev-http: &dev
build:
dockerfile: Dockerfile
context: .
target: dev
env_file: local.env
environment:
OTEL_SERVICE_NAME: "bootstrap-fastapi-dev"
ports:
- '8000:8000'
working_dir: "/app/src"
volumes:
- '.:/app'
depends_on:
- redis
- otel-collector
command:
- python
- ./http_app/dev_server.py

dev-socketio:
<<: *dev
ports:
- '8001:8001'
command:
- python
- ./socketio_app/dev_server.py

otel-http:
<<: *dev
environment:
OTEL_SERVICE_NAME: "bootstrap-fastapi-dev"
command:
- opentelemetry-instrument
- uvicorn
- http_app:create_app
- --host
- 0.0.0.0
- --port
- "8000"
- --factory
# Remember to disable the reloader in order to allow otel instrumentation
- --reload

# Production image
http:
build:
dockerfile: Dockerfile
context: .
target: http_app
depends_on:
- otel-collector
env_file: local.env
- python
- -m
- http_app

otel-socketio:
<<: *dev
environment:
OTEL_SERVICE_NAME: "bootstrap-fastapi-http"
OTEL_SERVICE_NAME: "bootstrap-socketio-dev"
ports:
- '8001:8000'
volumes:
- './src/sqlite.db:/app/sqlite.db'
- '8001:8001'
command:
- opentelemetry-instrument
- python
- -m
- socketio_app

#########################
#### Helper services ####
#########################

jaeger:
image: jaegertracing/all-in-one:latest
ports:
- "6831:6831/udp" # UDP port for Jaeger agent
- "16686:16686" # Web UI
- "14268:14268" # HTTP port for spans

otel-collector:
image: otel/opentelemetry-collector-contrib
depends_on:
- jaeger
volumes:
- ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml

redis:
image: redis

dramatiq-worker:
build:
dockerfile: Dockerfile
context: .
target: dramatiq_app
env_file: local.env
<<: *dev
environment:
OTEL_SERVICE_NAME: "bootstrap-fastapi-dramatiq-worker"
working_dir: "/app/src"
volumes:
- '.:/app'
depends_on:
- redis
- otel-collector
ports: []
command:
- opentelemetry-instrument
- dramatiq
Expand Down Expand Up @@ -134,7 +139,7 @@ services:
depends_on:
- kratos
- auth-ui
- dev
- dev-http
ports:
# Public traffic port
- "8080:4455"
Expand All @@ -161,11 +166,3 @@ services:
- "make"
- "test"

ci-test:
build:
dockerfile: Dockerfile
context: .
target: dev
command:
- "make"
- "ci-test"
2 changes: 1 addition & 1 deletion docs/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on `/docs` and `/redoc` paths using OpenAPI format.

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

## API versioning

Expand Down
4 changes: 2 additions & 2 deletions otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ receivers:
exporters:
debug:
verbosity: detailed
otlp:
otlp/jaeger:
endpoint: jaeger:4317
tls:
insecure: true
Expand All @@ -22,5 +22,5 @@ service:
exporters: [debug]
traces:
receivers: [otlp]
exporters: [debug]
exporters: [otlp/jaeger]
processors: [batch]
Loading