Skip to content

Commit fa7e793

Browse files
authored
Merge pull request #104 from febus982/src-layout
* Removes jaeger container and setup Opentelemetry collector to output to logs * Move all the application sources in src directory for a cleaner layout * Remove GRPC (The celery worker implementation is enough as example for another application)
2 parents e75bf77 + a5d6eb8 commit fa7e793

Some content is hidden

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

82 files changed

+181
-708
lines changed

.codeclimate.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ exclude_patterns:
66
- "spec/"
77
- "!spec/support/helpers"
88
- "config/"
9-
- "alembic/"
10-
- "grpc_app/generated/"
9+
- "src/alembic/"
1110
- "db/"
1211
- "dist/"
1312
- "features/"

.idea/bootstrap-fastapi-service.iml

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

Dockerfile

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ARG GID=2000
55
RUN addgroup --gid $GID nonroot && \
66
adduser --uid $UID --gid $GID --disabled-password --gecos "" nonroot
77
WORKDIR /app
8+
RUN chown nonroot:nonroot /app
89

910
# Creating a separate directory for venvs allows to easily
1011
# copy them from the builder and to mount the application
@@ -44,8 +45,8 @@ COPY --chown=nonroot:nonroot Makefile .
4445

4546
# Test image, contains all files and dependencies
4647
FROM base_builder as dev
47-
RUN make dev-dependencies
4848
COPY --chown=nonroot:nonroot . .
49+
RUN make dev-dependencies
4950
# Note that opentelemetry doesn't play well together with uvicorn reloader
5051
# when signals are propagated, we disable it in dev image default CMD
5152
CMD ["uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory", "--reload"]
@@ -58,40 +59,30 @@ RUN poetry install --no-root
5859
FROM base_builder as http_builder
5960
RUN poetry install --no-root --with http
6061

61-
# Installs requirements to run production grpc application
62-
FROM base_builder as grpc_builder
63-
RUN poetry install --no-root --with grpc
64-
6562
# Copy the shared python packages
6663
FROM base as base_app
6764
USER nonroot
6865
RUN poetry config virtualenvs.path /poetryvenvs
6966
COPY --chown=nonroot:nonroot pyproject.toml .
7067
COPY --chown=nonroot:nonroot poetry.lock .
71-
COPY --chown=nonroot:nonroot alembic ./alembic
72-
COPY --chown=nonroot:nonroot domains ./domains
73-
COPY --chown=nonroot:nonroot gateways ./gateways
74-
COPY --chown=nonroot:nonroot common ./common
75-
COPY --chown=nonroot:nonroot alembic.ini .
68+
COPY --chown=nonroot:nonroot src/alembic ./alembic
69+
COPY --chown=nonroot:nonroot src/domains ./domains
70+
COPY --chown=nonroot:nonroot src/gateways ./gateways
71+
COPY --chown=nonroot:nonroot src/common ./common
72+
COPY --chown=nonroot:nonroot src/alembic.ini .
7673
COPY --chown=nonroot:nonroot Makefile .
7774

7875
# Copy the http python package and requirements from relevant builder
7976
FROM base_app as http_app
8077
COPY --from=http_builder /poetryvenvs /poetryvenvs
81-
COPY --chown=nonroot:nonroot http_app ./http_app
78+
COPY --chown=nonroot:nonroot src/http_app ./http_app
8279
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
8380
CMD ["opentelemetry-instrument", "uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory"]
8481

85-
# Copy the grpc python package and requirements from relevant builder
86-
FROM base_app as grpc_app
87-
COPY --from=grpc_builder /poetryvenvs /poetryvenvs
88-
COPY --chown=nonroot:nonroot grpc_app ./grpc_app
89-
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
90-
CMD ["opentelemetry-instrument", "python3", "-m", "grpc_app"]
91-
9282
# Copy the celery python package and requirements from relevant builder
9383
FROM base_app as celery_app
9484
COPY --from=celery_builder /poetryvenvs /poetryvenvs
95-
COPY --chown=nonroot:nonroot celery_worker ./celery_worker
85+
COPY --chown=nonroot:nonroot src/celery_worker ./celery_worker
86+
RUN ls
9687
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
9788
CMD ["opentelemetry-instrument", "celery", "-A", "celery_worker:app", "worker", "-l", "INFO"]

