Skip to content

Commit d9a19a2

Browse files
committed
Merge branch 'release/3.3.6'
2 parents 1568837 + b696933 commit d9a19a2

File tree

26 files changed

+357
-90
lines changed

26 files changed

+357
-90
lines changed

README.md

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![](https://img.shields.io/pypi/dm/fastapi_template?style=for-the-badge)](https://pypi.org/project/fastapi-template/)
33
<div align="center">
44
<img src="https://raw.githubusercontent.com/s3rius/FastAPI-template/master/images/logo.png" width=700>
5-
<div><i>Flexible and Lightweight general-purpose template for FastAPI.</i></div>
5+
<div><i>Flexible general-purpose template for FastAPI.</i></div>
66
</div>
77

88
## Usage
@@ -11,6 +11,12 @@
1111

1212
Poetry version must be greater or equal than 1.1.8. Otherwise it won't be able to install SQLAlchemy.
1313

14+
<div align="center">
15+
<img src="https://user-images.githubusercontent.com/18153319/137182689-ce714440-7576-46a0-8f96-862a8469a28c.gif"/>
16+
<p>Templator in action</p>
17+
</div>
18+
19+
You can install it directly from pypi with pip.
1420
```bash
1521
python3 -m pip install fastapi_template
1622
python3 -m fastapi_template
@@ -22,7 +28,7 @@ docker-compose -f deploy/docker-compose.yml --project-directory . build
2228
docker-compose -f deploy/docker-compose.yml --project-directory . up --build
2329
```
2430

25-
If you want to install in from sources then try this:
31+
If you want to install it from sources, try this:
2632
```shell
2733
python3 -m pip install poetry
2834
python3 -m pip install .
@@ -34,33 +40,33 @@ Also you can use it with docker.
3440
docker run --rm -it -v "$(pwd):/projects" s3rius/fastapi_template
3541
```
3642

37-
<div align="center">
38-
<img src="https://user-images.githubusercontent.com/18153319/137182689-ce714440-7576-46a0-8f96-862a8469a28c.gif"/>
39-
<p>Templator in action</p>
40-
</div>
41-
42-
4343
## Features
4444

45-
One of the coolest features is that this project is extremely small and handy.
46-
You can choose between different databases and even ORMs.
47-
Currently SQLAlchemy1.4, TortoiseORM and Ormar are supported.
45+
One of the coolest features is that this project is extremely configurable.
46+
You can choose between different databases and even ORMs, or
47+
you can even generate a project without a database!
48+
Currently SQLAlchemy1.4, TortoiseORM, Piccolo and Ormar are supported.
4849

49-
TUI and CLI and excellent code documentation.
50+
This project can run as TUI or CLI and has excellent code documentation.
5051

5152
Generator features:
5253
- You can choose between GraphQL and REST api;
5354
- Different databases support;
5455
- Different ORMs support;
5556
- Optional migrations for each ORM except raw drivers;
56-
- redis support;
57-
- rabbitmq support;
57+
- Optional redis support;
58+
- Optional rabbitmq support;
5859
- different CI\CD;
59-
- Kubernetes config generation;
60-
- Demo routers and models;
61-
- Pre-commit integrations;
62-
- Generated tests;
63-
- Tests for the generator itself.
60+
- Optional Kubernetes config generation;
61+
- Optional Demo routers and models (This helps you to see how project is structured);
62+
- Pre-commit integration;
63+
- Generated tests with almost 90% coverage;
64+
- Tests for the generator itself;
65+
- Optional Prometheus integration;
66+
- Optional Sentry integration;
67+
- Optional Loguru logger;
68+
- Optional Opentelemetry integration.
69+
6470

6571
This project can handle arguments passed through command line.
6672

@@ -74,7 +80,8 @@ usage: FastAPI template [-h] [--version] [--name PROJECT_NAME]
7480
[--orm {ormar,sqlalchemy,tortoise,psycopg,piccolo}]
7581
[--ci {none,gitlab_ci,github}] [--redis] [--rabbit]
7682
[--migrations] [--kube] [--dummy] [--routers]
77-
[--swagger] [--force] [--quite]
83+
[--swagger] [--prometheus] [--sentry] [--loguru]
84+
[--opentelemetry] [--force] [--quite]
7885

7986
optional arguments:
8087
-h, --help show this help message and exit
@@ -98,6 +105,10 @@ optional arguments:
98105
Add dummy model
99106
--routers Add example routers
100107
--swagger Enable self-hosted Swagger
108+
--prometheus Add prometheus integration
109+
--sentry Add sentry integration
110+
--loguru Add loguru logger
111+
--opentelemetry Add opentelemetry integration
101112
--force Owerrite directory if it exists
102113
--quite Do not ask for features during generation
103114
```

fastapi_template/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ def parse_args():
144144
default=None,
145145
dest="sentry_enabled",
146146
)
147+
parser.add_argument(
148+
"--loguru",
149+
help="Add loguru logger",
150+
action="store_true",
151+
default=None,
152+
dest="enable_loguru",
153+
)
147154
parser.add_argument(
148155
"--opentelemetry",
149156
help="Add opentelemetry integration",
@@ -203,6 +210,10 @@ def ask_features(current_context: BuilderContext) -> BuilderContext:
203210
"name": "otlp_enabled",
204211
"value": current_context.otlp_enabled,
205212
},
213+
"Loguru logger": {
214+
"name": "enable_loguru",
215+
"value": current_context.enable_loguru,
216+
},
206217
}
207218
if current_context.db != DatabaseType.none:
208219
features["Migrations support"] = {

fastapi_template/input_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class BuilderContext(BaseModel):
121121
sentry_enabled: Optional[bool]
122122
otlp_enabled: Optional[bool]
123123
enable_rmq: Optional[bool]
124+
enable_loguru: Optional[bool]
124125
force: bool = False
125126
quite: bool = False
126127

fastapi_template/template/cookiecutter.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"enable_routers": {
3333
"type": "bool"
3434
},
35+
"enable_loguru": {
36+
"type": "bool"
37+
},
3538
"add_dummy": {
3639
"type": "bool"
3740
},

fastapi_template/template/{{cookiecutter.project_name}}/.pre-commit-config.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,3 @@ repos:
6161
pass_filenames: false
6262
args:
6363
- "{{cookiecutter.project_name}}"
64-
65-
- id: yesqa
66-
name: Remove usless noqa
67-
entry: poetry run yesqa
68-
language: system
69-
types: [python]

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

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,28 @@ But you have to rebuild image every time you modify `poetry.lock` or `pyproject.
4343
docker-compose -f deploy/docker-compose.yml --project-directory . build
4444
```
4545

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:
46+
## Project structure
5347

5448
```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/
49+
$ tree "{{cookiecutter.project_name}}"
50+
{{cookiecutter.project_name}}
51+
├── conftest.py # Fixtures for all tests.
52+
{%- if cookiecutter.db_info.name != "none" %}
53+
├── db # module contains db configurations
54+
│   ├── dao # Data Access Objects. Contains different classes to inteact with database.
55+
│   └── models # Package contains different models for ORMs.
6656
{%- endif %}
57+
├── __main__.py # Startup script. Starts uvicorn.
58+
├── services # Package for different external services such as rabbit or redis etc.
59+
├── settings.py # Main configuration settings for project.
60+
├── static # Static content.
61+
├── tests # Tests for project.
62+
└── web # Package contains web server. Handlers, startup config.
63+
├── api # Package with all handlers.
64+
│   └── router.py # Main router.
65+
├── application.py # FastAPI application configuration.
66+
└── lifetime.py # Contains actions to perform on startup and shutdown.
67+
```
6768
6869
## Configuration
6970
@@ -76,7 +77,8 @@ All environment variabels should start with "{{cookiecutter.project_name | upper
7677
7778
For example if you see in your "{{cookiecutter.project_name}}/settings.py" a variable named like
7879
`random_parameter`, you should provide the "{{cookiecutter.project_name | upper}}_RANDOM_PARAMETER"
79-
variable to configure the value.
80+
variable to configure the value. This behaviour can be changed by overriding `env_prefix` property
81+
in `{{cookiecutter.project_name}}.settings.Settings.Config`.
8082
8183
An exmaple of .env file:
8284
```bash
@@ -87,6 +89,28 @@ An exmaple of .env file:
8789
8890
You can read more about BaseSettings class here: https://pydantic-docs.helpmanual.io/usage/settings/
8991
92+
{%- if cookiecutter.otlp_enabled == "True" %}
93+
## Opentelemetry
94+
95+
If you want to start your project with opentelemetry collector
96+
you can add `-f ./deploy/docker-compose.otlp.yml` to your docker command.
97+
98+
Like this:
99+
100+
```bash
101+
docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.otlp.yml --project-directory . up
102+
```
103+
104+
This command will start opentelemetry collector and jaeger.
105+
After sending a requests you can see traces in jaeger's UI
106+
at http://localhost:16686/.
107+
108+
This docker configuration is not supposed to be used in production.
109+
It's only for demo purpose.
110+
111+
You can read more about opentelemetry here: https://opentelemetry.io/
112+
{%- endif %}
113+
90114
## Pre-commit
91115
92116
To install pre-commit simply run inside the shell:

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
".github"
8888
]
8989
},
90+
"Loguru": {
91+
"enabled": "{{cookiecutter.enable_loguru}}",
92+
"resources": [
93+
"{{cookiecutter.project_name}}/logging.py"
94+
]
95+
},
9096
"Routers": {
9197
"enabled": "{{cookiecutter.enable_routers}}",
9298
"resources": [
@@ -192,4 +198,4 @@
192198
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql"
193199
]
194200
}
195-
}
201+
}

fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ version: '3.9'
33
services:
44
api:
55
ports:
6+
# Exposes application port.
67
- "8000:8000"
78
volumes:
9+
# Adds current directory as volume.
810
- .:/app/src/
911
environment:
12+
# Enables autoreload.
1013
{{cookiecutter.project_name | upper}}_RELOAD: "True"

fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.otlp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
services:
22
api:
33
environment:
4+
# Adds opentelemetry endpoint.
45
{{cookiecutter.project_name | upper}}_OPENTELEMETRY_ENDPOINT: "http://otel-collector:4317"
56

67
otel-collector:
78
image: otel/opentelemetry-collector-contrib:0.53.0
89
volumes:
10+
# Adds config for opentelemetry.
911
- ./deploy/otel-collector-config.yml:/config.yml
1012
command: --config config.yml
1113
ports:

fastapi_template/template/{{cookiecutter.project_name}}/deploy/otel-collector-config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
# Receives all info with OpenTelemetry protocol.
12
receivers:
23
otlp:
34
protocols:
45
grpc:
56
http:
67

8+
# Batch all spans.
79
processors:
810
batch:
911

1012
exporters:
13+
# Exports spans to log.
1114
logging:
1215
logLevel: info
1316

17+
# Exports spans to jaeger.
1418
jaeger:
1519
endpoint: "jaeger:14250"
1620
tls:

0 commit comments

Comments
 (0)