Skip to content

Commit

Permalink
Merge branch 'release/3.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Apr 19, 2022
2 parents 41edbc3 + 07d3ece commit 465bbf3
Show file tree
Hide file tree
Showing 46 changed files with 917 additions and 67 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
name: Testing fastapi-template

on:
push:
branches-ignore:
- master
tags-ignore:
- "*"
on:
pull_request:
branches:
branches:
- 'release/**'

jobs:
Expand Down Expand Up @@ -39,5 +34,9 @@ jobs:
uses: knowsuchagency/poetry-install@v1
env:
POETRY_VIRTUALENVS_CREATE: false
- name: Setup GIT
run: |
git config --global user.name "fastapi_template"
git config --global user.email "[email protected]"
- name: Run tests
run: poetry run pytest -vv --exitfirst
run: poetry run pytest -vv --exitfirst -n auto
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ Currently SQLAlchemy1.4, TortoiseORM and Ormar are supported.
TUI and CLI and excellent code documentation.

Generator features:
- You can choose between GraphQL and REST api;
- Different databases support;
- Different ORMs support;
- Optional migrations for each ORM;
- Optional migrations for each ORM except raw drivers;
- redis support;
- different CI\CD;
- Kubernetes config generation;
Expand All @@ -67,23 +68,26 @@ $ python -m fastapi_template --help

usage: FastAPI template [-h] [--version] [--name PROJECT_NAME]
[--description PROJECT_DESCRIPTION]
[--api-type {rest,graphql}]
[--db {none,sqlite,mysql,postgresql}]
[--orm {ormar,sqlalchemy,tortoise}]
[--ci {none,gitlab,github}] [--redis] [--migrations]
[--kube] [--dummy] [--routers] [--swagger] [--force]
[--quite]
[--orm {ormar,sqlalchemy,tortoise,psycopg,piccolo}]
[--ci {none,gitlab_ci,github}] [--redis]
[--migrations] [--kube] [--dummy] [--routers]
[--swagger] [--force] [--quite]

