Skip to content

Commit

Permalink
feat: add fastapi recipe. (#278)
Browse files Browse the repository at this point in the history
* feat: add fastapi recipe.
* [pre-commit.ci] auto fixes from pre-commit.com hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
lucas-six and pre-commit-ci[bot] authored Mar 24, 2024
1 parent 03751a1 commit df62a45
Show file tree
Hide file tree
Showing 39 changed files with 15,513 additions and 413 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
.pytest_cache
.coverage

### vscode ###
.vscode
.env

playground/
15 changes: 12 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
exclude: .vscode/
- id: check-toml
- id: check-added-large-files
args: ['--maxkb=500']
Expand Down Expand Up @@ -47,11 +48,19 @@ repos:
rev: v3.1.0
hooks:
- id: pylint
additional_dependencies: [pydantic, pylint-pydantic]
additional_dependencies:
[
pydantic,
pydantic-settings,
pylint-pydantic,
fastapi,
uvicorn,
motor,
redis,
types-redis,
]
exclude: django_project/
language_version: python3.11
args:
- '--load-plugins=pylint_pydantic'
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.1
hooks:
Expand Down
50 changes: 50 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: FastAPI",
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"args": [
"--host",
"0.0.0.0",
"examples.web.fastapi.main:app",
"--reload"
],
"jinja": true,
"cwd": "${workspaceFolder}",
},
{
"name": "FastAPI: OpenAPI with Custom Static Files",
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"args": [
"--host",
"0.0.0.0",
"examples.web.fastapi.main_openapi_static:app",
"--reload"
],
"jinja": true,
"cwd": "${workspaceFolder}",
},
{
"name": "FastAPI: MongoDB",
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"args": [
"--host",
"0.0.0.0",
"examples.web.fastapi.main_mongodb:app",
"--reload"
],
"jinja": true,
"cwd": "${workspaceFolder}",
}
]
}
32 changes: 32 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"files.exclude": {
"**/__pycache__": true,
"**/.mypy_cache": true
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.fixAll": "explicit"
}
},
"python.analysis.importFormat": "absolute",
"python.analysis.typeCheckingMode": "basic",
"black-formatter.args": [
"--line-length=88",
"--skip-string-normalization"
],
"isort.args": [
"--profile=black"
],
"isort.severity": {
"E": "Error"
},
"pylint.args": [
"--load-plugins=pylint.extensions.bad_builtin,pylint_pydantic",
"--ignore=CVS,.git,__pycache__,.mypy_cache,tests",
"--disable=no-self-argument",
],
"python.testing.pytestEnabled": false
}
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ name = "pypi"

[packages]
pydantic = "*"
fastapi = {version = "*", extras = ["all"]}
uvicorn = {version = "*", extras = ["standard"]}
redis = {version = "*", extras = ["hiredis"]}
types-redis = "*"
#uvloop = {version = "*", markers = "sys_platform != 'win32'"}
Expand All @@ -13,6 +15,7 @@ celery = {version = "*", extras = ["librabbitmq", "mongodb", "redis"]}
#types-requests = "*"
#django = "~=4.2"
#psycopg = {version = ">=3.2", extras = ["binary", "pool"]}
motor = "*"

[dev-packages]
black = "*"
Expand Down
766 changes: 691 additions & 75 deletions Pipfile.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@
### Project

