Skip to content

Commit 1568837

Browse files
committed
Merge branch 'release/3.3.5'
2 parents 9aef9a5 + 8597037 commit 1568837

26 files changed

Lines changed: 526 additions & 82 deletions

fastapi_template/cli.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ def parse_args():
130130
default=None,
131131
dest="self_hosted_swagger",
132132
)
133+
parser.add_argument(
134+
"--prometheus",
135+
help="Add prometheus integration",
136+
action="store_true",
137+
default=None,
138+
dest="prometheus_enabled",
139+
)
140+
parser.add_argument(
141+
"--sentry",
142+
help="Add sentry integration",
143+
action="store_true",
144+
default=None,
145+
dest="sentry_enabled",
146+
)
147+
parser.add_argument(
148+
"--opentelemetry",
149+
help="Add opentelemetry integration",
150+
action="store_true",
151+
default=None,
152+
dest="otlp_enabled",
153+
)
133154
parser.add_argument(
134155
"--force",
135156
help="Owerrite directory if it exists",
@@ -170,6 +191,18 @@ def ask_features(current_context: BuilderContext) -> BuilderContext:
170191
"name": "enable_rmq",
171192
"value": current_context.enable_rmq,
172193
},
194+
"Prometheus integration": {
195+
"name": "prometheus_enabled",
196+
"value": current_context.prometheus_enabled,
197+
},
198+
"Sentry integration": {
199+
"name": "sentry_enabled",
200+
"value": current_context.sentry_enabled,
201+
},
202+
"Opentelemetry integration": {
203+
"name": "otlp_enabled",
204+
"value": current_context.otlp_enabled,
205+
},
173206
}
174207
if current_context.db != DatabaseType.none:
175208
features["Migrations support"] = {

fastapi_template/input_model.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ class BuilderContext(BaseModel):
115115
enable_migrations: Optional[bool]
116116
enable_kube: Optional[bool]
117117
enable_routers: Optional[bool]
118-
add_dummy: Optional[bool] = False
118+
add_dummy: Optional[bool]
119119
self_hosted_swagger: Optional[bool]
120+
prometheus_enabled: Optional[bool]
121+
sentry_enabled: Optional[bool]
122+
otlp_enabled: Optional[bool]
120123
enable_rmq: Optional[bool]
121124
force: bool = False
122125
quite: bool = False
Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,60 @@
11
{
2-
"project_name": {
3-
"type": "string"
4-
},
5-
"project_description": {
6-
"type": "string"
7-
},
8-
"api_type": {
9-
"type": "dict"
10-
},
11-
"db_info": {
12-
"type": "dict"
13-
},
14-
"enable_redis": {
15-
"type": "bool"
16-
},
17-
"enable_rmq": {
18-
"type": "bool"
19-
},
20-
"ci_type": {
21-
"type": "string"
22-
},
23-
"enable_migrations": {
24-
"type": "bool"
25-
},
26-
"enable_kube": {
27-
"type": "bool"
28-
},
29-
"kube_name": {
30-
"type": "string"
31-
},
32-
"enable_routers": {
33-
"type": "bool"
34-
},
35-
"add_dummy": {
36-
"type": "bool"
37-
},
38-
"orm": {
39-
"type": "str"
40-
},
41-
"self_hosted_swagger": {
42-
"type": "bool"
43-
},
44-
"_extensions": [
45-
"cookiecutter.extensions.RandomStringExtension"
46-
],
47-
"_copy_without_render": [
48-
"*.js",
49-
"*.css"
50-
]
2+
"project_name": {
3+
"type": "string"
4+
},
5+
"project_description": {
6+
"type": "string"
7+
},
8+
"api_type": {
9+
"type": "dict"
10+
},
11+
"db_info": {
12+
"type": "dict"
13+
},
14+
"enable_redis": {
15+
"type": "bool"
16+
},
17+
"enable_rmq": {
18+
"type": "bool"
19+
},
20+
"ci_type": {
21+
"type": "string"
22+
},
23+
"enable_migrations": {
24+
"type": "bool"
25+
},
26+
"enable_kube": {
27+
"type": "bool"
28+
},
29+
"kube_name": {
30+
"type": "string"
31+
},
32+
"enable_routers": {
33+
"type": "bool"
34+
},
35+
"add_dummy": {
36+
"type": "bool"
37+
},
38+
"orm": {
39+
"type": "str"
40+
},
41+
"self_hosted_swagger": {
42+
"type": "bool"
43+
},
44+
"prometheus_enabled": {
45+
"type": "bool"
46+
},
47+
"sentry_enabled": {
48+
"type": "bool"
49+
},
50+
"otlp_enabled": {
51+
"type": "bool"
52+
},
53+
"_extensions": [
54+
"cookiecutter.extensions.RandomStringExtension"
55+
],
56+
"_copy_without_render": [
57+
"*.js",
58+
"*.css"
59+
]
5160
}

fastapi_template/template/{{cookiecutter.project_name}}/README.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,112 @@
11
# {{cookiecutter.project_name}}
22

3+
This project was generated using fastapi_template.
34

4-
Start a project with:
5+
## Poetry
6+
7+
This project uses poetry. It's a modern dependency management
8+
tool.
9+
10+
To run the project use this set of commands:
511

612
```bash
7-
docker-compose -f deploy/docker-compose.yml --project-directory . up
13+
poetry install
14+
poetry run python -m {{cookiecutter.project_name}}
815
```
916

10-
If you want to develop in docker with autoreload, use this command:
17+
This will start the server on the configured host.
18+
19+
You can find swagger documentation at `/api/docs`.
20+
21+
You can read more about poetry here: https://python-poetry.org/
22+
23+
## Docker
24+
25+
You can start the project with docker using this command:
26+
27+
```bash
28+
docker-compose -f deploy/docker-compose.yml --project-directory . up --build
29+
```
30+
31+
If you want to develop in docker with autoreload add `-f deploy/docker-compose.dev.yml` to your docker command.
32+
Like this:
1133

1234
```bash
1335
docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up
1436
```
1537

16-
This command exposes application on port 8000, mounts current directory and enables autoreload.
38+
This command exposes the web application on port 8000, mounts current directory and enables autoreload.
1739

1840
But you have to rebuild image every time you modify `poetry.lock` or `pyproject.toml` with this command:
1941

2042
```bash
2143
docker-compose -f deploy/docker-compose.yml --project-directory . build
2244
```
2345

46+
{%- if cookiecutter.otlp_enabled == "True" %}
47+
## Opentelemetry
48+
49+
If you want to start your project with opentelemetry collector
50+
you can add `-f ./deploy/docker-compose.otlp.yml` to your docker command.
51+
52+
Like this:
53+
54+
```bash
55+
docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.otlp.yml --project-directory . up
56+
```
57+
58+
This command will start opentelemetry collector and jaeger.
59+
After sending a requests you can see traces in jaeger's UI
60+
at http://localhost:16686/.
61+
62+
This docker configuration is not supposed to be used in production.
63+
It's only for demo purpose.
64+
65+
You can read more about opentelemetry here: https://opentelemetry.io/
66+
{%- endif %}
67+
68+
## Configuration
69+
70+
This application can be configured with environment variables.
71+
72+
You can create `.env` file in the root directory and place all
73+
environment variables here.
74+
75+
All environment variabels should start with "{{cookiecutter.project_name | upper}}_" prefix.
76+
77+
For example if you see in your "{{cookiecutter.project_name}}/settings.py" a variable named like
78+
`random_parameter`, you should provide the "{{cookiecutter.project_name | upper}}_RANDOM_PARAMETER"
79+
variable to configure the value.
80+
81+
An exmaple of .env file:
82+
```bash
83+
{{cookiecutter.project_name | upper}}_RELOAD="True"
84+
{{cookiecutter.project_name | upper}}_PORT="8000"
85+
{{cookiecutter.project_name | upper}}_ENVIRONMENT="dev"
86+
```
87+
88+
You can read more about BaseSettings class here: https://pydantic-docs.helpmanual.io/usage/settings/
2489

2590
## Pre-commit
2691

2792
To install pre-commit simply run inside the shell:
2893
```bash
2994
pre-commit install
3095
```
96+
97+
pre-commit is very useful to check your code before publishing it.
98+
It's configured using .pre-commit-config.yaml file.
99+
100+
By default it runs:
101+
* black (formats your code);
102+
* mypy (validates types);
103+
* isort (sorts imports in all files);
104+
* flake8 (spots possibe bugs);
105+
* yesqa (removes useless `# noqa` comments).
106+
107+
108+
You can read more about pre-commit here: https://pre-commit.com/
109+
31110
{%- if cookiecutter.enable_kube == 'True' %}
32111

33112
## Kubernetes

fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,18 @@
178178
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_mysql.sql"
179179
]
180180
},
181+
"Opentelemetry support": {
182+
"enabled": "{{cookiecutter.otlp_enabled}}",
183+
"resources": [
184+
"deploy/docker-compose.otlp.yml",
185+
"deploy/otel-collector-config.yml"
186+
]
187+
},
181188
"SQLite DB": {
182189
"enabled": "{{cookiecutter.db_info.name == 'sqlite'}}",
183190
"resources": [
184191
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_sqlite.sql",
185192
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql"
186193
]
187194
}
188-
}
195+
}

fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM python:3.9.6-slim-buster
22

3-
{% if cookiecutter.db_info.name == "mysql" -%}
3+
{%- if cookiecutter.db_info.name == "mysql" %}
44
RUN apt-get update && apt-get install -y \
55
default-libmysqlclient-dev \
66
gcc \
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
api:
3+
environment:
4+
{{cookiecutter.project_name | upper}}_OPENTELEMETRY_ENDPOINT: "http://otel-collector:4317"
5+
6+
otel-collector:
7+
image: otel/opentelemetry-collector-contrib:0.53.0
8+
volumes:
9+
- ./deploy/otel-collector-config.yml:/config.yml
10+
command: --config config.yml
11+
ports:
12+
# Collector's endpoint
13+
- "4317:4317"
14+
15+
jaeger:
16+
image: jaegertracing/all-in-one:1.35
17+
hostname: jaeger
18+
ports:
19+
# Jaeger UI
20+
- 16686:16686
21+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
receivers:
2+
otlp:
3+
protocols:
4+
grpc:
5+
http:
6+
7+
processors:
8+
batch:
9+
10+
exporters:
11+
logging:
12+
logLevel: info
13+
14+
jaeger:
15+
endpoint: "jaeger:14250"
16+
tls:
17+
insecure: true
18+
19+
extensions:
20+
health_check:
21+
pprof:
22+
23+
service:
24+
extensions: [health_check, pprof]
25+
pipelines:
26+
traces:
27+
receivers: [otlp]
28+
processors: [batch]
29+
exporters: [logging, jaeger]

0 commit comments

Comments
 (0)