Makefile

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ otel:
1717
run:
1818
poetry run uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory
1919

20-
grpc:
21-
poetry run python3 -m grpc_app
22-
2320
test:
2421
poetry run pytest -n auto --cov
2522

@@ -33,13 +30,13 @@ typing:
3330
poetry run mypy
3431

3532
install-dependencies:
36-
poetry install --no-root --with http,grpc
33+
poetry install --no-root --with http
3734

3835
dev-dependencies:
39-
poetry install --no-root --with http,grpc,dev
36+
poetry install --with http,dev
4037

4138
update-dependencies:
42-
poetry update --with http,grpc,dev
39+
poetry update --with http,dev
4340

4441
migrate:
4542
poetry run alembic upgrade heads
@@ -50,23 +47,6 @@ format:
5047
lint:
5148
poetry run ruff .
5249

53-
# There are issues on how python imports are generated when using nested
54-
# packages. The following setup appears to work, however it might need
55-
# to be reviewed. https://github.com/protocolbuffers/protobuf/issues/1491
56-
generate-proto:
57-
rm -rf ./grpc_app/generated/*.p*
58-
touch ./grpc_app/generated/__init__.py
59-
poetry run python -m grpc_tools.protoc \
60-
-I grpc_app.generated=./grpc_app/proto/ \
61-
--python_out=. \
62-
--mypy_out=. \
63-
grpc_app/proto/*.proto
64-
poetry run python -m grpc_tools.protoc \
65-
-I grpc_app/generated=./grpc_app/proto/ \
66-
--grpc_python_out=. \
67-
grpc_app/proto/*.proto
68-
git add ./grpc_app/generated
69-
7050
fix:
7151
poetry run ruff . --fix
7252
poetry run ruff format .

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ Packages are ordered from the highest level to the lowest one.
4545

4646
------
4747

48+
* `alembic` (database migration manager)
4849
* `http_app` (http presentation layer)
49-
* `grpc_app` (grpc presentation layer)
50-
* `storage` (database connection manager, repository implementation)
50+
* `celery_worker` (async tasks runner)
51+
* `gateways` (database connection manager, repository implementation, event emitter, etc.)
5152

5253
------
5354

@@ -70,7 +71,7 @@ Using Docker:
7071
* `make containers`: Build containers
7172
* `docker compose run --rm dev make migrate`: Run database migrations
7273
* `docker compose up dev`: Run HTTP application with hot reload
73-
* `docker compose up grpc`: Run GRPC application
74+
* `docker compose up celery-worker`: Run the celery worker
7475
* `docker compose run --rm test`: Run test suite
7576

7677
Locally:
@@ -81,12 +82,10 @@ Locally:
8182
* `make update-dependencies`: Updates requirements
8283
* `make migrate`: Run database migrations
8384
* `make dev`: Run HTTP application with hot reload
84-
* `make grpc`: Run GRPC application
8585
* `make test`: Run test suite
8686

8787
## Other commands for development
8888

89-
* `make generate-proto`: Generates grpcio python stubs from `.proto` files in `grpc_app/proto` directory
9089
* `make check`: Run tests, code style and lint checks
9190
* `make fix`: Run tests, code style and lint checks with automatic fixes (where possible)
9291

alembic.ini

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[alembic]
44
# path to migration scripts
5-
script_location = alembic
5+
script_location = src/alembic
66

77
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
88
# Uncomment the line below if you want the files to be prepended with date and time
@@ -12,7 +12,7 @@ file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(hour).2d%%(minute).2d%%(sec
1212

1313
# sys.path path, will be prepended to sys.path if present.
1414
# defaults to the current working directory.
15-
prepend_sys_path = .
15+
#prepend_sys_path = .
1616

1717
# timezone to use when rendering the date within the migration file
1818
# as well as the filename.
@@ -56,13 +56,6 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
5656
# output_encoding = utf-8
5757

5858
# We inject db names and config using env.py in alembic directory
59-
#databases = engine1, engine2
60-
61-
#[engine1]
62-
#sqlalchemy.url = driver://user:pass@localhost/dbname
63-
64-
#[engine2]
65-
#sqlalchemy.url = driver://user:pass@localhost/dbname2
6659

6760
[post_write_hooks]
6861
# post_write_hooks defines scripts or Python functions that are run

architecture.png

-6.73 KB
Loading

architecture.puml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ package http_app #fff {
88
package routes #DDDDDD
99
}
1010

11-
package grpc_app #fff {
12-
package servicers #DDDDDD
13-
}
14-
1511
package domains #DDDDDD {
1612
package books {
1713
class Book
@@ -42,8 +38,6 @@ package gateways #DDDDDD {
4238
'links framework - domains
4339
routes --> BookService
4440
routes --> Book
45-
servicers --> BookService
46-
servicers --> Book
4741
celery_worker ---> tasks
4842
tasks <-u- BookService
4943
tasks -u-> Book

docker-compose.yaml

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
version: '3.8'
22
services:
33

4-
jaeger:
5-
image: jaegertracing/all-in-one
6-
ports:
7-
- "16686:16686"
8-
94
otel-collector:
105
image: otel/opentelemetry-collector-contrib
11-
depends_on:
12-
- jaeger
136
volumes:
147
- ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml
158

@@ -25,7 +18,24 @@ services:
2518
OTEL_SERVICE_NAME: "bootstrap-fastapi-worker"
2619
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"
2720
volumes:
28-
- '.:/app'
21+
- './src:/app'
22+
- './pyproject.toml:/app/pyproject.toml'
23+
depends_on:
24+
- redis
25+
- otel-collector
26+
- celery-beat
27+
28+
celery-beat:
29+
build:
30+
dockerfile: Dockerfile
31+
context: .
32+
target: celery_app
33+
environment:
34+
OTEL_SERVICE_NAME: "bootstrap-fastapi-worker"
35+
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"
36+
volumes:
37+
- './src:/app'
38+
- './pyproject.toml:/app/pyproject.toml'
2939
depends_on:
3040
- redis
3141
- otel-collector
@@ -34,8 +44,7 @@ services:
3444
- celery
3545
- -A
3646
- celery_worker:app
37-
- worker
38-
- --beat
47+
- beat
3948
- -l
4049
- INFO
4150

@@ -50,10 +59,11 @@ services:
5059
ports:
5160
- '8000:8000'
5261
volumes:
53-
- '.:/app'
62+
- './src:/app'
63+
- './pyproject.toml:/app/pyproject.toml'
5464
depends_on:
5565
- redis
56-
- celery-worker
66+
- otel-collector
5767
command:
5868
- opentelemetry-instrument
5969
- uvicorn
@@ -72,7 +82,6 @@ services:
7282
target: http_app
7383
depends_on:
7484
- redis
75-
- celery-worker
7685
- otel-collector
7786
environment:
7887
OTEL_SERVICE_NAME: "bootstrap-fastapi-http"
@@ -82,35 +91,7 @@ services:
8291
volumes:
8392
- './sqlite.db:/app/sqlite.db'
8493

85-
grpc:
86-
build:
87-
dockerfile: Dockerfile
88-
context: .
89-
target: grpc_app
90-
depends_on:
91-
- redis
92-
- celery-worker
93-
- otel-collector
94-
environment:
95-
OTEL_SERVICE_NAME: "bootstrap-fastapi-grpc"
96-
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"
97-
ports:
98-
- "9999:9999"
99-
volumes:
100-
- '.:/app'
101-
10294
# Starting from here there are only single-run commands, we can use `make` here
103-
generate-proto:
104-
build:
105-
dockerfile: Dockerfile
106-
context: .
107-
target: grpc_builder
108-
volumes:
109-
- '.:/app'
110-
command:
111-
- "make"
112-
- "generate-proto"
113-
11495
test:
11596
build:
11697
dockerfile: Dockerfile

docker-container.png

6.31 KB
Loading

0 commit comments

Comments
 (0)