- [Project: `pyproject.toml`](https://lucas-six.github.io/python-cookbook/cookbook/build/project)
- [FastAPI Project](https://lucas-six.github.io/python-cookbook/cookbook/build/project_fastapi)
- `black`
- `isort`
- `mypy`
Expand Down Expand Up @@ -193,11 +192,10 @@
- [Cache - Redis](https://lucas-six.github.io/python-cookbook/cookbook/web/django_cache_redis)
- [Logging](https://lucas-six.github.io/python-cookbook/cookbook/web/django_logging)
- [`Flask`](https://flask.palletsprojects.com/): tiny
- [**`FastAPI`**](https://fastapi.tiangolo.com/)
- [**`Starlette`**](https://www.starlette.io/): *ASGI*
- [**`Pydantic`**](https://pydantic-docs.helpmanual.io/): data validation
- [*`Swagger`*](https://swagger.io/): *OpenAPI*
- type annotation
- **`FastAPI`**: *ASGI* + type annotations + data model + *OpenAPI*(*Swagger UI*)
- [Quick Start](https://lucas-six.github.io/python-cookbook/cookbook/web/fastapi/fastapi_quickstart)
- [Data Model: **`Pydantic`**](https://lucas-six.github.io/python-cookbook/cookbook/web/pydantic)
- [with MongoDB: **`motor`**](https://lucas-six.github.io/python-cookbook/cookbook/web/fastapi/fastapi_mongodb)

### Web Server

Expand All @@ -217,8 +215,10 @@

- [Official Driver](https://www.mongodb.com/docs/drivers/python/)
- [`pymongo`: Sync](https://www.mongodb.com/docs/drivers/pymongo/)
- [**`motor`**: Async](https://www.mongodb.com/docs/drivers/motor/)
- [`Beanie`: Async ODM (based on `Pydantic`)](https://beanie-odm.dev/)
- [**`motor`**: Async](https://lucas-six.github.io/python-cookbook/cookbook/system_services/mongodb_motor)
- ODM
- [`Beanie`: Async ODM (based on `Pydantic`)](https://beanie-odm.dev/)
- [`PyODMongo`: Async ODM (based on `Pydantic V2`)](https://pyodmongo.dev/)

### Redis

Expand Down
19 changes: 18 additions & 1 deletion cookbook/build/deploy/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sed -i "1d" requirements.txt

```Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.10
FROM python:3.11

ARG PYPI_INDEX_URL=https://pypi.org/simple
ARG PYPI_TRUST_HOST=pypi.org
Expand All @@ -34,6 +34,23 @@ RUN python -m black . \
&& python -m pylint .
```

OR

```Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.11-alpine

ARG PYPI_INDEX_URL=https://pypi.org/simple
ARG PYPI_TRUST_HOST=pypi.org
ARG PYPI_TIMEOUT=300

# ENV PYTHONUNBUFFERD 1

# Aliyun mirrors
#RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
#RUN apk update && apk add git curl
```

```bash
docker build --network=host -t <DOCKER-IMAGE-NAME> . --build-arg PYPI_INDEX_URL=<PYPI_INDEX_URL> --build-arg PYPI_TRUST_HOST=<PYPI_TRUST_HOST>
docker run --rm <DOCKER-IMAGE-NAME> /bin/sh -c echo lint finished
Expand Down
27 changes: 16 additions & 11 deletions cookbook/build/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ classifiers = [
dependencies = [
"psycopg2 >= 2.8",
"redis >= 4.0",
"types-redis",

"requests >=2.6",
"configparser; python_version == '2.7'",
Expand Down Expand Up @@ -97,6 +98,7 @@ profile = "black"
skip_gitignore = true
extend_skip = [".gitignore", ".env", ".dockerignore"]
# skip_glob = []
extend_skip_glob = []

[tool.mypy]
python_version = "3.11"
Expand All @@ -107,23 +109,21 @@ follow_imports = "silent"
warn_redundant_casts = true
warn_unused_ignores = true
warn_unused_configs = true
disallow_any_generics = true
disallow_any_generics = false
check_untyped_defs = true
no_implicit_reexport = true
disallow_untyped_defs = true

# mypy for MongoDB motor
[[tool.mypy.overrides]]
module = "motor.*"
ignore_missing_imports = true

[tool.pylint.main]
recursive = true
py-version = 3.11
jobs = 0
ignore = "CVS,.git,__pycache__,.mypy_cache,tests"
ignore-paths = "tests"
ignore-patterns = "test_.*.py"
load-plugins = [
"pylint.extensions.bad_builtin",
]

[tool.pylint.'FORMAT']
max-line-length = 88
Expand All @@ -141,16 +141,22 @@ disable = [
"useless-suppression",
"deprecated-pragma",
"use-symbolic-message-instead",
"logging-fstring-interpolation"
"logging-fstring-interpolation",
#"missing-class-docstring",
#"missing-function-docstring",
#"duplicate-code",
]
enable = [
"c-extension-no-member",
]

[tool.pylint.design]
max-args = 12
min-public-methods = 1
max-locals = 22
max-args = 15
min-public-methods = 0
max-locals = 25

[tool.pylint.deprecated_builtins]
bad-functions = ["map", "filter"]

[tool.pyright]
include = [
Expand Down Expand Up @@ -209,7 +215,6 @@ ignore = [
## More

- [`pipenv` - Python Cookbook](pkg/pipenv)
- [FastAPI Project](project_fastapi)

## References

Expand Down
Loading

0 comments on commit df62a45

Please sign in to comment.