optional arguments:
-h, --help show this help message and exit
--version, -V Prints current version
--name PROJECT_NAME Name of your awesome project
--description PROJECT_DESCRIPTION
Project description
--api-type {rest,graphql}
API type
--db {none,sqlite,mysql,postgresql}
Database
--orm {ormar,sqlalchemy,tortoise}
--orm {ormar,sqlalchemy,tortoise,psycopg,piccolo}
ORM
--ci {none,gitlab,github}
--ci {none,gitlab_ci,github}
Choose CI support
--redis Add redis support
--migrations Add migrations support
Expand Down
17 changes: 17 additions & 0 deletions fastapi_template/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SUPPORTED_ORMS,
ORMS_WITHOUT_MIGRATIONS,
ORM,
APIType,
BuilderContext,
DB_INFO,
DatabaseType,
Expand Down Expand Up @@ -47,6 +48,14 @@ def parse_args():
dest="project_description",
help="Project description",
)
parser.add_argument(
"--api-type",
help="API type",
type=str,
choices=list(map(attrgetter("value"), APIType)),
default=None,
dest="api_type",
)
parser.add_argument(
"--db",
help="Database",
Expand Down Expand Up @@ -186,6 +195,14 @@ def read_user_input(current_context: BuilderContext) -> BuilderContext:
current_context.kube_name = current_context.project_name.replace("_", "-")
if current_context.project_description is None:
current_context.project_description = prompt("Project description: ")
if current_context.api_type is None:
current_context.api_type = radiolist_dialog(
"API type",
text="Which api type do you want?",
values=[(api, api.value) for api in list(APIType)],
).run()
if current_context.api_type is None:
raise KeyboardInterrupt()
if current_context.db is None:
current_context.db = radiolist_dialog(
"Databases",
Expand Down
14 changes: 11 additions & 3 deletions fastapi_template/input_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from pydantic import BaseModel


@enum.unique
class APIType(enum.Enum):
rest = "rest"
graphql = "graphql"

@enum.unique
class DatabaseType(enum.Enum):
none = "none"
Expand All @@ -25,6 +30,7 @@ class ORM(enum.Enum):
sqlalchemy = "sqlalchemy"
tortoise = "tortoise"
psycopg = "psycopg"
piccolo = "piccolo"


class Database(BaseModel):
Expand All @@ -46,15 +52,15 @@ class Database(BaseModel):
),
DatabaseType.postgresql: Database(
name=DatabaseType.postgresql.value,
image="postgres:13.4-buster",
image="postgres:13.6-bullseye",
async_driver="postgresql+asyncpg",
driver_short="postgres",
driver="postgresql",
port=5432,
),
DatabaseType.mysql: Database(
name=DatabaseType.mysql.value,
image="bitnami/mysql:8.0.26",
image="bitnami/mysql:8.0.28",
async_driver="mysql+aiomysql",
driver_short="mysql",
driver="mysql",
Expand All @@ -76,11 +82,13 @@ class Database(BaseModel):
ORM.psycopg,
ORM.tortoise,
ORM.sqlalchemy,
ORM.piccolo,
],
DatabaseType.sqlite: [
ORM.ormar,
ORM.tortoise,
ORM.sqlalchemy,
ORM.piccolo,
],
DatabaseType.mysql: [
ORM.ormar,
Expand All @@ -95,7 +103,7 @@ class Database(BaseModel):

class BuilderContext(BaseModel):
"""Options for project generation."""

api_type: Optional[APIType]
project_name: Optional[str]
kube_name: Optional[str]
project_description: Optional[str]
Expand Down
3 changes: 3 additions & 0 deletions fastapi_template/template/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"project_description": {
"type": "string"
},
"api_type": {
"type": "dict"
},
"db_info": {
"type": "dict"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ ignore =
D106,
; First line should be in imperative mood
D401,
; Found wrong variable name
WPS110,
; Found `__init__.py` module with logic
WPS326,
; Found string constant over-use
Expand Down Expand Up @@ -64,6 +66,8 @@ ignore =
DJ10,
; Model should define verbose_name_plural in its Meta inner class
DJ11,
; Found mutable module constant.
WPS407,

per-file-ignores =
; all tests
Expand Down
22 changes: 22 additions & 0 deletions fastapi_template/template/{{cookiecutter.project_name}}/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
# {{cookiecutter.project_name}}


Start a project with:

```bash
docker-compose -f deploy/docker-compose.yml --project-directory . up
```

If you want to develop in docker with autoreload, use this command:

```bash
docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up
```

This command exposes application on port 8000, mounts current directory and enables autoreload.

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

```bash
docker-compose -f deploy/docker-compose.yml --project-directory . build
```


## Pre-commit

To install pre-commit simply run inside the shell:
Expand Down Expand Up @@ -46,6 +62,12 @@ alembic upgrade "head"
{%- elif cookiecutter.orm == 'tortoise' %}
# Upgrade database to the last migration.
aerich upgrade

{%- elif cookiecutter.orm == 'piccolo' %}
# You have to set a PICCOLO_CONF variable
export PICCOLO_CONF="{{cookiecutter.project_name}}.piccolo_conf"
# Now you can easily run migrations usuing
piccolo migrations forwards all
{%- endif %}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
{
"GraphQL API": {
"enabled": "{{cookiecutter.api_type == 'graphql'}}",
"resources": [
"{{cookiecutter.project_name}}/web/gql"
]
},
"REST API": {
"enabled": "{{cookiecutter.api_type == 'rest'}}",
"resources": [
"{{cookiecutter.project_name}}/web/api/dummy",
"{{cookiecutter.project_name}}/web/api/echo",
"{{cookiecutter.project_name}}/web/api/redis"
]
},
"Redis": {
"enabled": "{{cookiecutter.enable_redis}}",
"resources": [
"{{cookiecutter.project_name}}/web/api/redis",
"{{cookiecutter.project_name}}/web/gql/redis",
"{{cookiecutter.project_name}}/services/redis",
"{{cookiecutter.project_name}}/tests/test_redis.py",
"deploy/kube/redis.yml"
Expand All @@ -20,6 +35,7 @@
"aerich.ini",
"alembic.ini",
"{{cookiecutter.project_name}}/web/api/dummy",
"{{cookiecutter.project_name}}/web/gql/dummy",
"{{cookiecutter.project_name}}/db_sa",
"{{cookiecutter.project_name}}/tests/test_dummy.py",
"deploy/kube/db.yml"
Expand All @@ -38,7 +54,8 @@
"alembic.ini",
"{{cookiecutter.project_name}}/db_sa/migrations",
"{{cookiecutter.project_name}}/db_ormar/migrations",
"{{cookiecutter.project_name}}/db_tortoise/migrations"
"{{cookiecutter.project_name}}/db_tortoise/migrations",
"{{cookiecutter.project_name}}/db_piccolo/migrations"
]
},
"Alembic migrations": {
Expand Down Expand Up @@ -83,11 +100,14 @@
"{{cookiecutter.project_name}}/db_psycopg/dao",
"{{cookiecutter.project_name}}/db_psycopg/models/dummy_model.py",
"{{cookiecutter.project_name}}/tests/test_dummy.py",
"{{cookiecutter.project_name}}/db_piccolo/dao",
"{{cookiecutter.project_name}}/db_piccolo/models/dummy_model.py",
"{{cookiecutter.project_name}}/db_sa/migrations/versions/2021-08-16-16-55_2b7380507a71.py",
"{{cookiecutter.project_name}}/db_ormar/migrations/versions/2021-08-16-16-55_2b7380507a71.py",
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_pg.sql",
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_mysql.sql",
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql"
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql",
"{{cookiecutter.project_name}}/db_piccolo/migrations/2022-04-16T17-38-51-672827.py"
]
},
"Self-hosted swagger": {
Expand Down Expand Up @@ -122,6 +142,13 @@
"{{cookiecutter.project_name}}/db_psycopg"
]
},
"Piccolo": {
"enabled": "{{cookiecutter.orm == 'piccolo'}}",
"resources": [
"{{cookiecutter.project_name}}/db_piccolo",
"{{cookiecutter.project_name}}/piccolo_conf.py"
]
},
"Postgresql DB": {
"enabled": "{{cookiecutter.db_info.name == 'postgresql'}}",
"resources": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.9'

services:
api:
ports:
- "8000:8000"
volumes:
- .:/app/src/
environment:
{{cookiecutter.project_name | upper}}_RELOAD: "True"
Loading

0 comments on commit 465bbf3

Please sign in to comment.