1185 changed files with 117588 additions and 38010 deletions
@ -1,5 +0,0 @@ |
|||
[flake8] |
|||
max-line-length = 88 |
|||
select = C,E,F,W,B,B9 |
|||
ignore = E203, E501, W503 |
|||
exclude = __init__.py |
@ -2,3 +2,15 @@ blank_issues_enabled: false |
|||
contact_links: |
|||
- name: Security Contact |
|||
about: Please report security vulnerabilities to [email protected] |
|||
- name: Question or Problem |
|||
about: Ask a question or ask about a problem in GitHub Discussions. |
|||
url: https://github.com/tiangolo/fastapi/discussions/categories/questions |
|||
- name: Feature Request |
|||
about: To suggest an idea or ask about a feature, please start with a question saying what you would like to achieve. There might be a way to do it already. |
|||
url: https://github.com/tiangolo/fastapi/discussions/categories/questions |
|||
- name: Show and tell |
|||
about: Show what you built with FastAPI or to be used with FastAPI. |
|||
url: https://github.com/tiangolo/fastapi/discussions/categories/show-and-tell |
|||
- name: Translations |
|||
about: Coordinate translations in GitHub Discussions. |
|||
url: https://github.com/tiangolo/fastapi/discussions/categories/translations |
|||
|
@ -1,181 +0,0 @@ |
|||
name: Feature Request |
|||
description: Suggest an idea or ask for a feature that you would like to have in FastAPI |
|||
labels: [enhancement] |
|||
body: |
|||
- type: markdown |
|||
attributes: |
|||
value: | |
|||
Thanks for your interest in FastAPI! ๐ |
|||
|
|||
Please follow these instructions, fill every question, and do every step. ๐ |
|||
|
|||
I'm asking this because answering questions and solving problems in GitHub issues is what consumes most of the time. |
|||
|
|||
I end up not being able to add new features, fix bugs, review pull requests, etc. as fast as I wish because I have to spend too much time handling issues. |
|||
|
|||
All that, on top of all the incredible help provided by a bunch of community members, the [FastAPI Experts](https://fastapi.tiangolo.com/fastapi-people/#experts), that give a lot of their time to come here and help others. |
|||
|
|||
That's a lot of work they are doing, but if more FastAPI users came to help others like them just a little bit more, it would be much less effort for them (and you and me ๐
). |
|||
|
|||
By asking questions in a structured way (following this) it will be much easier to help you. |
|||
|
|||
And there's a high chance that you will find the solution along the way and you won't even have to submit it and wait for an answer. ๐ |
|||
|
|||
As there are too many issues with questions, I'll have to close the incomplete ones. That will allow me (and others) to focus on helping people like you that follow the whole process and help us help you. ๐ค |
|||
- type: checkboxes |
|||
id: checks |
|||
attributes: |
|||
label: First Check |
|||
description: Please confirm and check all the following options. |
|||
options: |
|||
- label: I added a very descriptive title to this issue. |
|||
required: true |
|||
- label: I used the GitHub search to find a similar issue and didn't find it. |
|||
required: true |
|||
- label: I searched the FastAPI documentation, with the integrated search. |
|||
required: true |
|||
- label: I already searched in Google "How to X in FastAPI" and didn't find any information. |
|||
required: true |
|||
- label: I already read and followed all the tutorial in the docs and didn't find an answer. |
|||
required: true |
|||
- label: I already checked if it is not related to FastAPI but to [Pydantic](https://github.com/samuelcolvin/pydantic). |
|||
required: true |
|||
- label: I already checked if it is not related to FastAPI but to [Swagger UI](https://github.com/swagger-api/swagger-ui). |
|||
required: true |
|||
- label: I already checked if it is not related to FastAPI but to [ReDoc](https://github.com/Redocly/redoc). |
|||
required: true |
|||
- type: checkboxes |
|||
id: help |
|||
attributes: |
|||
label: Commit to Help |
|||
description: | |
|||
After submitting this, I commit to one of: |
|||
|
|||
* Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there. |
|||
* I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future. |
|||
* Implement a Pull Request for a confirmed bug. |
|||
|
|||
options: |
|||
- label: I commit to help with one of those options ๐ |
|||
required: true |
|||
- type: textarea |
|||
id: example |
|||
attributes: |
|||
label: Example Code |
|||
description: | |
|||
Please add a self-contained, [minimal, reproducible, example](https://stackoverflow.com/help/minimal-reproducible-example) with your use case. |
|||
|
|||
If I (or someone) can copy it, run it, and see it right away, there's a much higher chance I (or someone) will be able to help you. |
|||
|
|||
placeholder: | |
|||
from fastapi import FastAPI |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.get("/") |
|||
def read_root(): |
|||
return {"Hello": "World"} |
|||
render: python |
|||
validations: |
|||
required: true |
|||
- type: textarea |
|||
id: description |
|||
attributes: |
|||
label: Description |
|||
description: | |
|||
What is your feature request? |
|||
|
|||
Write a short description telling me what you are trying to solve and what you are currently doing. |
|||
placeholder: | |
|||
* Open the browser and call the endpoint `/`. |
|||
* It returns a JSON with `{"Hello": "World"}`. |
|||
* I would like it to have an extra parameter to teleport me to the moon and back. |
|||
validations: |
|||
required: true |
|||
- type: textarea |
|||
id: wanted-solution |
|||
attributes: |
|||
label: Wanted Solution |
|||
description: | |
|||
Tell me what's the solution you would like. |
|||
placeholder: | |
|||
I would like it to have a `teleport_to_moon` parameter that defaults to `False`, and can be set to `True` to teleport me. |
|||
validations: |
|||
required: true |
|||
- type: textarea |
|||
id: wanted-code |
|||
attributes: |
|||
label: Wanted Code |
|||
description: Show me an example of how you would want the code to look like. |
|||
placeholder: | |
|||
from fastapi import FastAPI |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.get("/", teleport_to_moon=True) |
|||
def read_root(): |
|||
return {"Hello": "World"} |
|||
render: python |
|||
validations: |
|||
required: true |
|||
- type: textarea |
|||
id: alternatives |
|||
attributes: |
|||
label: Alternatives |
|||
description: | |
|||
Tell me about alternatives you've considered. |
|||
placeholder: | |
|||
To wait for Space X moon travel plans to drop down long after they release them. But I would rather teleport. |
|||
- type: dropdown |
|||
id: os |
|||
attributes: |
|||
label: Operating System |
|||
description: What operating system are you on? |
|||
multiple: true |
|||
options: |
|||
- Linux |
|||
- Windows |
|||
- macOS |
|||
- Other |
|||
validations: |
|||
required: true |
|||
- type: textarea |
|||
id: os-details |
|||
attributes: |
|||
label: Operating System Details |
|||
description: You can add more details about your operating system here, in particular if you chose "Other". |
|||
- type: input |
|||
id: fastapi-version |
|||
attributes: |
|||
label: FastAPI Version |
|||
description: | |
|||
What FastAPI version are you using? |
|||
|
|||
You can find the FastAPI version with: |
|||
|
|||
```bash |
|||
python -c "import fastapi; print(fastapi.__version__)" |
|||
``` |
|||
validations: |
|||
required: true |
|||
- type: input |
|||
id: python-version |
|||
attributes: |
|||
label: Python Version |
|||
description: | |
|||
What Python version are you using? |
|||
|
|||
You can find the Python version with: |
|||
|
|||
```bash |
|||
python --version |
|||
``` |
|||
validations: |
|||
required: true |
|||
- type: textarea |
|||
id: context |
|||
attributes: |
|||
label: Additional Context |
|||
description: Add any additional context information or screenshots you think are useful. |
@ -0,0 +1,22 @@ |
|||
name: Privileged |
|||
description: You are @tiangolo or he asked you directly to create an issue here. If not, check the other options. ๐ |
|||
body: |
|||
- type: markdown |
|||
attributes: |
|||
value: | |
|||
Thanks for your interest in FastAPI! ๐ |
|||
|
|||
If you are not @tiangolo or he didn't ask you directly to create an issue here, please start the conversation in a [Question in GitHub Discussions](https://github.com/tiangolo/fastapi/discussions/categories/questions) instead. |
|||
- type: checkboxes |
|||
id: privileged |
|||
attributes: |
|||
label: Privileged issue |
|||
description: Confirm that you are allowed to create an issue here. |
|||
options: |
|||
- label: I'm @tiangolo or he asked me directly to create an issue here. |
|||
required: true |
|||
- type: textarea |
|||
id: content |
|||
attributes: |
|||
label: Issue Content |
|||
description: Add the content of the issue here. |
@ -1,19 +0,0 @@ |
|||
pt: 1211 |
|||
es: 1218 |
|||
zh: 1228 |
|||
ru: 1362 |
|||
it: 1556 |
|||
ja: 1572 |
|||
uk: 1748 |
|||
tr: 1892 |
|||
fr: 1972 |
|||
ko: 2017 |
|||
fa: 2041 |
|||
pl: 3169 |
|||
de: 3716 |
|||
id: 3717 |
|||
az: 3994 |
|||
nl: 4701 |
|||
uz: 4883 |
|||
sv: 5146 |
|||
he: 5157 |
@ -3,10 +3,7 @@ description: "Generate the data for the FastAPI People page" |
|||
author: "Sebastiรกn Ramรญrez <[email protected]>" |
|||
inputs: |
|||
token: |
|||
description: 'User token, to read the GitHub API. Can be passed in using {{ secrets.ACTION_TOKEN }}' |
|||
required: true |
|||
standard_token: |
|||
description: 'Default GitHub Action token, used for the PR. Can be passed in using {{ secrets.GITHUB_TOKEN }}' |
|||
description: 'User token, to read the GitHub API. Can be passed in using {{ secrets.FASTAPI_PEOPLE }}' |
|||
required: true |
|||
runs: |
|||
using: 'docker' |
|||
|
@ -1,7 +0,0 @@ |
|||
FROM python:3.7 |
|||
|
|||
RUN pip install httpx PyGithub "pydantic==1.5.1" |
|||
|
|||
COPY ./app /app |
|||
|
|||
CMD ["python", "/app/main.py"] |
@ -1,10 +0,0 @@ |
|||
name: "Watch docs previews in PRs" |
|||
description: "Check PRs and trigger new docs deploys" |
|||
author: "Sebastiรกn Ramรญrez <[email protected]>" |
|||
inputs: |
|||
token: |
|||
description: 'Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}' |
|||
required: true |
|||
runs: |
|||
using: 'docker' |
|||
image: 'Dockerfile' |
@ -1,101 +0,0 @@ |
|||
import logging |
|||
from datetime import datetime |
|||
from pathlib import Path |
|||
from typing import List, Union |
|||
|
|||
import httpx |
|||
from github import Github |
|||
from github.NamedUser import NamedUser |
|||
from pydantic import BaseModel, BaseSettings, SecretStr |
|||
|
|||
github_api = "https://api.github.com" |
|||
netlify_api = "https://api.netlify.com" |
|||
|
|||
|
|||
class Settings(BaseSettings): |
|||
input_token: SecretStr |
|||
github_repository: str |
|||
github_event_path: Path |
|||
github_event_name: Union[str, None] = None |
|||
|
|||
|
|||
class Artifact(BaseModel): |
|||
id: int |
|||
node_id: str |
|||
name: str |
|||
size_in_bytes: int |
|||
url: str |
|||
archive_download_url: str |
|||
expired: bool |
|||
created_at: datetime |
|||
updated_at: datetime |
|||
|
|||
|
|||
class ArtifactResponse(BaseModel): |
|||
total_count: int |
|||
artifacts: List[Artifact] |
|||
|
|||
|
|||
def get_message(commit: str) -> str: |
|||
return f"Docs preview for commit {commit} at" |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
logging.basicConfig(level=logging.INFO) |
|||
settings = Settings() |
|||
logging.info(f"Using config: {settings.json()}") |
|||
g = Github(settings.input_token.get_secret_value()) |
|||
repo = g.get_repo(settings.github_repository) |
|||
owner: NamedUser = repo.owner |
|||
headers = {"Authorization": f"token {settings.input_token.get_secret_value()}"} |
|||
prs = list(repo.get_pulls(state="open")) |
|||
response = httpx.get( |
|||
f"{github_api}/repos/{settings.github_repository}/actions/artifacts", |
|||
headers=headers, |
|||
) |
|||
data = response.json() |
|||
artifacts_response = ArtifactResponse.parse_obj(data) |
|||
for pr in prs: |
|||
logging.info("-----") |
|||
logging.info(f"Processing PR #{pr.number}: {pr.title}") |
|||
pr_comments = list(pr.get_issue_comments()) |
|||
pr_commits = list(pr.get_commits()) |
|||
last_commit = pr_commits[0] |
|||
for pr_commit in pr_commits: |
|||
if pr_commit.commit.author.date > last_commit.commit.author.date: |
|||
last_commit = pr_commit |
|||
commit = last_commit.commit.sha |
|||
logging.info(f"Last commit: {commit}") |
|||
message = get_message(commit) |
|||
notified = False |
|||
for pr_comment in pr_comments: |
|||
if message in pr_comment.body: |
|||
notified = True |
|||
logging.info(f"Docs preview was notified: {notified}") |
|||
if not notified: |
|||
artifact_name = f"docs-zip-{commit}" |
|||
use_artifact: Union[Artifact, None] = None |
|||
for artifact in artifacts_response.artifacts: |
|||
if artifact.name == artifact_name: |
|||
use_artifact = artifact |
|||
break |
|||
if not use_artifact: |
|||
logging.info("Artifact not available") |
|||
else: |
|||
logging.info(f"Existing artifact: {use_artifact.name}") |
|||
response = httpx.post( |
|||
"https://api.github.com/repos/tiangolo/fastapi/actions/workflows/preview-docs.yml/dispatches", |
|||
headers=headers, |
|||
json={ |
|||
"ref": "master", |
|||
"inputs": { |
|||
"pr": f"{pr.number}", |
|||
"name": artifact_name, |
|||
"commit": commit, |
|||
}, |
|||
}, |
|||
) |
|||
logging.info( |
|||
f"Trigger sent, response status: {response.status_code} - content: {response.content}" |
|||
) |
|||
logging.info("Finished") |
@ -4,10 +4,68 @@ on: |
|||
branches: |
|||
- master |
|||
pull_request: |
|||
types: [opened, synchronize] |
|||
types: |
|||
- opened |
|||
- synchronize |
|||
jobs: |
|||
changes: |
|||
runs-on: ubuntu-latest |
|||
# Required permissions |
|||
permissions: |
|||
pull-requests: read |
|||
# Set job outputs to values from filter step |
|||
outputs: |
|||
docs: ${{ steps.filter.outputs.docs }} |
|||
steps: |
|||
- uses: actions/checkout@v3 |
|||
# For pull requests it's not necessary to checkout the code but for master it is |
|||
- uses: dorny/paths-filter@v2 |
|||
id: filter |
|||
with: |
|||
filters: | |
|||
docs: |
|||
- README.md |
|||
- docs/** |
|||
- docs_src/** |
|||
- requirements-docs.txt |
|||
langs: |
|||
needs: |
|||
- changes |
|||
runs-on: ubuntu-latest |
|||
outputs: |
|||
langs: ${{ steps.show-langs.outputs.langs }} |
|||
steps: |
|||
- uses: actions/checkout@v3 |
|||
- name: Set up Python |
|||
uses: actions/setup-python@v4 |
|||
with: |
|||
python-version: "3.11" |
|||
- uses: actions/cache@v3 |
|||
id: cache |
|||
with: |
|||
path: ${{ env.pythonLocation }} |
|||
key: ${{ runner.os }}-python-docs-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'requirements-docs.txt') }}-v06 |
|||
- name: Install docs extras |
|||
if: steps.cache.outputs.cache-hit != 'true' |
|||
run: pip install -r requirements-docs.txt |
|||
# Install MkDocs Material Insiders here just to put it in the cache for the rest of the steps |
|||
- name: Install Material for MkDocs Insiders |
|||
if: ( github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false ) && steps.cache.outputs.cache-hit != 'true' |
|||
run: pip install git+https://${{ secrets.FASTAPI_MKDOCS_MATERIAL_INSIDERS }}@github.com/squidfunk/mkdocs-material-insiders.git |
|||
- name: Export Language Codes |
|||
id: show-langs |
|||
run: | |
|||
echo "langs=$(python ./scripts/docs.py langs-json)" >> $GITHUB_OUTPUT |
|||
|
|||
build-docs: |
|||
runs-on: ubuntu-18.04 |
|||
needs: |
|||
- changes |
|||
- langs |
|||
if: ${{ needs.changes.outputs.docs == 'true' }} |
|||
runs-on: ubuntu-latest |
|||
strategy: |
|||
matrix: |
|||
lang: ${{ fromJson(needs.langs.outputs.langs) }} |
|||
steps: |
|||
- name: Dump GitHub context |
|||
env: |
|||
@ -17,36 +75,40 @@ jobs: |
|||
- name: Set up Python |
|||
uses: actions/setup-python@v4 |
|||
with: |
|||
python-version: "3.7" |
|||
python-version: "3.11" |
|||
- uses: actions/cache@v3 |
|||
id: cache |
|||
with: |
|||
path: ${{ env.pythonLocation }} |
|||
key: ${{ runner.os }}-python-docs-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-v03 |
|||
- name: Install Flit |
|||
if: steps.cache.outputs.cache-hit != 'true' |
|||
run: python3.7 -m pip install flit |
|||
key: ${{ runner.os }}-python-docs-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'requirements-docs.txt') }}-v06 |
|||
- name: Install docs extras |
|||
if: steps.cache.outputs.cache-hit != 'true' |
|||
run: python3.7 -m flit install --deps production --extras doc |
|||
run: pip install -r requirements-docs.txt |
|||
- name: Install Material for MkDocs Insiders |
|||
if: ( github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false ) && steps.cache.outputs.cache-hit != 'true' |
|||
run: pip install git+https://${{ secrets.ACTIONS_TOKEN }}@github.com/squidfunk/mkdocs-material-insiders.git |
|||
run: pip install git+https://${{ secrets.FASTAPI_MKDOCS_MATERIAL_INSIDERS }}@github.com/squidfunk/mkdocs-material-insiders.git |
|||
- name: Update Languages |
|||
run: python ./scripts/docs.py update-languages |
|||
- uses: actions/cache@v3 |
|||
with: |
|||
key: mkdocs-cards-${{ matrix.lang }}-${{ github.ref }} |
|||
path: docs/${{ matrix.lang }}/.cache |
|||
- name: Build Docs |
|||
run: python3.7 ./scripts/docs.py build-all |
|||
- name: Zip docs |
|||
run: bash ./scripts/zip-docs.sh |
|||
run: python ./scripts/docs.py build-lang ${{ matrix.lang }} |
|||
- uses: actions/upload-artifact@v3 |
|||
with: |
|||
name: docs-zip |
|||
path: ./docs.zip |
|||
- name: Deploy to Netlify |
|||
uses: nwtgck/[email protected] |
|||
name: docs-site |
|||
path: ./site/** |
|||
|
|||
# https://github.com/marketplace/actions/alls-green#why |
|||
docs-all-green: # This job does nothing and is only used for the branch protection |
|||
if: always() |
|||
needs: |
|||
- build-docs |
|||
runs-on: ubuntu-latest |
|||
steps: |
|||
- name: Decide whether the needed jobs succeeded or failed |
|||
uses: re-actors/alls-green@release/v1 |
|||
with: |
|||
publish-dir: './site' |
|||
production-branch: master |
|||
github-token: ${{ secrets.GITHUB_TOKEN }} |
|||
enable-commit-comment: false |
|||
env: |
|||
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
|||
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
|||
jobs: ${{ toJSON(needs) }} |
|||
allowed-skips: build-docs |
|||
|
@ -0,0 +1,48 @@ |
|||
name: Deploy Docs |
|||
on: |
|||
workflow_run: |
|||
workflows: |
|||
- Build Docs |
|||
types: |
|||
- completed |
|||
|
|||
jobs: |
|||
deploy-docs: |
|||
runs-on: ubuntu-latest |
|||
steps: |
|||
- name: Dump GitHub context |
|||
env: |
|||
GITHUB_CONTEXT: ${{ toJson(github) }} |
|||
run: echo "$GITHUB_CONTEXT" |
|||
- uses: actions/checkout@v3 |
|||
- name: Clean site |
|||
run: | |
|||
rm -rf ./site |
|||
mkdir ./site |
|||
- name: Download Artifact Docs |
|||
id: download |
|||
uses: dawidd6/[email protected] |
|||
with: |
|||
if_no_artifact_found: ignore |
|||
github_token: ${{ secrets.FASTAPI_PREVIEW_DOCS_DOWNLOAD_ARTIFACTS }} |
|||
workflow: build-docs.yml |
|||
run_id: ${{ github.event.workflow_run.id }} |
|||
name: docs-site |
|||
path: ./site/ |
|||
- name: Deploy to Cloudflare Pages |
|||
if: steps.download.outputs.found_artifact == 'true' |
|||
id: deploy |
|||
uses: cloudflare/pages-action@v1 |
|||
with: |
|||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
|||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
|||
projectName: fastapitiangolo |
|||
directory: './site' |
|||
gitHubToken: ${{ secrets.GITHUB_TOKEN }} |
|||
branch: ${{ ( github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch == 'master' && 'main' ) || ( github.event.workflow_run.head_sha ) }} |
|||
- name: Comment Deploy |
|||
if: steps.deploy.outputs.url != '' |
|||
uses: ./.github/actions/comment-docs-preview-in-pr |
|||
with: |
|||
token: ${{ secrets.FASTAPI_PREVIEW_DOCS_COMMENT_DEPLOY }} |
|||
deploy_url: "${{ steps.deploy.outputs.url }}" |
@ -2,7 +2,7 @@ name: Issue Manager |
|||
|
|||
on: |
|||
schedule: |
|||
- cron: "0 0 * * *" |
|||
- cron: "10 3 * * *" |
|||
issue_comment: |
|||
types: |
|||
- created |
|||
@ -16,11 +16,16 @@ on: |
|||
|
|||
jobs: |
|||
issue-manager: |
|||
if: github.repository_owner == 'tiangolo' |
|||
runs-on: ubuntu-latest |
|||
steps: |
|||
- name: Dump GitHub context |
|||
env: |
|||
GITHUB_CONTEXT: ${{ toJson(github) }} |
|||
run: echo "$GITHUB_CONTEXT" |
|||
- uses: tiangolo/[email protected] |
|||
with: |
|||
token: ${{ secrets.GITHUB_TOKEN }} |
|||
token: ${{ secrets.FASTAPI_ISSUE_MANAGER }} |
|||
config: > |
|||
{ |
|||
"answered": { |
|||
|
@ -1,41 +0,0 @@ |
|||
name: Preview Docs |
|||
on: |
|||
workflow_run: |
|||
workflows: |
|||
- Build Docs |
|||
types: |
|||
- completed |
|||
|
|||
jobs: |
|||
preview-docs: |
|||
runs-on: ubuntu-latest |
|||
steps: |
|||
- uses: actions/checkout@v3 |
|||
- name: Download Artifact Docs |
|||
uses: dawidd6/[email protected] |
|||
with: |
|||
github_token: ${{ secrets.GITHUB_TOKEN }} |
|||
workflow: build-docs.yml |
|||
run_id: ${{ github.event.workflow_run.id }} |
|||
name: docs-zip |
|||
- name: Unzip docs |
|||
run: | |
|||
rm -rf ./site |
|||
unzip docs.zip |
|||
rm -f docs.zip |
|||
- name: Deploy to Netlify |
|||
id: netlify |
|||
uses: nwtgck/[email protected] |
|||
with: |
|||
publish-dir: './site' |
|||
production-deploy: false |
|||
github-token: ${{ secrets.GITHUB_TOKEN }} |
|||
enable-commit-comment: false |
|||
env: |
|||
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
|||
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
|||
- name: Comment Deploy |
|||
uses: ./.github/actions/comment-docs-preview-in-pr |
|||
with: |
|||
token: ${{ secrets.GITHUB_TOKEN }} |
|||
deploy_url: "${{ steps.netlify.outputs.deploy-url }}" |
@ -17,30 +17,25 @@ jobs: |
|||
- name: Set up Python |
|||
uses: actions/setup-python@v4 |
|||
with: |
|||
python-version: "3.6" |
|||
python-version: "3.7" |
|||
# Issue ref: https://github.com/actions/setup-python/issues/436 |
|||
# cache: "pip" |
|||
cache-dependency-path: pyproject.toml |
|||
- uses: actions/cache@v3 |
|||
id: cache |
|||
with: |
|||
path: ${{ env.pythonLocation }} |
|||
key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-publish |
|||
- name: Install Flit |
|||
- name: Install build dependencies |
|||
if: steps.cache.outputs.cache-hit != 'true' |
|||
run: pip install flit |
|||
- name: Install Dependencies |
|||
if: steps.cache.outputs.cache-hit != 'true' |
|||
run: flit install --symlink |
|||
run: pip install build |
|||
- name: Build distribution |
|||
run: python -m build |
|||
- name: Publish |
|||
env: |
|||
FLIT_USERNAME: ${{ secrets.FLIT_USERNAME }} |
|||
FLIT_PASSWORD: ${{ secrets.FLIT_PASSWORD }} |
|||
run: bash scripts/publish.sh |
|||
uses: pypa/[email protected] |
|||
with: |
|||
password: ${{ secrets.PYPI_API_TOKEN }} |
|||
- name: Dump GitHub context |
|||
env: |
|||
GITHUB_CONTEXT: ${{ toJson(github) }} |
|||
run: echo "$GITHUB_CONTEXT" |
|||
# - name: Notify |
|||
# env: |
|||
# GITTER_TOKEN: ${{ secrets.GITTER_TOKEN }} |
|||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
|||
# TAG: ${{ github.event.release.name }} |
|||
# run: bash scripts/notify.sh |
|||
|
@ -0,0 +1,40 @@ |
|||
name: Smokeshow |
|||
|
|||
on: |
|||
workflow_run: |
|||
workflows: [Test] |
|||
types: [completed] |
|||
|
|||
permissions: |
|||
statuses: write |
|||
|
|||
jobs: |
|||
smokeshow: |
|||
if: ${{ github.event.workflow_run.conclusion == 'success' }} |
|||
runs-on: ubuntu-latest |
|||
|
|||
steps: |
|||
- name: Dump GitHub context |
|||
env: |
|||
GITHUB_CONTEXT: ${{ toJson(github) }} |
|||
run: echo "$GITHUB_CONTEXT" |
|||
- uses: actions/setup-python@v4 |
|||
with: |
|||
python-version: '3.9' |
|||
|
|||
- run: pip install smokeshow |
|||
|
|||
- uses: dawidd6/[email protected] |
|||
with: |
|||
github_token: ${{ secrets.FASTAPI_SMOKESHOW_DOWNLOAD_ARTIFACTS }} |
|||
workflow: test.yml |
|||
commit: ${{ github.event.workflow_run.head_sha }} |
|||
|
|||
- run: smokeshow upload coverage-html |
|||
env: |
|||
SMOKESHOW_GITHUB_STATUS_DESCRIPTION: Coverage {coverage-percentage} |
|||
SMOKESHOW_GITHUB_COVERAGE_THRESHOLD: 100 |
|||
SMOKESHOW_GITHUB_CONTEXT: coverage |
|||
SMOKESHOW_GITHUB_TOKEN: ${{ secrets.FASTAPI_SMOKESHOW_UPLOAD }} |
|||
SMOKESHOW_GITHUB_PR_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
|||
SMOKESHOW_AUTH_KEY: ${{ secrets.SMOKESHOW_AUTH_KEY }} |
@ -1,145 +0,0 @@ |
|||
site_name: FastAPI |
|||
site_description: FastAPI framework, high performance, easy to learn, fast to code, ready for production |
|||
site_url: https://fastapi.tiangolo.com/az/ |
|||
theme: |
|||
name: material |
|||
custom_dir: overrides |
|||
palette: |
|||
- media: '(prefers-color-scheme: light)' |
|||
scheme: default |
|||
primary: teal |
|||
accent: amber |
|||
toggle: |
|||
icon: material/lightbulb |
|||
name: Switch to light mode |
|||
- media: '(prefers-color-scheme: dark)' |
|||
scheme: slate |
|||
primary: teal |
|||
accent: amber |
|||
toggle: |
|||
icon: material/lightbulb-outline |
|||
name: Switch to dark mode |
|||
features: |
|||
- search.suggest |
|||
- search.highlight |
|||
- content.tabs.link |
|||
icon: |
|||
repo: fontawesome/brands/github-alt |
|||
logo: https://fastapi.tiangolo.com/img/icon-white.svg |
|||
favicon: https://fastapi.tiangolo.com/img/favicon.png |
|||
language: en |
|||
repo_name: tiangolo/fastapi |
|||
repo_url: https://github.com/tiangolo/fastapi |
|||
edit_uri: '' |
|||
plugins: |
|||
- search |
|||
- markdownextradata: |
|||
data: data |
|||
nav: |
|||
- FastAPI: index.md |
|||
- Languages: |
|||
- en: / |
|||
- az: /az/ |
|||
- de: /de/ |
|||
- es: /es/ |
|||
- fa: /fa/ |
|||
- fr: /fr/ |
|||
- he: /he/ |
|||
- id: /id/ |
|||
- it: /it/ |
|||
- ja: /ja/ |
|||
- ko: /ko/ |
|||
- nl: /nl/ |
|||
- pl: /pl/ |
|||
- pt: /pt/ |
|||
- ru: /ru/ |
|||
- sq: /sq/ |
|||
- sv: /sv/ |
|||
- tr: /tr/ |
|||
- uk: /uk/ |
|||
- zh: /zh/ |
|||
markdown_extensions: |
|||
- toc: |
|||
permalink: true |
|||
- markdown.extensions.codehilite: |
|||
guess_lang: false |
|||
- mdx_include: |
|||
base_path: docs |
|||
- admonition |
|||
- codehilite |
|||
- extra |
|||
- pymdownx.superfences: |
|||
custom_fences: |
|||
- name: mermaid |
|||
class: mermaid |
|||
format: !!python/name:pymdownx.superfences.fence_code_format '' |
|||
- pymdownx.tabbed: |
|||
alternate_style: true |
|||
- attr_list |
|||
- md_in_html |
|||
extra: |
|||
analytics: |
|||
provider: google |
|||
property: UA-133183413-1 |
|||
social: |
|||
- icon: fontawesome/brands/github-alt |
|||
link: https://github.com/tiangolo/fastapi |
|||
- icon: fontawesome/brands/discord |
|||
link: https://discord.gg/VQjSZaeJmf |
|||
- icon: fontawesome/brands/twitter |
|||
link: https://twitter.com/fastapi |
|||
- icon: fontawesome/brands/linkedin |
|||
link: https://www.linkedin.com/in/tiangolo |
|||
- icon: fontawesome/brands/dev |
|||
link: https://dev.to/tiangolo |
|||
- icon: fontawesome/brands/medium |
|||
link: https://medium.com/@tiangolo |
|||
- icon: fontawesome/solid/globe |
|||
link: https://tiangolo.com |
|||
alternate: |
|||
- link: / |
|||
name: en - English |
|||
- link: /az/ |
|||
name: az |
|||
- link: /de/ |
|||
name: de |
|||
- link: /es/ |
|||
name: es - espaรฑol |
|||
- link: /fa/ |
|||
name: fa |
|||
- link: /fr/ |
|||
name: fr - franรงais |
|||
- link: /he/ |
|||
name: he |
|||
- link: /id/ |
|||
name: id |
|||
- link: /it/ |
|||
name: it - italiano |
|||
- link: /ja/ |
|||
name: ja - ๆฅๆฌ่ช |
|||
- link: /ko/ |
|||
name: ko - ํ๊ตญ์ด |
|||
- link: /nl/ |
|||
name: nl |
|||
- link: /pl/ |
|||
name: pl |
|||
- link: /pt/ |
|||
name: pt - portuguรชs |
|||
- link: /ru/ |
|||
name: ru - ััััะบะธะน ัะทัะบ |
|||
- link: /sq/ |
|||
name: sq - shqip |
|||
- link: /sv/ |
|||
name: sv - svenska |
|||
- link: /tr/ |
|||
name: tr - Tรผrkรงe |
|||
- link: /uk/ |
|||
name: uk - ัะบัะฐัะฝััะบะฐ ะผะพะฒะฐ |
|||
- link: /zh/ |
|||
name: zh - ๆฑ่ฏญ |
|||
extra_css: |
|||
- https://fastapi.tiangolo.com/css/termynal.css |
|||
- https://fastapi.tiangolo.com/css/custom.css |
|||
extra_javascript: |
|||
- https://fastapi.tiangolo.com/js/termynal.js |
|||
- https://fastapi.tiangolo.com/js/custom.js |
@ -1,464 +0,0 @@ |
|||
|
|||
{!../../../docs/missing-translation.md!} |
|||
|
|||
|
|||
<p align="center"> |
|||
<a href="https://fastapi.tiangolo.com"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" alt="FastAPI"></a> |
|||
</p> |
|||
<p align="center"> |
|||
<em>FastAPI framework, high performance, easy to learn, fast to code, ready for production</em> |
|||
</p> |
|||
<p align="center"> |
|||
<a href="https://github.com/tiangolo/fastapi/actions?query=workflow%3ATest" target="_blank"> |
|||
<img src="https://github.com/tiangolo/fastapi/workflows/Test/badge.svg" alt="Test"> |
|||
</a> |
|||
<a href="https://codecov.io/gh/tiangolo/fastapi" target="_blank"> |
|||
<img src="https://img.shields.io/codecov/c/github/tiangolo/fastapi?color=%2334D058" alt="Coverage"> |
|||
</a> |
|||
<a href="https://pypi.org/project/fastapi" target="_blank"> |
|||
<img src="https://img.shields.io/pypi/v/fastapi?color=%2334D058&label=pypi%20package" alt="Package version"> |
|||
</a> |
|||
</p> |
|||
|
|||
--- |
|||
|
|||
**Documentation**: <a href="https://fastapi.tiangolo.com" target="_blank">https://fastapi.tiangolo.com</a> |
|||
|
|||
**Source Code**: <a href="https://github.com/tiangolo/fastapi" target="_blank">https://github.com/tiangolo/fastapi</a> |
|||
|
|||
--- |
|||
|
|||
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. |
|||
|
|||
The key features are: |
|||
|
|||
* **Fast**: Very high performance, on par with **NodeJS** and **Go** (thanks to Starlette and Pydantic). [One of the fastest Python frameworks available](#performance). |
|||
|
|||
* **Fast to code**: Increase the speed to develop features by about 200% to 300%. * |
|||
* **Fewer bugs**: Reduce about 40% of human (developer) induced errors. * |
|||
* **Intuitive**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging. |
|||
* **Easy**: Designed to be easy to use and learn. Less time reading docs. |
|||
* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs. |
|||
* **Robust**: Get production-ready code. With automatic interactive documentation. |
|||
* **Standards-based**: Based on (and fully compatible with) the open standards for APIs: <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank">OpenAPI</a> (previously known as Swagger) and <a href="https://json-schema.org/" class="external-link" target="_blank">JSON Schema</a>. |
|||
|
|||
<small>* estimation based on tests on an internal development team, building production applications.</small> |
|||
|
|||
## Sponsors |
|||
|
|||
<!-- sponsors --> |
|||
|
|||
{% if sponsors %} |
|||
{% for sponsor in sponsors.gold -%} |
|||
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> |
|||
{% endfor -%} |
|||
{%- for sponsor in sponsors.silver -%} |
|||
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
<!-- /sponsors --> |
|||
|
|||
<a href="https://fastapi.tiangolo.com/fastapi-people/#sponsors" class="external-link" target="_blank">Other sponsors</a> |
|||
|
|||
## Opinions |
|||
|
|||
"_[...] I'm using **FastAPI** a ton these days. [...] I'm actually planning to use it for all of my team's **ML services at Microsoft**. Some of them are getting integrated into the core **Windows** product and some **Office** products._" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">Kabir Khan - <strong>Microsoft</strong> <a href="https://github.com/tiangolo/fastapi/pull/26" target="_blank"><small>(ref)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_We adopted the **FastAPI** library to spawn a **REST** server that can be queried to obtain **predictions**. [for Ludwig]_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">Piero Molino, Yaroslav Dudin, and Sai Sumanth Miryala - <strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/" target="_blank"><small>(ref)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_**Netflix** is pleased to announce the open-source release of our **crisis management** orchestration framework: **Dispatch**! [built with **FastAPI**]_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">Kevin Glisson, Marc Vilanova, Forest Monsen - <strong>Netflix</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072" target="_blank"><small>(ref)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_Iโm over the moon excited about **FastAPI**. Itโs so fun!_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">Brian Okken - <strong><a href="https://pythonbytes.fm/episodes/show/123/time-to-right-the-py-wrongs?time_in_sec=855" target="_blank">Python Bytes</a> podcast host</strong> <a href="https://twitter.com/brianokken/status/1112220079972728832" target="_blank"><small>(ref)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_Honestly, what you've built looks super solid and polished. In many ways, it's what I wanted **Hug** to be - it's really inspiring to see someone build that._" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">Timothy Crosley - <strong><a href="https://www.hug.rest/" target="_blank">Hug</a> creator</strong> <a href="https://news.ycombinator.com/item?id=19455465" target="_blank"><small>(ref)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_If you're looking to learn one **modern framework** for building REST APIs, check out **FastAPI** [...] It's fast, easy to use and easy to learn [...]_" |
|||
|
|||
"_We've switched over to **FastAPI** for our **APIs** [...] I think you'll like it [...]_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">Ines Montani - Matthew Honnibal - <strong><a href="https://explosion.ai" target="_blank">Explosion AI</a> founders - <a href="https://spacy.io" target="_blank">spaCy</a> creators</strong> <a href="https://twitter.com/_inesmontani/status/1144173225322143744" target="_blank"><small>(ref)</small></a> - <a href="https://twitter.com/honnibal/status/1144031421859655680" target="_blank"><small>(ref)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
## **Typer**, the FastAPI of CLIs |
|||
|
|||
<a href="https://typer.tiangolo.com" target="_blank"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" style="width: 20%;"></a> |
|||
|
|||
If you are building a <abbr title="Command Line Interface">CLI</abbr> app to be used in the terminal instead of a web API, check out <a href="https://typer.tiangolo.com/" class="external-link" target="_blank">**Typer**</a>. |
|||
|
|||
**Typer** is FastAPI's little sibling. And it's intended to be the **FastAPI of CLIs**. โจ๏ธ ๐ |
|||
|
|||
## Requirements |
|||
|
|||
Python 3.6+ |
|||
|
|||
FastAPI stands on the shoulders of giants: |
|||
|
|||
* <a href="https://www.starlette.io/" class="external-link" target="_blank">Starlette</a> for the web parts. |
|||
* <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> for the data parts. |
|||
|
|||
## Installation |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install fastapi |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
You will also need an ASGI server, for production such as <a href="https://www.uvicorn.org" class="external-link" target="_blank">Uvicorn</a> or <a href="https://gitlab.com/pgjones/hypercorn" class="external-link" target="_blank">Hypercorn</a>. |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install uvicorn[standard] |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
## Example |
|||
|
|||
### Create it |
|||
|
|||
* Create a file `main.py` with: |
|||
|
|||
```Python |
|||
from typing import Union |
|||
|
|||
from fastapi import FastAPI |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.get("/") |
|||
def read_root(): |
|||
return {"Hello": "World"} |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
def read_item(item_id: int, q: Union[str, None] = None): |
|||
return {"item_id": item_id, "q": q} |
|||
``` |
|||
|
|||
<details markdown="1"> |
|||
<summary>Or use <code>async def</code>...</summary> |
|||
|
|||
If your code uses `async` / `await`, use `async def`: |
|||
|
|||
```Python hl_lines="9 14" |
|||
from typing import Union |
|||
|
|||
from fastapi import FastAPI |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.get("/") |
|||
async def read_root(): |
|||
return {"Hello": "World"} |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
async def read_item(item_id: int, q: Union[str, None] = None): |
|||
return {"item_id": item_id, "q": q} |
|||
``` |
|||
|
|||
**Note**: |
|||
|
|||
If you don't know, check the _"In a hurry?"_ section about <a href="https://fastapi.tiangolo.com/async/#in-a-hurry" target="_blank">`async` and `await` in the docs</a>. |
|||
|
|||
</details> |
|||
|
|||
### Run it |
|||
|
|||
Run the server with: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --reload |
|||
|
|||
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
INFO: Started reloader process [28720] |
|||
INFO: Started server process [28722] |
|||
INFO: Waiting for application startup. |
|||
INFO: Application startup complete. |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
<details markdown="1"> |
|||
<summary>About the command <code>uvicorn main:app --reload</code>...</summary> |
|||
|
|||
The command `uvicorn main:app` refers to: |
|||
|
|||
* `main`: the file `main.py` (the Python "module"). |
|||
* `app`: the object created inside of `main.py` with the line `app = FastAPI()`. |
|||
* `--reload`: make the server restart after code changes. Only do this for development. |
|||
|
|||
</details> |
|||
|
|||
### Check it |
|||
|
|||
Open your browser at <a href="http://127.0.0.1:8000/items/5?q=somequery" class="external-link" target="_blank">http://127.0.0.1:8000/items/5?q=somequery</a>. |
|||
|
|||
You will see the JSON response as: |
|||
|
|||
```JSON |
|||
{"item_id": 5, "q": "somequery"} |
|||
``` |
|||
|
|||
You already created an API that: |
|||
|
|||
* Receives HTTP requests in the _paths_ `/` and `/items/{item_id}`. |
|||
* Both _paths_ take `GET` <em>operations</em> (also known as HTTP _methods_). |
|||
* The _path_ `/items/{item_id}` has a _path parameter_ `item_id` that should be an `int`. |
|||
* The _path_ `/items/{item_id}` has an optional `str` _query parameter_ `q`. |
|||
|
|||
### Interactive API docs |
|||
|
|||
Now go to <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
You will see the automatic interactive API documentation (provided by <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">Swagger UI</a>): |
|||
|
|||
 |
|||
|
|||
### Alternative API docs |
|||
|
|||
And now, go to <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>. |
|||
|
|||
You will see the alternative automatic documentation (provided by <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">ReDoc</a>): |
|||
|
|||
 |
|||
|
|||
## Example upgrade |
|||
|
|||
Now modify the file `main.py` to receive a body from a `PUT` request. |
|||
|
|||
Declare the body using standard Python types, thanks to Pydantic. |
|||
|
|||
```Python hl_lines="4 9-12 25-27" |
|||
from typing import Union |
|||
|
|||
from fastapi import FastAPI |
|||
from pydantic import BaseModel |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
class Item(BaseModel): |
|||
name: str |
|||
price: float |
|||
is_offer: Union[bool, None] = None |
|||
|
|||
|
|||
@app.get("/") |
|||
def read_root(): |
|||
return {"Hello": "World"} |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
def read_item(item_id: int, q: Union[str, None] = None): |
|||
return {"item_id": item_id, "q": q} |
|||
|
|||
|
|||
@app.put("/items/{item_id}") |
|||
def update_item(item_id: int, item: Item): |
|||
return {"item_name": item.name, "item_id": item_id} |
|||
``` |
|||
|
|||
The server should reload automatically (because you added `--reload` to the `uvicorn` command above). |
|||
|
|||
### Interactive API docs upgrade |
|||
|
|||
Now go to <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
* The interactive API documentation will be automatically updated, including the new body: |
|||
|
|||
 |
|||
|
|||
* Click on the button "Try it out", it allows you to fill the parameters and directly interact with the API: |
|||
|
|||
 |
|||
|
|||
* Then click on the "Execute" button, the user interface will communicate with your API, send the parameters, get the results and show them on the screen: |
|||
|
|||
 |
|||
|
|||
### Alternative API docs upgrade |
|||
|
|||
And now, go to <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>. |
|||
|
|||
* The alternative documentation will also reflect the new query parameter and body: |
|||
|
|||
 |
|||
|
|||
### Recap |
|||
|
|||
In summary, you declare **once** the types of parameters, body, etc. as function parameters. |
|||
|
|||
You do that with standard modern Python types. |
|||
|
|||
You don't have to learn a new syntax, the methods or classes of a specific library, etc. |
|||
|
|||
Just standard **Python 3.6+**. |
|||
|
|||
For example, for an `int`: |
|||
|
|||
```Python |
|||
item_id: int |
|||
``` |
|||
|
|||
or for a more complex `Item` model: |
|||
|
|||
```Python |
|||
item: Item |
|||
``` |
|||
|
|||
...and with that single declaration you get: |
|||
|
|||
* Editor support, including: |
|||
* Completion. |
|||
* Type checks. |
|||
* Validation of data: |
|||
* Automatic and clear errors when the data is invalid. |
|||
* Validation even for deeply nested JSON objects. |
|||
* <abbr title="also known as: serialization, parsing, marshalling">Conversion</abbr> of input data: coming from the network to Python data and types. Reading from: |
|||
* JSON. |
|||
* Path parameters. |
|||
* Query parameters. |
|||
* Cookies. |
|||
* Headers. |
|||
* Forms. |
|||
* Files. |
|||
* <abbr title="also known as: serialization, parsing, marshalling">Conversion</abbr> of output data: converting from Python data and types to network data (as JSON): |
|||
* Convert Python types (`str`, `int`, `float`, `bool`, `list`, etc). |
|||
* `datetime` objects. |
|||
* `UUID` objects. |
|||
* Database models. |
|||
* ...and many more. |
|||
* Automatic interactive API documentation, including 2 alternative user interfaces: |
|||
* Swagger UI. |
|||
* ReDoc. |
|||
|
|||
--- |
|||
|
|||
Coming back to the previous code example, **FastAPI** will: |
|||
|
|||
* Validate that there is an `item_id` in the path for `GET` and `PUT` requests. |
|||
* Validate that the `item_id` is of type `int` for `GET` and `PUT` requests. |
|||
* If it is not, the client will see a useful, clear error. |
|||
* Check if there is an optional query parameter named `q` (as in `http://127.0.0.1:8000/items/foo?q=somequery`) for `GET` requests. |
|||
* As the `q` parameter is declared with `= None`, it is optional. |
|||
* Without the `None` it would be required (as is the body in the case with `PUT`). |
|||
* For `PUT` requests to `/items/{item_id}`, Read the body as JSON: |
|||
* Check that it has a required attribute `name` that should be a `str`. |
|||
* Check that it has a required attribute `price` that has to be a `float`. |
|||
* Check that it has an optional attribute `is_offer`, that should be a `bool`, if present. |
|||
* All this would also work for deeply nested JSON objects. |
|||
* Convert from and to JSON automatically. |
|||
* Document everything with OpenAPI, that can be used by: |
|||
* Interactive documentation systems. |
|||
* Automatic client code generation systems, for many languages. |
|||
* Provide 2 interactive documentation web interfaces directly. |
|||
|
|||
--- |
|||
|
|||
We just scratched the surface, but you already get the idea of how it all works. |
|||
|
|||
Try changing the line with: |
|||
|
|||
```Python |
|||
return {"item_name": item.name, "item_id": item_id} |
|||
``` |
|||
|
|||
...from: |
|||
|
|||
```Python |
|||
... "item_name": item.name ... |
|||
``` |
|||
|
|||
...to: |
|||
|
|||
```Python |
|||
... "item_price": item.price ... |
|||
``` |
|||
|
|||
...and see how your editor will auto-complete the attributes and know their types: |
|||
|
|||
 |
|||
|
|||
For a more complete example including more features, see the <a href="https://fastapi.tiangolo.com/tutorial/">Tutorial - User Guide</a>. |
|||
|
|||
**Spoiler alert**: the tutorial - user guide includes: |
|||
|
|||
* Declaration of **parameters** from other different places as: **headers**, **cookies**, **form fields** and **files**. |
|||
* How to set **validation constraints** as `maximum_length` or `regex`. |
|||
* A very powerful and easy to use **<abbr title="also known as components, resources, providers, services, injectables">Dependency Injection</abbr>** system. |
|||
* Security and authentication, including support for **OAuth2** with **JWT tokens** and **HTTP Basic** auth. |
|||
* More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic). |
|||
* Many extra features (thanks to Starlette) as: |
|||
* **WebSockets** |
|||
* extremely easy tests based on `requests` and `pytest` |
|||
* **CORS** |
|||
* **Cookie Sessions** |
|||
* ...and more. |
|||
|
|||
## Performance |
|||
|
|||
Independent TechEmpower benchmarks show **FastAPI** applications running under Uvicorn as <a href="https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7" class="external-link" target="_blank">one of the fastest Python frameworks available</a>, only below Starlette and Uvicorn themselves (used internally by FastAPI). (*) |
|||
|
|||
To understand more about it, see the section <a href="https://fastapi.tiangolo.com/benchmarks/" class="internal-link" target="_blank">Benchmarks</a>. |
|||
|
|||
## Optional Dependencies |
|||
|
|||
Used by Pydantic: |
|||
|
|||
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - for faster JSON <abbr title="converting the string that comes from an HTTP request into Python data">"parsing"</abbr>. |
|||
* <a href="https://github.com/JoshData/python-email-validator" target="_blank"><code>email_validator</code></a> - for email validation. |
|||
|
|||
Used by Starlette: |
|||
|
|||
* <a href="https://requests.readthedocs.io" target="_blank"><code>requests</code></a> - Required if you want to use the `TestClient`. |
|||
* <a href="https://jinja.palletsprojects.com" target="_blank"><code>jinja2</code></a> - Required if you want to use the default template configuration. |
|||
* <a href="https://andrew-d.github.io/python-multipart/" target="_blank"><code>python-multipart</code></a> - Required if you want to support form <abbr title="converting the string that comes from an HTTP request into Python data">"parsing"</abbr>, with `request.form()`. |
|||
* <a href="https://pythonhosted.org/itsdangerous/" target="_blank"><code>itsdangerous</code></a> - Required for `SessionMiddleware` support. |
|||
* <a href="https://pyyaml.org/wiki/PyYAMLDocumentation" target="_blank"><code>pyyaml</code></a> - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI). |
|||
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`. |
|||
|
|||
Used by FastAPI / Starlette: |
|||
|
|||
* <a href="https://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application. |
|||
* <a href="https://github.com/ijl/orjson" target="_blank"><code>orjson</code></a> - Required if you want to use `ORJSONResponse`. |
|||
|
|||
You can install all of these with `pip install fastapi[all]`. |
|||
|
|||
## License |
|||
|
|||
This project is licensed under the terms of the MIT license. |
@ -1,146 +1 @@ |
|||
site_name: FastAPI |
|||
site_description: FastAPI framework, high performance, easy to learn, fast to code, ready for production |
|||
site_url: https://fastapi.tiangolo.com/de/ |
|||
theme: |
|||
name: material |
|||
custom_dir: overrides |
|||
palette: |
|||
- media: '(prefers-color-scheme: light)' |
|||
scheme: default |
|||
primary: teal |
|||
accent: amber |
|||
toggle: |
|||
icon: material/lightbulb |
|||
name: Switch to light mode |
|||
- media: '(prefers-color-scheme: dark)' |
|||
scheme: slate |
|||
primary: teal |
|||
accent: amber |
|||
toggle: |
|||
icon: material/lightbulb-outline |
|||
name: Switch to dark mode |
|||
features: |
|||
- search.suggest |
|||
- search.highlight |
|||
- content.tabs.link |
|||
icon: |
|||
repo: fontawesome/brands/github-alt |
|||
logo: https://fastapi.tiangolo.com/img/icon-white.svg |
|||
favicon: https://fastapi.tiangolo.com/img/favicon.png |
|||
language: de |
|||
repo_name: tiangolo/fastapi |
|||
repo_url: https://github.com/tiangolo/fastapi |
|||
edit_uri: '' |
|||
plugins: |
|||
- search |
|||
- markdownextradata: |
|||
data: data |
|||
nav: |
|||
- FastAPI: index.md |
|||
- Languages: |
|||
- en: / |
|||
- az: /az/ |
|||
- de: /de/ |
|||
- es: /es/ |
|||
- fa: /fa/ |
|||
- fr: /fr/ |
|||
- he: /he/ |
|||
- id: /id/ |
|||
- it: /it/ |
|||
- ja: /ja/ |
|||
- ko: /ko/ |
|||
- nl: /nl/ |
|||
- pl: /pl/ |
|||
- pt: /pt/ |
|||
- ru: /ru/ |
|||
- sq: /sq/ |
|||
- sv: /sv/ |
|||
- tr: /tr/ |
|||
- uk: /uk/ |
|||
- zh: /zh/ |
|||
- features.md |
|||
markdown_extensions: |
|||
- toc: |
|||
permalink: true |
|||
- markdown.extensions.codehilite: |
|||
guess_lang: false |
|||
- mdx_include: |
|||
base_path: docs |
|||
- admonition |
|||
- codehilite |
|||
- extra |
|||
- pymdownx.superfences: |
|||
custom_fences: |
|||
- name: mermaid |
|||
class: mermaid |
|||
format: !!python/name:pymdownx.superfences.fence_code_format '' |
|||
- pymdownx.tabbed: |
|||
alternate_style: true |
|||
- attr_list |
|||
- md_in_html |
|||
extra: |
|||
analytics: |
|||
provider: google |
|||
property: UA-133183413-1 |
|||
social: |
|||
- icon: fontawesome/brands/github-alt |
|||
link: https://github.com/tiangolo/fastapi |
|||
- icon: fontawesome/brands/discord |
|||
link: https://discord.gg/VQjSZaeJmf |
|||
- icon: fontawesome/brands/twitter |
|||
link: https://twitter.com/fastapi |
|||
- icon: fontawesome/brands/linkedin |
|||
link: https://www.linkedin.com/in/tiangolo |
|||
- icon: fontawesome/brands/dev |
|||
link: https://dev.to/tiangolo |
|||
- icon: fontawesome/brands/medium |
|||
link: https://medium.com/@tiangolo |
|||
- icon: fontawesome/solid/globe |
|||
link: https://tiangolo.com |
|||
alternate: |
|||
- link: / |
|||
name: en - English |
|||
- link: /az/ |
|||
name: az |
|||
- link: /de/ |
|||
name: de |
|||
- link: /es/ |
|||
name: es - espaรฑol |
|||
- link: /fa/ |
|||
name: fa |
|||
- link: /fr/ |
|||
name: fr - franรงais |
|||
- link: /he/ |
|||
name: he |
|||
- link: /id/ |
|||
name: id |
|||
- link: /it/ |
|||
name: it - italiano |
|||
- link: /ja/ |
|||
name: ja - ๆฅๆฌ่ช |
|||
- link: /ko/ |
|||
name: ko - ํ๊ตญ์ด |
|||
- link: /nl/ |
|||
name: nl |
|||
- link: /pl/ |
|||
name: pl |
|||
- link: /pt/ |
|||
name: pt - portuguรชs |
|||
- link: /ru/ |
|||
name: ru - ััััะบะธะน ัะทัะบ |
|||
- link: /sq/ |
|||
name: sq - shqip |
|||
- link: /sv/ |
|||
name: sv - svenska |
|||
- link: /tr/ |
|||
name: tr - Tรผrkรงe |
|||
- link: /uk/ |
|||
name: uk - ัะบัะฐัะฝััะบะฐ ะผะพะฒะฐ |
|||
- link: /zh/ |
|||
name: zh - ๆฑ่ฏญ |
|||
extra_css: |
|||
- https://fastapi.tiangolo.com/css/termynal.css |
|||
- https://fastapi.tiangolo.com/css/custom.css |
|||
extra_javascript: |
|||
- https://fastapi.tiangolo.com/js/termynal.js |
|||
- https://fastapi.tiangolo.com/js/custom.js |
|||
INHERIT: ../en/mkdocs.yml |
|||
|
@ -0,0 +1,240 @@ |
|||
# ๐ ๐จ ๐ |
|||
|
|||
!!! warning |
|||
๐ ๐ ๐ง โ. |
|||
|
|||
๐ฅ ๐ โถ๏ธ โฎ๏ธ **FastAPI**, ๐ ๐ช ๐ซ ๐ช ๐. |
|||
|
|||
๐ ๐ช ๐ฃ ๐ ๐จ, โฎ๏ธ ๐ ๐ ๐, ๐ ๐, ๐, โ๏ธ. |
|||
|
|||
๐ ๐ ๐จ ๐ ๐ ๐ ๐, ๐ซ ๐ ๐ ๐ ๏ธ ๐ฉบ. |
|||
|
|||
โ๏ธ ๐ ๐ ๐จ ๐ โ๏ธ โ ๐ญ ๐ ๐จ `Response` ๐ `JSONResponse` ๐, โฎ๏ธ ๐ ๐ ๐ & ๐. |
|||
|
|||
## ๐ ๐จ โฎ๏ธ `model` |
|||
|
|||
๐ ๐ช ๐ถโโ๏ธ ๐ *โก ๐ ๏ธ ๐จโ๐จ* ๐ข `responses`. |
|||
|
|||
โซ๏ธ ๐จ `dict`, ๐ ๐ ๐ ๐ ๐จ, ๐ `200`, & ๐ฒ ๐ `dict`โ โฎ๏ธ โน ๐ ๐ซ. |
|||
|
|||
๐ ๐ ๐จ `dict`โ ๐ช โ๏ธ ๐ `model`, โ Pydantic ๐ท, ๐ `response_model`. |
|||
|
|||
**FastAPI** ๐ โ ๐ ๐ท, ๐ ๐ฎ ๐ป ๐ & ๐ โซ๏ธ โ ๐ฅ ๐. |
|||
|
|||
๐ผ, ๐ฃ โ1๏ธโฃ ๐จ โฎ๏ธ ๐ ๐ `404` & Pydantic ๐ท `Message`, ๐ ๐ช โ: |
|||
|
|||
```Python hl_lines="18 22" |
|||
{!../../../docs_src/additional_responses/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! note |
|||
โ๏ธ ๐คฏ ๐ ๐ โ๏ธ ๐จ `JSONResponse` ๐. |
|||
|
|||
!!! info |
|||
`model` ๐ ๐ซ ๐ ๐. |
|||
|
|||
**FastAPI** ๐ โ Pydantic ๐ท โช๏ธโก๏ธ ๐ค, ๐ `JSON Schema`, & ๐ฎ โซ๏ธ โ ๐ฅ. |
|||
|
|||
โ ๐ฅ: |
|||
|
|||
* ๐ `content`, ๐ โ๏ธ ๐ฒ โ1๏ธโฃ ๐ป ๐ (`dict`) ๐ ๐: |
|||
* ๐ โฎ๏ธ ๐ป ๐, โ
`application/json`, ๐ ๐ ๐ฒ โ1๏ธโฃ ๐ป ๐, ๐ ๐: |
|||
* ๐ `schema`, ๐ โ๏ธ ๐ฒ ๐ป ๐ โช๏ธโก๏ธ ๐ท, ๐ฅ โ ๐ฅ. |
|||
* **FastAPI** ๐ฎ ๐ ๐ฅ ๐ ๐ป ๐ โ1๏ธโฃ ๐ฅ ๐ ๐ โฉ๏ธ โ
โซ๏ธ ๐. ๐ ๐, ๐ ๐ธ & ๐ฉโ๐ป ๐ช โ๏ธ ๐ ๐ป ๐ ๐, ๐ ๐ป ๐ โก ๐งฐ, โ๏ธ. |
|||
|
|||
๐ ๐จ ๐ ๐ *โก ๐ ๏ธ* ๐: |
|||
|
|||
```JSON hl_lines="3-12" |
|||
{ |
|||
"responses": { |
|||
"404": { |
|||
"description": "Additional Response", |
|||
"content": { |
|||
"application/json": { |
|||
"schema": { |
|||
"$ref": "#/components/schemas/Message" |
|||
} |
|||
} |
|||
} |
|||
}, |
|||
"200": { |
|||
"description": "Successful Response", |
|||
"content": { |
|||
"application/json": { |
|||
"schema": { |
|||
"$ref": "#/components/schemas/Item" |
|||
} |
|||
} |
|||
} |
|||
}, |
|||
"422": { |
|||
"description": "Validation Error", |
|||
"content": { |
|||
"application/json": { |
|||
"schema": { |
|||
"$ref": "#/components/schemas/HTTPValidationError" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
``` |
|||
|
|||
๐ ๐ โ1๏ธโฃ ๐ฅ ๐ ๐ ๐: |
|||
|
|||
```JSON hl_lines="4-16" |
|||
{ |
|||
"components": { |
|||
"schemas": { |
|||
"Message": { |
|||
"title": "Message", |
|||
"required": [ |
|||
"message" |
|||
], |
|||
"type": "object", |
|||
"properties": { |
|||
"message": { |
|||
"title": "Message", |
|||
"type": "string" |
|||
} |
|||
} |
|||
}, |
|||
"Item": { |
|||
"title": "Item", |
|||
"required": [ |
|||
"id", |
|||
"value" |
|||
], |
|||
"type": "object", |
|||
"properties": { |
|||
"id": { |
|||
"title": "Id", |
|||
"type": "string" |
|||
}, |
|||
"value": { |
|||
"title": "Value", |
|||
"type": "string" |
|||
} |
|||
} |
|||
}, |
|||
"ValidationError": { |
|||
"title": "ValidationError", |
|||
"required": [ |
|||
"loc", |
|||
"msg", |
|||
"type" |
|||
], |
|||
"type": "object", |
|||
"properties": { |
|||
"loc": { |
|||
"title": "Location", |
|||
"type": "array", |
|||
"items": { |
|||
"type": "string" |
|||
} |
|||
}, |
|||
"msg": { |
|||
"title": "Message", |
|||
"type": "string" |
|||
}, |
|||
"type": { |
|||
"title": "Error Type", |
|||
"type": "string" |
|||
} |
|||
} |
|||
}, |
|||
"HTTPValidationError": { |
|||
"title": "HTTPValidationError", |
|||
"type": "object", |
|||
"properties": { |
|||
"detail": { |
|||
"title": "Detail", |
|||
"type": "array", |
|||
"items": { |
|||
"$ref": "#/components/schemas/ValidationError" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
``` |
|||
|
|||
## ๐ ๐ ๐ ๐ ๐จ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ `responses` ๐ข ๐ฎ ๐ ๐ ๐ ๐ ๐ ๐จ. |
|||
|
|||
๐ผ, ๐ ๐ช ๐ฎ ๐ ๐ป ๐ `image/png`, ๐ฃ ๐ ๐ *โก ๐ ๏ธ* ๐ช ๐จ ๐ป ๐ (โฎ๏ธ ๐ป ๐ `application/json`) โ๏ธ ๐ฉ๐ด ๐ผ: |
|||
|
|||
```Python hl_lines="19-24 28" |
|||
{!../../../docs_src/additional_responses/tutorial002.py!} |
|||
``` |
|||
|
|||
!!! note |
|||
๐ ๐ ๐ โ๏ธ ๐จ ๐ผ โ๏ธ `FileResponse` ๐. |
|||
|
|||
!!! info |
|||
๐ฅ ๐ โ ๐ ๐ป ๐ ๐ฏ ๐ `responses` ๐ข, FastAPI ๐ ๐ค ๐จ โ๏ธ ๐ ๐ป ๐ ๐ ๐จ ๐ (๐ข `application/json`). |
|||
|
|||
โ๏ธ ๐ฅ ๐ โ๏ธ โ ๐ ๐จ ๐ โฎ๏ธ `None` ๐ฎ ๐ป ๐, FastAPI ๐ โ๏ธ `application/json` ๐ ๐ ๐จ ๐ โ๏ธ ๐จโ๐ผ ๐ท. |
|||
|
|||
## ๐ โน |
|||
|
|||
๐ ๐ช ๐ ๐จ โน โช๏ธโก๏ธ ๐ ๐ฅ, ๐ `response_model`, `status_code`, & `responses` ๐ข. |
|||
|
|||
๐ ๐ช ๐ฃ `response_model`, โ๏ธ ๐ข ๐ ๐ `200` (โ๏ธ ๐ 1๏ธโฃ ๐ฅ ๐ ๐ช), & โคด๏ธ ๐ฃ ๐ โน ๐ ๐ ๐จ `responses`, ๐ ๐ ๐. |
|||
|
|||
**FastAPI** ๐ ๐ง ๐ โน โช๏ธโก๏ธ `responses`, & ๐ โซ๏ธ โฎ๏ธ ๐ป ๐ โช๏ธโก๏ธ ๐ ๐ท. |
|||
|
|||
๐ผ, ๐ ๐ช ๐ฃ ๐จ โฎ๏ธ ๐ ๐ `404` ๐ โ๏ธ Pydantic ๐ท & โ๏ธ ๐ `description`. |
|||
|
|||
& ๐จ โฎ๏ธ ๐ ๐ `200` ๐ โ๏ธ ๐ `response_model`, โ๏ธ ๐ ๐ `example`: |
|||
|
|||
```Python hl_lines="20-31" |
|||
{!../../../docs_src/additional_responses/tutorial003.py!} |
|||
``` |
|||
|
|||
โซ๏ธ ๐ ๐ ๐ & ๐ ๐ ๐, & ๐ฆ ๐ ๏ธ ๐ฉบ: |
|||
|
|||
<img src="/img/tutorial/additional-responses/image01.png"> |
|||
|
|||
## ๐ ๐ข ๐จ & ๐ ๐ |
|||
|
|||
๐ ๐ช ๐ โ๏ธ ๐ ๐จ ๐ โ ๐ *โก ๐ ๏ธ*, โ๏ธ ๐ ๐ ๐ ๐ซ โฎ๏ธ ๐ ๐จ ๐ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
๐ ๐ผ, ๐ ๐ช โ๏ธ ๐ โ "๐" `dict` โฎ๏ธ `**dict_to_unpack`: |
|||
|
|||
```Python |
|||
old_dict = { |
|||
"old key": "old value", |
|||
"second old key": "second old value", |
|||
} |
|||
new_dict = {**old_dict, "new key": "new value"} |
|||
``` |
|||
|
|||
๐ฅ, `new_dict` ๐ ๐ ๐ ๐-๐ฒ ๐ซ โช๏ธโก๏ธ `old_dict` โ ๐ ๐-๐ฒ ๐ซ: |
|||
|
|||
```Python |
|||
{ |
|||
"old key": "old value", |
|||
"second old key": "second old value", |
|||
"new key": "new value", |
|||
} |
|||
``` |
|||
|
|||
๐ ๐ช โ๏ธ ๐ โ ๐ค-โ๏ธ ๐ข ๐จ ๐ *โก ๐ ๏ธ* & ๐ ๐ซ โฎ๏ธ ๐ ๐ ๐. |
|||
|
|||
๐ผ: |
|||
|
|||
```Python hl_lines="13-17 26" |
|||
{!../../../docs_src/additional_responses/tutorial004.py!} |
|||
``` |
|||
|
|||
## ๐ โน ๐ ๐ ๐จ |
|||
|
|||
๐ โซ๏ธโ โซ๏ธโ ๐ ๐ช ๐ ๐จ, ๐ ๐ช โ
๐ ๐ ๐ ๐ง: |
|||
|
|||
* <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#responsesObject" class="external-link" target="_blank">๐ ๐จ ๐</a>, โซ๏ธ ๐ `Response Object`. |
|||
* <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#responseObject" class="external-link" target="_blank">๐ ๐จ ๐</a>, ๐ ๐ช ๐ ๐ณ โช๏ธโก๏ธ ๐ ๐ ๐ ๐จ ๐ ๐ `responses` ๐ข. โ
`description`, `headers`, `content` (๐ ๐ ๐ ๐ ๐ฃ ๐ ๐ ๐ & ๐ป ๐), & `links`. |
@ -0,0 +1,37 @@ |
|||
# ๐ ๐ ๐ |
|||
|
|||
๐ข, **FastAPI** ๐ ๐จ ๐จ โ๏ธ `JSONResponse`, ๐ฎ ๐ ๐ ๐จ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ* ๐ ๐ `JSONResponse`. |
|||
|
|||
โซ๏ธ ๐ โ๏ธ ๐ข ๐ ๐ โ๏ธ 1๏ธโฃ ๐ โ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
## ๐ ๐ ๐ |
|||
|
|||
๐ฅ ๐ ๐ ๐จ ๐ ๐ ๐ โ๏ธ โช๏ธโก๏ธ ๐ 1๏ธโฃ, ๐ ๐ช ๐ ๐ฌ `Response` ๐, ๐ `JSONResponse`, & โ ๐ ๐ ๐ ๐. |
|||
|
|||
๐ผ, โก๏ธ ๐ฌ ๐ ๐ ๐ โ๏ธ *โก ๐ ๏ธ* ๐ โ โน ๐ฌ, & ๐จ ๐บ๐ธ๐ ๐ ๐ 2๏ธโฃ0๏ธโฃ0๏ธโฃ "๐" ๐โ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ โซ๏ธ ๐ซ ๐ ๐ฌ. & ๐โ ๐ฌ ๐ซ ๐ โญ, โซ๏ธ โ ๐ซ, & ๐จ ๐บ๐ธ๐ ๐ ๐ 2๏ธโฃ0๏ธโฃ1๏ธโฃ "โ". |
|||
|
|||
๐ ๐, ๐ `JSONResponse`, & ๐จ ๐ ๐ ๐ค ๐, โ `status_code` ๐ ๐ ๐: |
|||
|
|||
```Python hl_lines="4 25" |
|||
{!../../../docs_src/additional_status_codes/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! warning |
|||
๐โ ๐ ๐จ `Response` ๐, ๐ ๐ผ ๐, โซ๏ธ ๐ ๐จ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ซ ๐ป โฎ๏ธ ๐ท, โ๏ธ. |
|||
|
|||
โ ๐ญ โซ๏ธ โ๏ธ ๐ ๐ ๐ โซ๏ธ โ๏ธ, & ๐ ๐ฒ โ ๐ป (๐ฅ ๐ โ๏ธ `JSONResponse`). |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.responses import JSONResponse`. |
|||
|
|||
**FastAPI** ๐ ๐ `starlette.responses` `fastapi.responses` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐. ๐ โฎ๏ธ `status`. |
|||
|
|||
## ๐ & ๐ ๏ธ ๐ฉบ |
|||
|
|||
๐ฅ ๐ ๐จ ๐ ๐ ๐ & ๐จ ๐, ๐ซ ๐ ๐ซ ๐ ๐ ๐ (๐ ๏ธ ๐ฉบ), โฉ๏ธ FastAPI ๐ซ โ๏ธ ๐ ๐ญ โช โซ๏ธโ ๐ ๐ถ ๐จ. |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ ๐ ๐ ๐, โ๏ธ: [๐ ๐จ](additional-responses.md){.internal-link target=_blank}. |
@ -0,0 +1,70 @@ |
|||
# ๐ง ๐ |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ ๐ ๐ฅ โ๏ธ ๐ ๐ง ๐ข โ๏ธ ๐. |
|||
|
|||
โ๏ธ ๐ค ๐ช ๐ผ ๐โ ๐ ๐ ๐ช โ ๐ข ๐ ๐, ๐ต โ๏ธ ๐ฃ ๐ ๐ ๐ข โ๏ธ ๐. |
|||
|
|||
โก๏ธ ๐ ๐ ๐ฅ ๐ โ๏ธ ๐ ๐ โ
๐ฅ ๐ข ๐ข `q` ๐ ๐ง ๐. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ช ๐ ๐ ๐ง ๐. |
|||
|
|||
## "๐ง๐ฒ" ๐ |
|||
|
|||
๐ ๐ค ๐ โ ๐ ๐ "๐ง๐ฒ". |
|||
|
|||
๐ซ ๐ โซ๏ธ (โ โช ๐ง๐ฒ), โ๏ธ ๐ ๐ ๐. |
|||
|
|||
๐, ๐ฅ ๐ฃ ๐ฉโ๐ฌ `__call__`: |
|||
|
|||
```Python hl_lines="10" |
|||
{!../../../docs_src/dependencies/tutorial011.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, ๐ `__call__` โซ๏ธโ **FastAPI** ๐ โ๏ธ โ
๐ ๐ข & ๐ง-๐, & ๐ โซ๏ธโ ๐ ๐ค ๐ถโโ๏ธ ๐ฒ ๐ข ๐ *โก ๐ ๏ธ ๐ข* โช. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
& ๐, ๐ฅ ๐ช โ๏ธ `__init__` ๐ฃ ๐ข ๐ ๐ ๐ฅ ๐ช โ๏ธ "๐" ๐: |
|||
|
|||
```Python hl_lines="7" |
|||
{!../../../docs_src/dependencies/tutorial011.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, **FastAPI** ๐ ๐ซ โฑ ๐ โ๏ธ ๐
๐ `__init__`, ๐ฅ ๐ โ๏ธ โซ๏ธ ๐ ๐ ๐. |
|||
|
|||
## โ ๐ |
|||
|
|||
๐ฅ ๐ช โ ๐ ๐ ๐ โฎ๏ธ: |
|||
|
|||
```Python hl_lines="16" |
|||
{!../../../docs_src/dependencies/tutorial011.py!} |
|||
``` |
|||
|
|||
& ๐ ๐ ๐ฅ ๐ช "๐" ๐ ๐, ๐ ๐ โ๏ธ `"bar"` ๐ โซ๏ธ, ๐ข `checker.fixed_content`. |
|||
|
|||
## โ๏ธ ๐ ๐ |
|||
|
|||
โคด๏ธ, ๐ฅ ๐ช โ๏ธ ๐ `checker` `Depends(checker)`, โฉ๏ธ `Depends(FixedContentQueryChecker)`, โฉ๏ธ ๐ ๐, `checker`, ๐ซ ๐ โซ๏ธ. |
|||
|
|||
& ๐โ โ ๐, **FastAPI** ๐ ๐ค ๐ `checker` ๐: |
|||
|
|||
```Python |
|||
checker(q="somequery") |
|||
``` |
|||
|
|||
...& ๐ถโโ๏ธ โซ๏ธโ ๐ ๐จ ๐ฒ ๐ ๐ *โก ๐ ๏ธ ๐ข* ๐ข `fixed_content_included`: |
|||
|
|||
```Python hl_lines="20" |
|||
{!../../../docs_src/dependencies/tutorial011.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ช ๐ ๐ญ. & โซ๏ธ ๐ช ๐ซ ๐ถ ๐ โ โซ๏ธ โ . |
|||
|
|||
๐ซ ๐ผ ๐ซ ๐
, โ๏ธ ๐ฆ โ โซ๏ธ ๐ ๐ท. |
|||
|
|||
๐ ๐ ๐โโ, ๐ค ๐ ๐ข ๐ ๐ ๏ธ ๐ ๐ ๐. |
|||
|
|||
๐ฅ ๐ ๐ค ๐ ๐, ๐ โช ๐ญ โ ๐ ๐ ๐งฐ ๐โโ ๐ท ๐. |
@ -0,0 +1,162 @@ |
|||
# ๐ ๐ (๐) ๐ฝ |
|||
|
|||
๐ ๐ช โ๏ธ <a href="https://github.com/encode/databases" class="external-link" target="_blank">`encode/databases`</a> โฎ๏ธ **FastAPI** ๐ ๐ฝ โ๏ธ `async` & `await`. |
|||
|
|||
โซ๏ธ ๐ โฎ๏ธ: |
|||
|
|||
* โณ |
|||
* โณ |
|||
* ๐ |
|||
|
|||
๐ ๐ผ, ๐ฅ ๐ โ๏ธ **๐**, โฉ๏ธ โซ๏ธ โ๏ธ ๐ ๐ & ๐ โ๏ธ ๐ ๏ธ ๐โ๐ฆบ. , ๐ ๐ช ๐ ๐ ๐ผ & ๐ โซ๏ธ. |
|||
|
|||
โช, ๐ ๐ญ ๐ธ, ๐ ๐ช ๐ โ๏ธ ๐ฝ ๐ฝ ๐ **โณ**. |
|||
|
|||
!!! tip |
|||
๐ ๐ช ๐ ๏ธ ๐ญ โช๏ธโก๏ธ ๐ ๐ ๐ธ๐ฒ ๐ ([๐ (๐) ๐ฝ](../tutorial/sql-databases.md){.internal-link target=_blank}), ๐ โ๏ธ ๐ ๐ข ๐ญ ๐ ๏ธ ๐ฝ, ๐ฌ ๐ **FastAPI** ๐. |
|||
|
|||
๐ ๐ ๐ซ โ ๐ ๐ญ, ๐ ๐ <a href="https://www.starlette.io/database/" class="external-link" target="_blank">๐</a>. |
|||
|
|||
## ๐ & โ ๐ `SQLAlchemy` |
|||
|
|||
* ๐ `SQLAlchemy`. |
|||
* โ `metadata` ๐. |
|||
* โ ๐ `notes` โ๏ธ `metadata` ๐. |
|||
|
|||
```Python hl_lines="4 14 16-22" |
|||
{!../../../docs_src/async_sql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ ๐ ๐ ๐ ๐ธ๐ฒ ๐. |
|||
|
|||
`databases` ๐ซ ๐จ ๐ณ ๐ฅ. |
|||
|
|||
## ๐ & โ ๐ `databases` |
|||
|
|||
* ๐ `databases`. |
|||
* โ `DATABASE_URL`. |
|||
* โ `database` ๐. |
|||
|
|||
```Python hl_lines="3 9 12" |
|||
{!../../../docs_src/async_sql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ ๐ ๐ฝ (โ
โณ), ๐ ๐ ๐ช ๐ `DATABASE_URL`. |
|||
|
|||
## โ ๐ |
|||
|
|||
๐ ๐ผ, ๐ฅ ๐ ๐ ๐ ๐ ๐, โ๏ธ ๐ญ, ๐ ๐ ๐ฒ ๐ โ ๐ซ โฎ๏ธ โ, ๐ ๏ธ โฎ๏ธ ๐ ๏ธ, โ๏ธ. |
|||
|
|||
๐ฅ, ๐ ๐ ๐ ๐ ๐, โถ๏ธ๏ธ โญ โถ๏ธ ๐ **FastAPI** ๐ธ. |
|||
|
|||
* โ `engine`. |
|||
* โ ๐ ๐ โช๏ธโก๏ธ `metadata` ๐. |
|||
|
|||
```Python hl_lines="25-28" |
|||
{!../../../docs_src/async_sql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
## โ ๐ท |
|||
|
|||
โ Pydantic ๐ท: |
|||
|
|||
* ๐ โ (`NoteIn`). |
|||
* ๐ ๐จ (`Note`). |
|||
|
|||
```Python hl_lines="31-33 36-39" |
|||
{!../../../docs_src/async_sql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ ๐ซ Pydantic ๐ท, ๐ข ๐ฝ ๐ โ, ๐ป (๐), & โ (๐). |
|||
|
|||
, ๐ ๐ ๐ช ๐ โซ๏ธ ๐ ๐ ๐ ๏ธ ๐ฉบ. |
|||
|
|||
## ๐ & ๐ |
|||
|
|||
* โ ๐ `FastAPI` ๐ธ. |
|||
* โ ๐ ๐โ๐ฆบ ๐ & ๐ โช๏ธโก๏ธ ๐ฝ. |
|||
|
|||
```Python hl_lines="42 45-47 50-52" |
|||
{!../../../docs_src/async_sql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
## โ ๐ |
|||
|
|||
โ *โก ๐ ๏ธ ๐ข* โ ๐: |
|||
|
|||
```Python hl_lines="55-58" |
|||
{!../../../docs_src/async_sql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! Note |
|||
๐ ๐ ๐ฅ ๐ โฎ๏ธ ๐ฝ โ๏ธ `await`, *โก ๐ ๏ธ ๐ข* ๐ฃ โฎ๏ธ `async`. |
|||
|
|||
### ๐ `response_model=List[Note]` |
|||
|
|||
โซ๏ธ โ๏ธ `typing.List`. |
|||
|
|||
๐ ๐ (& โ, ๐ป, โฝ) ๐ข ๐ฝ, `list` `Note`โ. |
|||
|
|||
## โ ๐ |
|||
|
|||
โ *โก ๐ ๏ธ ๐ข* โ ๐: |
|||
|
|||
```Python hl_lines="61-65" |
|||
{!../../../docs_src/async_sql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! Note |
|||
๐ ๐ ๐ฅ ๐ โฎ๏ธ ๐ฝ โ๏ธ `await`, *โก ๐ ๏ธ ๐ข* ๐ฃ โฎ๏ธ `async`. |
|||
|
|||
### ๐ `{**note.dict(), "id": last_record_id}` |
|||
|
|||
`note` Pydantic `Note` ๐. |
|||
|
|||
`note.dict()` ๐จ `dict` โฎ๏ธ ๐ฎ ๐ฝ, ๐ณ ๐: |
|||
|
|||
```Python |
|||
{ |
|||
"text": "Some note", |
|||
"completed": False, |
|||
} |
|||
``` |
|||
|
|||
โ๏ธ โซ๏ธ ๐ซ โ๏ธ `id` ๐. |
|||
|
|||
๐ฅ โ ๐ `dict`, ๐ ๐ ๐-๐ฒ ๐ซ โช๏ธโก๏ธ `note.dict()` โฎ๏ธ: |
|||
|
|||
```Python |
|||
{**note.dict()} |
|||
``` |
|||
|
|||
`**note.dict()` "unpacks" the key value pairs directly, so, `{**note.dict()}` would be, more or less, a copy of `note.dict()`. |
|||
|
|||
& โคด๏ธ, ๐ฅ โ ๐ ๐ `dict`, โ โ1๏ธโฃ ๐-๐ฒ ๐ซ: `"id": last_record_id`: |
|||
|
|||
```Python |
|||
{**note.dict(), "id": last_record_id} |
|||
``` |
|||
|
|||
, ๐ ๐ ๐จ ๐ ๐ณ ๐: |
|||
|
|||
```Python |
|||
{ |
|||
"id": 1, |
|||
"text": "Some note", |
|||
"completed": False, |
|||
} |
|||
``` |
|||
|
|||
## โ
โซ๏ธ |
|||
|
|||
๐ ๐ช ๐ ๐ ๐, & ๐ ๐ฉบ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
๐ค ๐ ๐ช ๐ ๐ ๐ ๐ ๏ธ ๐ & ๐ โฎ๏ธ โซ๏ธ: |
|||
|
|||
<img src="/img/tutorial/async-sql-databases/image01.png"> |
|||
|
|||
## ๐
โน |
|||
|
|||
๐ ๐ช โ ๐
๐ <a href="https://github.com/encode/databases" class="external-link" target="_blank">`encode/databases` ๐ฎ ๐ ๐</a>. |
@ -0,0 +1,92 @@ |
|||
# ๐ ๐ฏ |
|||
|
|||
๐ โ๏ธ โช ๐ โ ๐ฏ ๐ **FastAPI** ๐ธ โ๏ธ ๐ `TestClient`. ๐ ๐, ๐ โ๏ธ ๐ด ๐ โ โ ๐ ๐ฏ, ๐ต โ๏ธ `async` ๐ข. |
|||
|
|||
โ ๐ช โ๏ธ ๐ ๐ข ๐ ๐ฏ ๐ช โ , ๐ผ, ๐โ ๐ ๐ฌ ๐ ๐ฝ ๐. ๐ ๐ ๐ ๐ฏ ๐จ ๐จ ๐ FastAPI ๐ธ & โคด๏ธ โ ๐ ๐ ๐ฉโ๐ป โช โ โ ๐ฝ ๐ฝ, โช โ๏ธ ๐ ๐ฝ ๐. |
|||
|
|||
โก๏ธ ๐ โ ๐ฅ ๐ช โ ๐ ๐ท. |
|||
|
|||
## pytest.mark.anyio |
|||
|
|||
๐ฅ ๐ฅ ๐ ๐ค ๐ ๐ข ๐ ๐ฏ, ๐ ๐ฏ ๐ข โ๏ธ ๐. AnyIO ๐ ๐ ๐ ๐, ๐ โ ๐ฅ โ ๐ ๐ฏ ๐ข ๐ค ๐. |
|||
|
|||
## ๐ธ๐ฒ |
|||
|
|||
๐ฅ ๐ **FastAPI** ๐ธ โ๏ธ ๐ `def` ๐ข โฉ๏ธ `async def`, โซ๏ธ `async` ๐ธ ๐. |
|||
|
|||
`TestClient` ๐จ ๐ฑ ๐ ๐ค ๐ FastAPI ๐ธ ๐ ๐ `def` ๐ฏ ๐ข, โ๏ธ ๐ฉ โณ. โ๏ธ ๐ ๐ฑ ๐ซ ๐ท ๐ซ๐ ๐โ ๐ฅ โ๏ธ โซ๏ธ ๐ ๐ ๐ข. ๐ ๐ ๐ฏ ๐, ๐ฅ ๐ช ๐
โโ ๐ โ๏ธ `TestClient` ๐ ๐ ๐ฏ ๐ข. |
|||
|
|||
`TestClient` โ๏ธ ๐ <a href="https://www.python-httpx.org" class="external-link" target="_blank">๐ธ๐ฒ</a>, & โฉ๏ธ, ๐ฅ ๐ช โ๏ธ โซ๏ธ ๐ ๐ฏ ๐ ๏ธ. |
|||
|
|||
## ๐ผ |
|||
|
|||
๐
๐ผ, โก๏ธ ๐ค ๐ ๐ ๐ 1๏ธโฃ ๐ฌ [๐ฆ ๐ธ](../tutorial/bigger-applications.md){.internal-link target=_blank} & [๐ฌ](../tutorial/testing.md){.internal-link target=_blank}: |
|||
|
|||
``` |
|||
. |
|||
โโโ app |
|||
โย ย โโโ __init__.py |
|||
โย ย โโโ main.py |
|||
โย ย โโโ test_main.py |
|||
``` |
|||
|
|||
๐ `main.py` ๐ โ๏ธ: |
|||
|
|||
```Python |
|||
{!../../../docs_src/async_tests/main.py!} |
|||
``` |
|||
|
|||
๐ `test_main.py` ๐ โ๏ธ ๐ฏ `main.py`, โซ๏ธ ๐ช ๐ ๐ ๐ ๐: |
|||
|
|||
```Python |
|||
{!../../../docs_src/async_tests/test_main.py!} |
|||
``` |
|||
|
|||
## ๐ โซ๏ธ |
|||
|
|||
๐ ๐ช ๐ ๐ ๐ฏ ๐ ๐จ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pytest |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
## โน |
|||
|
|||
๐ `@pytest.mark.anyio` ๐ฌ โณ ๐ ๐ ๐ฏ ๐ข ๐ ๐ค ๐: |
|||
|
|||
```Python hl_lines="7" |
|||
{!../../../docs_src/async_tests/test_main.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ฏ ๐ข ๐ `async def` โฉ๏ธ `def` โญ ๐โ โ๏ธ `TestClient`. |
|||
|
|||
โคด๏ธ ๐ฅ ๐ช โ `AsyncClient` โฎ๏ธ ๐ฑ, & ๐จ ๐ ๐จ โซ๏ธ, โ๏ธ `await`. |
|||
|
|||
```Python hl_lines="9-10" |
|||
{!../../../docs_src/async_tests/test_main.py!} |
|||
``` |
|||
|
|||
๐ ๐: |
|||
|
|||
```Python |
|||
response = client.get('/') |
|||
``` |
|||
|
|||
...๐ ๐ฅ โ๏ธ โ ๐ ๐จ โฎ๏ธ `TestClient`. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ฅ โ๏ธ ๐/โ โฎ๏ธ ๐ `AsyncClient` - ๐จ ๐. |
|||
|
|||
## ๐ ๐ ๐ข ๐ค |
|||
|
|||
๐ฌ ๐ข ๐ ๐, ๐ ๐ช ๐ ๐ค (& `await`) ๐ `async` ๐ข โ๏ธ โช๏ธโก๏ธ ๐จ ๐จ ๐ FastAPI ๐ธ ๐ ๐ฏ, โซ๏ธโ ๐ ๐ ๐ค ๐ซ ๐ ๐ ๐ ๐. |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ โ `RuntimeError: Task attached to a different loop` ๐โ ๐ ๏ธ ๐ ๐ข ๐ค ๐ ๐ฏ (โ
๐โ โ๏ธ <a href="https://stackoverflow.com/questions/41584243/runtimeerror-task-attached-to-a-different-loop" class="external-link" target="_blank">โณ MotorClient</a>) ๐ญ ๐ ๐ ๐ ๐ช ๐ โฐ ๐ด ๐ ๐ ๐ข, โ
`'@app.on_event("startup")` โฒ. |
@ -0,0 +1,346 @@ |
|||
# โ
๐ณ |
|||
|
|||
โ , ๐ 5๏ธโฃ๐ ๐ช โ๏ธ **๐ณ** ๐ฝ ๐ Traefik โ๏ธ ๐ โฎ๏ธ ๐ณ ๐ ๐ฎ โ โก ๐ก ๐ ๐ซ ๐ ๐ ๐ธ. |
|||
|
|||
๐ซ ๐ผ ๐ ๐ช โ๏ธ `root_path` ๐ ๐ ๐ธ. |
|||
|
|||
`root_path` ๐ ๏ธ ๐ ๐ซ ๐ง (๐ FastAPI ๐ ๐, ๐ ๐). |
|||
|
|||
`root_path` โ๏ธ ๐ต ๐ซ ๐ฏ ๐ผ. |
|||
|
|||
& โซ๏ธ โ๏ธ ๐ ๐โ ๐ ๐ง-๐ธ. |
|||
|
|||
## ๐ณ โฎ๏ธ ๐ โก ๐ก |
|||
|
|||
โ๏ธ ๐ณ โฎ๏ธ ๐ โก ๐ก, ๐ ๐ผ, โ ๐ ๐ ๐ช ๐ฃ โก `/app` ๐ ๐, โ๏ธ โคด๏ธ, ๐ ๐ฎ ๐งฝ ๐ ๐ (๐ณ) ๐ ๐ ๐ฎ ๐ **FastAPI** ๐ธ ๐ฝ โก ๐ `/api/v1`. |
|||
|
|||
๐ ๐ผ, โฎ๏ธ โก `/app` ๐ ๐ค ๐ฆ `/api/v1/app`. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ โ ๐ค ๐ค `/app`. |
|||
|
|||
& ๐ณ ๐ **"โ"** **โก ๐ก** ๐ โ โญ ๐ถ ๐จ Uvicorn, ๐ง ๐ ๐ธ ๐ค ๐ โซ๏ธ ๐ฆ `/app`, ๐ ๐ ๐ซ โ๏ธ โน ๐ ๐ ๐ ๐ ๐ก `/api/v1`. |
|||
|
|||
๐ ๐ฅ, ๐ ๐ ๐ท ๐. |
|||
|
|||
โ๏ธ โคด๏ธ, ๐โ ๐ ๐ ๐ ๏ธ ๐ฉบ ๐ (๐ธ), โซ๏ธ ๐ โ ๐ค ๐ ๐ `/openapi.json`, โฉ๏ธ `/api/v1/openapi.json`. |
|||
|
|||
, ๐ธ (๐ ๐ ๐ฅ) ๐ ๐ ๐ `/openapi.json` & ๐ซ๐ ๐ช ๐ค ๐ ๐. |
|||
|
|||
โฉ๏ธ ๐ฅ โ๏ธ ๐ณ โฎ๏ธ โก ๐ก `/api/v1` ๐ ๐ฑ, ๐ธ ๐ช โ ๐ ๐ `/api/v1/openapi.json`. |
|||
|
|||
```mermaid |
|||
graph LR |
|||
|
|||
browser("Browser") |
|||
proxy["Proxy on http://0.0.0.0:9999/api/v1/app"] |
|||
server["Server on http://127.0.0.1:8000/app"] |
|||
|
|||
browser --> proxy |
|||
proxy --> server |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ข `0.0.0.0` ๐ โ๏ธ โ ๐ ๐ ๐ ๐ ๐ ๐ข ๐ช ๐ ๐ฐ/๐ฝ. |
|||
|
|||
๐ฉบ ๐ ๐ ๐ช ๐ ๐ ๐ฃ ๐ ๐ ๐ ๏ธ `server` ๐ `/api/v1` (โ
๐ณ). ๐ผ: |
|||
|
|||
```JSON hl_lines="4-8" |
|||
{ |
|||
"openapi": "3.0.2", |
|||
// More stuff here |
|||
"servers": [ |
|||
{ |
|||
"url": "/api/v1" |
|||
} |
|||
], |
|||
"paths": { |
|||
// More stuff here |
|||
} |
|||
} |
|||
``` |
|||
|
|||
๐ ๐ผ, "๐ณ" ๐ช ๐ณ ๐ **Traefik**. & ๐ฝ ๐ ๐ณ ๐ **Uvicorn**, ๐โโ ๐ FastAPI ๐ธ. |
|||
|
|||
### ๐ `root_path` |
|||
|
|||
๐ ๐, ๐ ๐ช โ๏ธ ๐ โธ ๐ `--root-path` ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --root-path /api/v1 |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ฅ ๐ โ๏ธ Hypercorn, โซ๏ธ โ๏ธ ๐ `--root-path`. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ซ ๐ง ๐ฌ `root_path` ๐ โ๏ธ ๐ผ. |
|||
|
|||
& `--root-path` ๐ โธ ๐ ๐ ๐ `root_path`. |
|||
|
|||
### โ
โฎ๏ธ `root_path` |
|||
|
|||
๐ ๐ช ๐ค โฎ๏ธ `root_path` โ๏ธ ๐ ๐ธ ๐ ๐จ, โซ๏ธ ๐ `scope` ๐ (๐ ๐ ๐ซ ๐). |
|||
|
|||
๐ฅ ๐ฅ โ
โซ๏ธ ๐ง ๐ฆ ๐ฏ. |
|||
|
|||
```Python hl_lines="8" |
|||
{!../../../docs_src/behind_a_proxy/tutorial001.py!} |
|||
``` |
|||
|
|||
โคด๏ธ, ๐ฅ ๐ โถ๏ธ Uvicorn โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --root-path /api/v1 |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐จ ๐ ๐ณ ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"message": "Hello World", |
|||
"root_path": "/api/v1" |
|||
} |
|||
``` |
|||
|
|||
### โ `root_path` FastAPI ๐ฑ |
|||
|
|||
๐, ๐ฅ ๐ ๐ซ โ๏ธ ๐ ๐ ๐ โธ ๐ ๐ `--root-path` โ๏ธ ๐, ๐ ๐ช โ `root_path` ๐ข ๐โ ๐ ๐ FastAPI ๐ฑ: |
|||
|
|||
```Python hl_lines="3" |
|||
{!../../../docs_src/behind_a_proxy/tutorial002.py!} |
|||
``` |
|||
|
|||
๐ถโโ๏ธ `root_path` `FastAPI` ๐ ๐ ๐ถโโ๏ธ `--root-path` ๐ โธ ๐ Uvicorn โ๏ธ Hypercorn. |
|||
|
|||
### ๐ `root_path` |
|||
|
|||
โ๏ธ ๐คฏ ๐ ๐ฝ (Uvicorn) ๐ ๐ซ โ๏ธ ๐ `root_path` ๐ณ ๐ ๐ ๐ถโโ๏ธ โซ๏ธ ๐ฑ. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ถ โฎ๏ธ ๐ ๐ฅ <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000/app</a> ๐ ๐ ๐ ๐ ๐จ: |
|||
|
|||
```JSON |
|||
{ |
|||
"message": "Hello World", |
|||
"root_path": "/api/v1" |
|||
} |
|||
``` |
|||
|
|||
, โซ๏ธ ๐ ๐ซ โ ๐ `http://127.0.0.1:8000/api/v1/app`. |
|||
|
|||
Uvicorn ๐ โ ๐ณ ๐ Uvicorn `http://127.0.0.1:8000/app`, & โคด๏ธ โซ๏ธ ๐ ๐ณ ๐ฏ ๐ฎ โ `/api/v1` ๐ก ๐ ๐. |
|||
|
|||
## ๐ ๐ณ โฎ๏ธ ๐ โก ๐ก |
|||
|
|||
โ๏ธ ๐คฏ ๐ ๐ณ โฎ๏ธ ๐ โก ๐ก ๐ด 1๏ธโฃ ๐ ๐ โซ๏ธ. |
|||
|
|||
๐ฒ ๐ ๐ผ ๐ข ๐ ๐ ๐ณ ๐ซ โ๏ธ ๐ โก ๐ก. |
|||
|
|||
๐ผ ๐ ๐ (๐ต ๐ โก ๐ก), ๐ณ ๐ ๐ ๐ ๐ณ ๐ `https://myawesomeapp.com`, & โคด๏ธ ๐ฅ ๐ฅ ๐ถ `https://myawesomeapp.com/api/v1/app` & ๐ ๐ฝ (โ
Uvicorn) ๐ ๐ `http://127.0.0.1:8000` ๐ณ (๐ต ๐ โก ๐ก) ๐ ๐ Uvicorn ๐ โก: `http://127.0.0.1:8000/api/v1/app`. |
|||
|
|||
## ๐ฌ ๐ โฎ๏ธ Traefik |
|||
|
|||
๐ ๐ช ๐ช ๐ ๐ฅผ ๐ โฎ๏ธ ๐ โก ๐ก โ๏ธ <a href="https://docs.traefik.io/" class="external-link" target="_blank">Traefik</a>. |
|||
|
|||
<a href="https://github.com/containous/traefik/releases" class="external-link" target="_blank">โฌ Traefik</a>, โซ๏ธ ๐ ๐ฑ, ๐ ๐ช โ ๐ ๐ & ๐ โซ๏ธ ๐ โช๏ธโก๏ธ ๐ถ. |
|||
|
|||
โคด๏ธ โ ๐ `traefik.toml` โฎ๏ธ: |
|||
|
|||
```TOML hl_lines="3" |
|||
[entryPoints] |
|||
[entryPoints.http] |
|||
address = ":9999" |
|||
|
|||
[providers] |
|||
[providers.file] |
|||
filename = "routes.toml" |
|||
``` |
|||
|
|||
๐ ๐ฌ Traefik ๐ ๐ โด 9๏ธโฃ9๏ธโฃ9๏ธโฃ9๏ธโฃ & โ๏ธ โ1๏ธโฃ ๐ `routes.toml`. |
|||
|
|||
!!! tip |
|||
๐ฅ โ๏ธ โด 9๏ธโฃ9๏ธโฃ9๏ธโฃ9๏ธโฃ โฉ๏ธ ๐ฉ ๐บ๐ธ๐ โด 8๏ธโฃ0๏ธโฃ ๐ ๐ ๐ซ โ๏ธ ๐ โซ๏ธ โฎ๏ธ ๐ก (`sudo`) ๐. |
|||
|
|||
๐ โ ๐ ๐ ๐ `routes.toml`: |
|||
|
|||
```TOML hl_lines="5 12 20" |
|||
[http] |
|||
[http.middlewares] |
|||
|
|||
[http.middlewares.api-stripprefix.stripPrefix] |
|||
prefixes = ["/api/v1"] |
|||
|
|||
[http.routers] |
|||
|
|||
[http.routers.app-http] |
|||
entryPoints = ["http"] |
|||
service = "app" |
|||
rule = "PathPrefix(`/api/v1`)" |
|||
middlewares = ["api-stripprefix"] |
|||
|
|||
[http.services] |
|||
|
|||
[http.services.app] |
|||
[http.services.app.loadBalancer] |
|||
[[http.services.app.loadBalancer.servers]] |
|||
url = "http://127.0.0.1:8000" |
|||
``` |
|||
|
|||
๐ ๐ ๐ Traefik โ๏ธ โก ๐ก `/api/v1`. |
|||
|
|||
& โคด๏ธ โซ๏ธ ๐ โ ๐ฎ ๐จ ๐ Uvicorn ๐โโ ๐ `http://127.0.0.1:8000`. |
|||
|
|||
๐ โถ๏ธ Traefik: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ ./traefik --configFile=traefik.toml |
|||
|
|||
INFO[0000] Configuration loaded from file: /home/user/awesomeapi/traefik.toml |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
& ๐ โถ๏ธ ๐ ๐ฑ โฎ๏ธ Uvicorn, โ๏ธ `--root-path` ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --root-path /api/v1 |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
### โ
๐จ |
|||
|
|||
๐, ๐ฅ ๐ ๐ถ ๐ โฎ๏ธ โด Uvicorn: <a href="http://127.0.0.1:8000/app" class="external-link" target="_blank">http://127.0.0.1:8000/app</a>, ๐ ๐ ๐ ๐ ๐จ: |
|||
|
|||
```JSON |
|||
{ |
|||
"message": "Hello World", |
|||
"root_path": "/api/v1" |
|||
} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ โ๏ธ ๐ ๐ โซ๏ธ `http://127.0.0.1:8000/app` โซ๏ธ ๐ฆ `root_path` `/api/v1`, โ โช๏ธโก๏ธ ๐ `--root-path`. |
|||
|
|||
& ๐ ๐ ๐ โฎ๏ธ โด Traefik, โ
โก ๐ก: <a href="http://127.0.0.1:9999/api/v1/app" class="external-link" target="_blank">http://127.0.0.1:9999/api/v1/app</a>. |
|||
|
|||
๐ฅ ๐ค ๐ ๐จ: |
|||
|
|||
```JSON |
|||
{ |
|||
"message": "Hello World", |
|||
"root_path": "/api/v1" |
|||
} |
|||
``` |
|||
|
|||
โ๏ธ ๐ ๐ฐ ๐ โฎ๏ธ ๐ก โก ๐ ๐ณ: `/api/v1`. |
|||
|
|||
โ๏ธ, ๐ญ ๐ฅ ๐ ๐ฑ ๐ ๐ ๐ฑ ๐ ๐ณ, โฌ โฎ๏ธ โก ๐ก `/app/v1` "โ" 1๏ธโฃ. |
|||
|
|||
& โฌ ๐ต โก ๐ก (`http://127.0.0.1:8000/app`), ๐ Uvicorn ๐, ๐ ๐ฏ _๐ณ_ (Traefik) ๐ โซ๏ธ. |
|||
|
|||
๐ ๐ฆ โ ๐ณ (Traefik) โ๏ธ โก ๐ก & โ ๐ฝ (Uvicorn) โ๏ธ `root_path` โช๏ธโก๏ธ ๐ `--root-path`. |
|||
|
|||
### โ
๐ฉบ ๐ |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐. ๐ถ |
|||
|
|||
"๐" ๐ ๐ ๐ฑ ๐ ๐ ๐ณ โฎ๏ธ โก ๐ก ๐ ๐ฅ ๐ฌ. , ๐ฅ ๐ โ, ๐ฅ ๐ ๐ ๐ฉบ ๐ ๐ฆ Uvicorn ๐, ๐ต โก ๐ก ๐, โซ๏ธ ๐ ๐ซ ๐ท, โฉ๏ธ โซ๏ธ โ ๐ ๐ ๐ณ. |
|||
|
|||
๐ ๐ช โ
โซ๏ธ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>: |
|||
|
|||
<img src="/img/tutorial/behind-a-proxy/image01.png"> |
|||
|
|||
โ๏ธ ๐ฅ ๐ฅ ๐ ๐ฉบ ๐ "๐" ๐ โ๏ธ ๐ณ โฎ๏ธ โด `9999`, `/api/v1/docs`, โซ๏ธ ๐ท โ โ ๐ถ |
|||
|
|||
๐ ๐ช โ
โซ๏ธ <a href="http://127.0.0.1:9999/api/v1/docs" class="external-link" target="_blank">http://127.0.0.1:9999/api/v1/docs</a>: |
|||
|
|||
<img src="/img/tutorial/behind-a-proxy/image02.png"> |
|||
|
|||
โถ๏ธ๏ธ ๐ฅ ๐ โซ๏ธ. ๐ถ ๐ถ |
|||
|
|||
๐ โฉ๏ธ FastAPI โ๏ธ ๐ `root_path` โ ๐ข `server` ๐ โฎ๏ธ ๐ ๐ `root_path`. |
|||
|
|||
## ๐ ๐ฝ |
|||
|
|||
!!! warning |
|||
๐ ๐
๐ง โ๏ธ ๐ผ. ๐ญ ๐ ๐ถ โซ๏ธ. |
|||
|
|||
๐ข, **FastAPI** ๐ โ `server` ๐ ๐ โฎ๏ธ ๐ `root_path`. |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ ๐ ๐ `servers`, ๐ผ ๐ฅ ๐ ๐ *๐* ๐ฉบ ๐ ๐ โฎ๏ธ ๐ & ๐ญ ๐. |
|||
|
|||
๐ฅ ๐ ๐ถโโ๏ธ ๐ ๐ `servers` & ๐ค `root_path` (โฉ๏ธ ๐ ๐ ๏ธ ๐จโโคโ๐จ โ
๐ณ), **FastAPI** ๐ ๐ฉ "๐ฝ" โฎ๏ธ ๐ `root_path` โถ๏ธ ๐. |
|||
|
|||
๐ผ: |
|||
|
|||
```Python hl_lines="4-7" |
|||
{!../../../docs_src/behind_a_proxy/tutorial003.py!} |
|||
``` |
|||
|
|||
๐ ๐ ๐ ๐ ๐: |
|||
|
|||
```JSON hl_lines="5-7" |
|||
{ |
|||
"openapi": "3.0.2", |
|||
// More stuff here |
|||
"servers": [ |
|||
{ |
|||
"url": "/api/v1" |
|||
}, |
|||
{ |
|||
"url": "https://stag.example.com", |
|||
"description": "Staging environment" |
|||
}, |
|||
{ |
|||
"url": "https://prod.example.com", |
|||
"description": "Production environment" |
|||
} |
|||
], |
|||
"paths": { |
|||
// More stuff here |
|||
} |
|||
} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐-๐ ๐ฝ โฎ๏ธ `url` ๐ฒ `/api/v1`, โ โช๏ธโก๏ธ `root_path`. |
|||
|
|||
๐ฉบ ๐ <a href="http://127.0.0.1:9999/api/v1/docs" class="external-link" target="_blank">http://127.0.0.1:9999/api/v1/docs</a> โซ๏ธ ๐ ๐ ๐: |
|||
|
|||
<img src="/img/tutorial/behind-a-proxy/image03.png"> |
|||
|
|||
!!! tip |
|||
๐ฉบ ๐ ๐ ๐ โฎ๏ธ ๐ฝ ๐ ๐ ๐. |
|||
|
|||
### โ ๐ง ๐ฝ โช๏ธโก๏ธ `root_path` |
|||
|
|||
๐ฅ ๐ ๐ซ ๐ **FastAPI** ๐ ๐ง ๐ฝ โ๏ธ `root_path`, ๐ ๐ช โ๏ธ ๐ข `root_path_in_servers=False`: |
|||
|
|||
```Python hl_lines="9" |
|||
{!../../../docs_src/behind_a_proxy/tutorial004.py!} |
|||
``` |
|||
|
|||
& โคด๏ธ โซ๏ธ ๐ ๐ซ ๐ โซ๏ธ ๐ ๐. |
|||
|
|||
## ๐ ๐ง-๐ธ |
|||
|
|||
๐ฅ ๐ ๐ช ๐ป ๐ง-๐ธ (๐ฌ [๐ง ๐ธ - ๐ป](./sub-applications.md){.internal-link target=_blank}) โช โ๏ธ ๐ณ โฎ๏ธ `root_path`, ๐ ๐ช โซ๏ธ ๐, ๐ ๐ โ. |
|||
|
|||
FastAPI ๐ ๐ โ๏ธ `root_path` ๐, โซ๏ธ ๐ ๐ท. ๐ถ |
@ -0,0 +1,300 @@ |
|||
# ๐ ๐จ - ๐ธ, ๐, ๐, ๐ |
|||
|
|||
๐ข, **FastAPI** ๐ ๐จ ๐จ โ๏ธ `JSONResponse`. |
|||
|
|||
๐ ๐ช ๐ โซ๏ธ ๐ฌ `Response` ๐ ๐ [๐จ ๐จ ๐](response-directly.md){.internal-link target=_blank}. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐จ `Response` ๐, ๐ ๐ ๐ซ ๐ ๐, & ๐งพ ๐ ๐ซ ๐ ๐ (๐ผ, ๐ ๐ฏ "๐ป ๐", ๐บ๐ธ๐ ๐ `Content-Type` ๐ ๐ ๐). |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ฃ `Response` ๐ ๐ ๐ โ๏ธ, *โก ๐ ๏ธ ๐จโ๐จ*. |
|||
|
|||
๐ ๐ ๐ ๐จ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ ๐ข* ๐ ๐ฎ ๐ ๐ `Response`. |
|||
|
|||
& ๐ฅ ๐ `Response` โ๏ธ ๐ป ๐ป ๐ (`application/json`), ๐ ๐ผ โฎ๏ธ `JSONResponse` & `UJSONResponse`, ๐ฝ ๐ ๐จ ๐ ๐ ๐ (& โฝ) โฎ๏ธ ๐ Pydantic `response_model` ๐ ๐ ๐ฃ *โก ๐ ๏ธ ๐จโ๐จ*. |
|||
|
|||
!!! note |
|||
๐ฅ ๐ โ๏ธ ๐จ ๐ โฎ๏ธ ๐
โโ ๐ป ๐, FastAPI ๐ โ ๐ ๐จ โ๏ธ ๐
โโ ๐, โซ๏ธ ๐ ๐ซ ๐ ๐จ ๐ ๐ฎ ๐ ๐ ๐ฉบ. |
|||
|
|||
## โ๏ธ `ORJSONResponse` |
|||
|
|||
๐ผ, ๐ฅ ๐ โ ๐ญ, ๐ ๐ช โ & โ๏ธ <a href="https://github.com/ijl/orjson" class="external-link" target="_blank">`orjson`</a> & โ ๐จ `ORJSONResponse`. |
|||
|
|||
๐ `Response` ๐ (๐ง-๐) ๐ ๐ โ๏ธ & ๐ฃ โซ๏ธ *โก ๐ ๏ธ ๐จโ๐จ*. |
|||
|
|||
โญ ๐จ, ๐จ `Response` ๐ ๐
โฉ ๐ ๐ฌ ๐. |
|||
|
|||
๐ โฉ๏ธ ๐ข, FastAPI ๐ โ ๐ ๐ฌ ๐ & โ ๐ญ โซ๏ธ ๐ป โฎ๏ธ ๐ป, โ๏ธ ๐ [๐ป ๐ ๐ข](../tutorial/encoder.md){.internal-link target=_blank} ๐ฌ ๐ฐ. ๐ โซ๏ธโ โ ๐ ๐จ **โ ๐**, ๐ผ ๐ฝ ๐ท. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ฏ ๐ ๐ ๐ ๐ ๐ฌ **๐ป โฎ๏ธ ๐ป**, ๐ ๐ช ๐ถโโ๏ธ โซ๏ธ ๐ ๐จ ๐ & โ โ ๐ฅ ๐ FastAPI ๐ โ๏ธ ๐ถโโ๏ธ ๐ ๐จ ๐ ๐ `jsonable_encoder` โญ ๐ถโโ๏ธ โซ๏ธ ๐จ ๐. |
|||
|
|||
```Python hl_lines="2 7" |
|||
{!../../../docs_src/custom_response/tutorial001b.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
๐ข `response_class` ๐ โ๏ธ ๐ฌ "๐ป ๐" ๐จ. |
|||
|
|||
๐ ๐ผ, ๐บ๐ธ๐ ๐ `Content-Type` ๐ โ `application/json`. |
|||
|
|||
& โซ๏ธ ๐ ๐ โ
๐. |
|||
|
|||
!!! tip |
|||
`ORJSONResponse` โณ ๐ด ๐ช FastAPI, ๐ซ ๐. |
|||
|
|||
## ๐ธ ๐จ |
|||
|
|||
๐จ ๐จ โฎ๏ธ ๐ธ ๐ โช๏ธโก๏ธ **FastAPI**, โ๏ธ `HTMLResponse`. |
|||
|
|||
* ๐ `HTMLResponse`. |
|||
* ๐ถโโ๏ธ `HTMLResponse` ๐ข `response_class` ๐ *โก ๐ ๏ธ ๐จโ๐จ*. |
|||
|
|||
```Python hl_lines="2 7" |
|||
{!../../../docs_src/custom_response/tutorial002.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
๐ข `response_class` ๐ โ๏ธ ๐ฌ "๐ป ๐" ๐จ. |
|||
|
|||
๐ ๐ผ, ๐บ๐ธ๐ ๐ `Content-Type` ๐ โ `text/html`. |
|||
|
|||
& โซ๏ธ ๐ ๐ โ
๐. |
|||
|
|||
### ๐จ `Response` |
|||
|
|||
๐ [๐จ ๐จ ๐](response-directly.md){.internal-link target=_blank}, ๐ ๐ช ๐ ๐จ ๐ ๐ *โก ๐ ๏ธ*, ๐ฌ โซ๏ธ. |
|||
|
|||
๐ ๐ผ โช๏ธโก๏ธ ๐, ๐ฌ `HTMLResponse`, ๐ช ๐ ๐: |
|||
|
|||
```Python hl_lines="2 7 19" |
|||
{!../../../docs_src/custom_response/tutorial003.py!} |
|||
``` |
|||
|
|||
!!! warning |
|||
`Response` ๐จ ๐ ๐ *โก ๐ ๏ธ ๐ข* ๐ ๐ซ ๐ ๐ (๐ผ, `Content-Type` ๐ ๐ซ ๐) & ๐ ๐ซ โญ ๐ง ๐ ๐ฉบ. |
|||
|
|||
!!! info |
|||
โ๏ธ, โ `Content-Type` ๐, ๐ ๐, โ๏ธ, ๐ ๐ โช๏ธโก๏ธ `Response` ๐ ๐ ๐จ. |
|||
|
|||
### ๐ ๐ & ๐ `Response` |
|||
|
|||
๐ฅ ๐ ๐ ๐ ๐จ โช๏ธโก๏ธ ๐ ๐ข โ๏ธ ๐ ๐ฐ ๐ "๐ป ๐" ๐, ๐ ๐ช โ๏ธ `response_class` ๐ข & ๐จ `Response` ๐. |
|||
|
|||
`response_class` ๐ โคด๏ธ โ๏ธ ๐ด ๐ ๐ *โก ๐ ๏ธ*, โ๏ธ ๐ `Response` ๐ โ๏ธ. |
|||
|
|||
#### ๐จ `HTMLResponse` ๐ |
|||
|
|||
๐ผ, โซ๏ธ ๐ช ๐ณ ๐: |
|||
|
|||
```Python hl_lines="7 21 23" |
|||
{!../../../docs_src/custom_response/tutorial004.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, ๐ข `generate_html_response()` โช ๐ & ๐จ `Response` โฉ๏ธ ๐ฌ ๐ธ `str`. |
|||
|
|||
๐ฌ ๐ ๐ค `generate_html_response()`, ๐ โช ๐ฌ `Response` ๐ ๐ ๐ ๐ข **FastAPI** ๐ญ. |
|||
|
|||
โ๏ธ ๐ ๐ถโโ๏ธ `HTMLResponse` `response_class` ๐โโ๏ธ, **FastAPI** ๐ ๐ญ โ ๐ โซ๏ธ ๐ & ๐ ๐ฉบ ๐ธ โฎ๏ธ `text/html`: |
|||
|
|||
<img src="/img/tutorial/custom-response/image01.png"> |
|||
|
|||
## ๐ช ๐จ |
|||
|
|||
๐ฅ ๐ช ๐จ. |
|||
|
|||
โ๏ธ ๐คฏ ๐ ๐ ๐ช โ๏ธ `Response` ๐จ ๐ณ ๐, โ๏ธ โ ๐ ๐ง-๐. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.responses import HTMLResponse`. |
|||
|
|||
**FastAPI** ๐ ๐ `starlette.responses` `fastapi.responses` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐. |
|||
|
|||
### `Response` |
|||
|
|||
๐ `Response` ๐, ๐ ๐ ๐จ ๐ โช๏ธโก๏ธ โซ๏ธ. |
|||
|
|||
๐ ๐ช ๐จ โซ๏ธ ๐. |
|||
|
|||
โซ๏ธ ๐ซ ๐ ๐ข: |
|||
|
|||
* `content` - `str` โ๏ธ `bytes`. |
|||
* `status_code` - `int` ๐บ๐ธ๐ ๐ ๐. |
|||
* `headers` - `dict` ๐ป. |
|||
* `media_type` - `str` ๐ค ๐ป ๐. ๐คถ โ. `"text/html"`. |
|||
|
|||
FastAPI (๐ค ๐) ๐ ๐ ๐ ๐-๐ ๐. โซ๏ธ ๐ ๐ ๐-๐ ๐, โ๏ธ ๐ = & ๐ = โ ๐. |
|||
|
|||
```Python hl_lines="1 18" |
|||
{!../../../docs_src/response_directly/tutorial002.py!} |
|||
``` |
|||
|
|||
### `HTMLResponse` |
|||
|
|||
โ โ โ๏ธ ๐ข & ๐จ ๐ธ ๐จ, ๐ โ ๐. |
|||
|
|||
### `PlainTextResponse` |
|||
|
|||
โ โ โ๏ธ ๐ข & ๐จ โ
โ ๐จ. |
|||
|
|||
```Python hl_lines="2 7 9" |
|||
{!../../../docs_src/custom_response/tutorial005.py!} |
|||
``` |
|||
|
|||
### `JSONResponse` |
|||
|
|||
โ ๐ฝ & ๐จ `application/json` ๐ ๐จ. |
|||
|
|||
๐ ๐ข ๐จ โ๏ธ **FastAPI**, ๐ โ ๐. |
|||
|
|||
### `ORJSONResponse` |
|||
|
|||
โฉ ๐ ๐ป ๐จ โ๏ธ <a href="https://github.com/ijl/orjson" class="external-link" target="_blank">`orjson`</a>, ๐ โ ๐. |
|||
|
|||
### `UJSONResponse` |
|||
|
|||
๐ ๐ป ๐จ โ๏ธ <a href="https://github.com/ultrajson/ultrajson" class="external-link" target="_blank">`ujson`</a>. |
|||
|
|||
!!! warning |
|||
`ujson` ๐ ๐ ๐ ๐ ๐-๐ ๏ธ โ โซ๏ธ ๐ต ๐-๐ผ. |
|||
|
|||
```Python hl_lines="2 7" |
|||
{!../../../docs_src/custom_response/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
โซ๏ธ ๐ช ๐ `ORJSONResponse` ๐ช โฉ ๐. |
|||
|
|||
### `RedirectResponse` |
|||
|
|||
๐จ ๐บ๐ธ๐ โ. โ๏ธ 3๏ธโฃ0๏ธโฃ7๏ธโฃ ๐ ๐ (๐ โ) ๐ข. |
|||
|
|||
๐ ๐ช ๐จ `RedirectResponse` ๐: |
|||
|
|||
```Python hl_lines="2 9" |
|||
{!../../../docs_src/custom_response/tutorial006.py!} |
|||
``` |
|||
|
|||
--- |
|||
|
|||
โ๏ธ ๐ ๐ช โ๏ธ โซ๏ธ `response_class` ๐ข: |
|||
|
|||
|
|||
```Python hl_lines="2 7 9" |
|||
{!../../../docs_src/custom_response/tutorial006b.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ ๐, โคด๏ธ ๐ ๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ* ๐ข. |
|||
|
|||
๐ ๐ผ, `status_code` โ๏ธ ๐ ๐ข 1๏ธโฃ `RedirectResponse`, โ `307`. |
|||
|
|||
--- |
|||
|
|||
๐ ๐ช โ๏ธ `status_code` ๐ข ๐ โฎ๏ธ `response_class` ๐ข: |
|||
|
|||
```Python hl_lines="2 7 9" |
|||
{!../../../docs_src/custom_response/tutorial006c.py!} |
|||
``` |
|||
|
|||
### `StreamingResponse` |
|||
|
|||
โ ๐ ๐ โ๏ธ ๐ ๐/๐ป & ๐ ๐จ ๐ช. |
|||
|
|||
```Python hl_lines="2 14" |
|||
{!../../../docs_src/custom_response/tutorial007.py!} |
|||
``` |
|||
|
|||
#### โ๏ธ `StreamingResponse` โฎ๏ธ ๐-๐ ๐ |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐-๐ ๐ (โ
๐ ๐จ `open()`), ๐ ๐ช โ ๐ ๐ข ๐ ๐คญ ๐ ๐-๐ ๐. |
|||
|
|||
๐ ๐, ๐ ๐ซ โ๏ธ โ โซ๏ธ ๐ ๐ฅ ๐พ, & ๐ ๐ช ๐ถโโ๏ธ ๐ ๐ ๐ข `StreamingResponse`, & ๐จ โซ๏ธ. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ โฎ๏ธ โ ๐พ, ๐น ๐ญ, & ๐. |
|||
|
|||
```{ .python .annotate hl_lines="2 10-12 14" } |
|||
{!../../../docs_src/custom_response/tutorial008.py!} |
|||
``` |
|||
|
|||
1๏ธโฃ. ๐ ๐ ๐ข. โซ๏ธ "๐ ๐ข" โฉ๏ธ โซ๏ธ ๐ `yield` ๐ ๐. |
|||
2๏ธโฃ. โ๏ธ `with` ๐ซ, ๐ฅ โ ๐ญ ๐ ๐-๐ ๐ ๐ช โฎ๏ธ ๐ ๐ข ๐จ. , โฎ๏ธ โซ๏ธ ๐ ๐จ ๐จ. |
|||
3๏ธโฃ. ๐ `yield from` ๐ฌ ๐ข ๐ ๐คญ ๐ ๐ ๐ `file_like`. & โคด๏ธ, ๐ ๐ ๐, ๐พ ๐ ๐ ๐ โช๏ธโก๏ธ ๐ ๐ ๐ข. |
|||
|
|||
, โซ๏ธ ๐ ๐ข ๐ ๐จ "๐ญ" ๐ท ๐ณ ๐ ๐. |
|||
|
|||
๐จ โซ๏ธ ๐ ๐, ๐ฅ ๐ช ๐ฎ โซ๏ธ `with` ๐ซ, & ๐ ๐, ๐ ๐ โซ๏ธ ๐ช โฎ๏ธ ๐. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ฅ ๐ฅ โ๏ธ ๐ฉ `open()` ๐ ๐ซ ๐โ๐ฆบ `async` & `await`, ๐ฅ ๐ฃ โก ๐ ๏ธ โฎ๏ธ ๐ `def`. |
|||
|
|||
### `FileResponse` |
|||
|
|||
๐ ๐ ๐ ๐จ. |
|||
|
|||
โ ๐ โ โ ๐ ๐ ๐ ๐จ ๐: |
|||
|
|||
* `path` - ๐ ๐ ๐. |
|||
* `headers` - ๐ ๐ ๐ ๐, ๐. |
|||
* `media_type` - ๐ป ๐ค ๐ป ๐. ๐ฅ ๐ข, ๐ โ๏ธ โก ๐ โ๏ธ ๐ ๐ป ๐. |
|||
* `filename` - ๐ฅ โ, ๐ ๐ ๐ ๐จ `Content-Disposition`. |
|||
|
|||
๐ ๐จ ๐ ๐ โ `Content-Length`, `Last-Modified` & `ETag` ๐. |
|||
|
|||
```Python hl_lines="2 10" |
|||
{!../../../docs_src/custom_response/tutorial009.py!} |
|||
``` |
|||
|
|||
๐ ๐ช โ๏ธ `response_class` ๐ข: |
|||
|
|||
```Python hl_lines="2 8 10" |
|||
{!../../../docs_src/custom_response/tutorial009b.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, ๐ ๐ช ๐จ ๐ โก ๐ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ* ๐ข. |
|||
|
|||
## ๐ ๐จ ๐ |
|||
|
|||
๐ ๐ช โ ๐ ๐ ๐ ๐จ ๐, ๐ โช๏ธโก๏ธ `Response` & โ๏ธ โซ๏ธ. |
|||
|
|||
๐ผ, โก๏ธ ๐ฌ ๐ ๐ ๐ โ๏ธ <a href="https://github.com/ijl/orjson" class="external-link" target="_blank">`orjson`</a>, โ๏ธ โฎ๏ธ ๐ โ ๐ซ โ๏ธ ๐ `ORJSONResponse` ๐. |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ โซ๏ธ ๐จ ๐ & ๐ ๐ป, ๐ ๐ โ๏ธ Orjson ๐ `orjson.OPT_INDENT_2`. |
|||
|
|||
๐ ๐ช โ `CustomORJSONResponse`. ๐ ๐ ๐ โ๏ธ โ `Response.render(content)` ๐ฉโ๐ฌ ๐ ๐จ ๐ `bytes`: |
|||
|
|||
```Python hl_lines="9-14 17" |
|||
{!../../../docs_src/custom_response/tutorial009c.py!} |
|||
``` |
|||
|
|||
๐ โฉ๏ธ ๐ฌ: |
|||
|
|||
```json |
|||
{"message": "Hello World"} |
|||
``` |
|||
|
|||
...๐ ๐จ ๐ ๐จ: |
|||
|
|||
```json |
|||
{ |
|||
"message": "Hello World" |
|||
} |
|||
``` |
|||
|
|||
โ๏ธ, ๐ ๐ ๐ฒ ๐ ๐
๐ ๐ โ ๐ ๐ ๐ โ ๐ป. ๐ถ |
|||
|
|||
## ๐ข ๐จ ๐ |
|||
|
|||
๐โ ๐ **FastAPI** ๐ ๐ โ๏ธ `APIRouter` ๐ ๐ช โ โ ๐จ ๐ โ๏ธ ๐ข. |
|||
|
|||
๐ข ๐ ๐ฌ ๐ `default_response_class`. |
|||
|
|||
๐ผ ๐, **FastAPI** ๐ โ๏ธ `ORJSONResponse` ๐ข, ๐ *โก ๐ ๏ธ*, โฉ๏ธ `JSONResponse`. |
|||
|
|||
```Python hl_lines="2 4" |
|||
{!../../../docs_src/custom_response/tutorial010.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ช ๐ `response_class` *โก ๐ ๏ธ* โญ. |
|||
|
|||
## ๐ ๐งพ |
|||
|
|||
๐ ๐ช ๐ฃ ๐ป ๐ & ๐ ๐ โน ๐ โ๏ธ `responses`: [๐ ๐จ ๐](additional-responses.md){.internal-link target=_blank}. |
@ -0,0 +1,98 @@ |
|||
# โ๏ธ ๐ป |
|||
|
|||
FastAPI ๐ ๐ ๐ **Pydantic**, & ๐ค โ๏ธ ๐ ๐ โ โ๏ธ Pydantic ๐ท ๐ฃ ๐จ & ๐จ. |
|||
|
|||
โ๏ธ FastAPI ๐โ๐ฆบ โ๏ธ <a href="https://docs.python.org/3/library/dataclasses.html" class="external-link" target="_blank">`dataclasses`</a> ๐ ๐: |
|||
|
|||
```Python hl_lines="1 7-12 19-20" |
|||
{!../../../docs_src/dataclasses/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ ๐โ๐ฆบ ๐ **Pydantic**, โซ๏ธ โ๏ธ <a href="https://pydantic-docs.helpmanual.io/usage/dataclasses/#use-of-stdlib-dataclasses-with-basemodel" class="external-link" target="_blank">๐ ๐โ๐ฆบ `dataclasses`</a>. |
|||
|
|||
, โฎ๏ธ ๐ ๐ ๐ ๐ซ โ๏ธ Pydantic ๐ฏ, FastAPI โ๏ธ Pydantic ๐ ๐ ๐ฉ ๐ป Pydantic ๐ ๐ ๐ป. |
|||
|
|||
& โ๏ธ, โซ๏ธ ๐โ๐ฆบ ๐: |
|||
|
|||
* ๐ฝ ๐ฌ |
|||
* ๐ฝ ๐ ๏ธ |
|||
* ๐ฝ ๐งพ, โ๏ธ. |
|||
|
|||
๐ ๐ท ๐ ๐ โฎ๏ธ Pydantic ๐ท. & โซ๏ธ ๐ค ๐ ๐ ๐ ๐, โ๏ธ Pydantic. |
|||
|
|||
!!! info |
|||
โ๏ธ ๐คฏ ๐ ๐ป ๐ช ๐ซ ๐ Pydantic ๐ท ๐ช. |
|||
|
|||
, ๐ 5๏ธโฃ๐ ๐ช โ๏ธ Pydantic ๐ท. |
|||
|
|||
โ๏ธ ๐ฅ ๐ โ๏ธ ๐ ๐ป ๐คฅ ๐คญ, ๐ ๐ ๐ฑ โ๏ธ ๐ซ ๐๏ธ ๐ธ ๐ ๏ธ โ๏ธ FastAPI. ๐ถ |
|||
|
|||
## ๐ป `response_model` |
|||
|
|||
๐ ๐ช โ๏ธ `dataclasses` `response_model` ๐ข: |
|||
|
|||
```Python hl_lines="1 7-13 19" |
|||
{!../../../docs_src/dataclasses/tutorial002.py!} |
|||
``` |
|||
|
|||
๐ป ๐ ๐ ๐ Pydantic ๐ป. |
|||
|
|||
๐ ๐, ๐ฎ ๐ ๐ ๐ฆ ๐ ๐ ๏ธ ๐ฉบ ๐ฉโ๐ป ๐ข: |
|||
|
|||
<img src="/img/tutorial/dataclasses/image01.png"> |
|||
|
|||
## ๐ป ๐ ๐ ๐ |
|||
|
|||
๐ ๐ช ๐ `dataclasses` โฎ๏ธ ๐ ๐ โ โ ๐ฆ ๐ ๐. |
|||
|
|||
๐ผ, ๐ ๐ช โ๏ธ โ๏ธ Pydantic โฌ `dataclasses`. ๐ผ, ๐ฅ ๐ โ๏ธ โ โฎ๏ธ ๐ ๐ ๐ ๏ธ ๐งพ. |
|||
|
|||
๐ ๐ผ, ๐ ๐ช ๐ฏ ๐ฑ ๐ฉ `dataclasses` โฎ๏ธ `pydantic.dataclasses`, โ ๐ง-โป: |
|||
|
|||
```{ .python .annotate hl_lines="1 5 8-11 14-17 23-25 28" } |
|||
{!../../../docs_src/dataclasses/tutorial003.py!} |
|||
``` |
|||
|
|||
1๏ธโฃ. ๐ฅ ๐ `field` โช๏ธโก๏ธ ๐ฉ `dataclasses`. |
|||
|
|||
2๏ธโฃ. `pydantic.dataclasses` ๐ง-โป `dataclasses`. |
|||
|
|||
3๏ธโฃ. `Author` ๐ป ๐ ๐ `Item` ๐ป. |
|||
|
|||
4๏ธโฃ. `Author` ๐ป โ๏ธ `response_model` ๐ข. |
|||
|
|||
5๏ธโฃ. ๐ ๐ช โ๏ธ ๐ ๐ฉ ๐ โ โฎ๏ธ ๐ป ๐จ ๐ช. |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ `Item` ๐ป. |
|||
|
|||
6๏ธโฃ. ๐ฅ ๐ฅ ๐ฌ ๐ ๐ ๐ `items` โ ๐ ๐ป. |
|||
|
|||
FastAPI ๐ฏ <abbr title="converting the data to a format that can be transmitted">โ</abbr> ๐ฝ ๐ป. |
|||
|
|||
7๏ธโฃ. ๐ฅ `response_model` โ๏ธ ๐ โ ๐ `Author` ๐ป. |
|||
|
|||
๐, ๐ ๐ช ๐ `dataclasses` โฎ๏ธ ๐ฉ ๐ โ. |
|||
|
|||
8๏ธโฃ. ๐ ๐ ๐ *โก ๐ ๏ธ ๐ข* โ๏ธ ๐ฅ `def` โฉ๏ธ `async def`. |
|||
|
|||
๐ง, FastAPI ๐ ๐ช ๐ `def` & `async def` ๐ช. |
|||
|
|||
๐ฅ ๐ ๐ช โ๏ธ ๐ ๐โ โ๏ธ โ, โ
๐
๐ _"๐ โ" _ ๐ฉบ ๐ <a href="https://fastapi.tiangolo.com/async/#in-a-hurry" target="_blank" class="internal-link">`async` & `await`</a>. |
|||
|
|||
9๏ธโฃ. ๐ *โก ๐ ๏ธ ๐ข* ๐ซ ๐ฌ ๐ป (๐ โซ๏ธ ๐ช), โ๏ธ ๐ ๐ โฎ๏ธ ๐ ๐ฝ. |
|||
|
|||
FastAPI ๐ โ๏ธ `response_model` ๐ข (๐ ๐ ๐ป) ๐ ๐จ. |
|||
|
|||
๐ ๐ช ๐ `dataclasses` โฎ๏ธ ๐ ๐ โ ๐ ๐ ๐ ๐จ ๐ ๐ ๐. |
|||
|
|||
โ
-๐ โ ๐โโ ๐ ๐ ๐
๐ฏ โน. |
|||
|
|||
## ๐ก ๐
|
|||
|
|||
๐ ๐ช ๐ `dataclasses` โฎ๏ธ ๐ Pydantic ๐ท, ๐ โช๏ธโก๏ธ ๐ซ, ๐ ๐ซ ๐ ๐ ๐ท, โ๏ธ. |
|||
|
|||
๐ก ๐
, โ
<a href="https://pydantic-docs.helpmanual.io/usage/dataclasses/" class="external-link" target="_blank">Pydantic ๐ฉบ ๐ ๐ป</a>. |
|||
|
|||
## โฌ |
|||
|
|||
๐ ๐ช โฉ๏ธ FastAPI โฌ `0.67.0`. ๐ถ |
@ -0,0 +1,160 @@ |
|||
# ๐ ๐ |
|||
|
|||
๐ ๐ช ๐ฌ โ (๐) ๐ ๐ ๐ ๏ธ โญ ๐ธ **โถ๏ธ ๐**. ๐ โ ๐ ๐ ๐ ๐ ๐ ๏ธ **๐**, **โญ** ๐ธ **โถ๏ธ ๐จ ๐จ**. |
|||
|
|||
๐ ๐, ๐ ๐ช ๐ฌ โ (๐) ๐ ๐ ๐ ๏ธ ๐โ ๐ธ **๐คซ ๐ฝ**. ๐ ๐ผ, ๐ ๐ ๐ ๐ ๏ธ **๐**, **โฎ๏ธ** โ๏ธ ๐ต ๐ฒ **๐ ๐จ**. |
|||
|
|||
โฉ๏ธ ๐ ๐ ๐ ๏ธ โญ ๐ธ **โถ๏ธ** โ ๐จ, & โถ๏ธ๏ธ โฎ๏ธ โซ๏ธ **๐** ๐ ๐จ, โซ๏ธ ๐ ๐ ๐ธ **๐** (๐ค "๐" ๐ โ ๐ฅ ๐ถ). |
|||
|
|||
๐ ๐ช ๐ถ โ โ ๐ **โน** ๐ ๐ ๐ช โ๏ธ ๐ ๐ฑ, & ๐ **๐ฐ** ๐ช ๐จ, &/โ๏ธ ๐ ๐ ๐ช **๐งน ๐** โฎ๏ธ. ๐ผ, ๐ฝ ๐ ๐ฑ, โ๏ธ ๐ ๐ ๐ฐ ๐ซ ๐ท. |
|||
|
|||
## โ๏ธ ๐ผ |
|||
|
|||
โก๏ธ โถ๏ธ โฎ๏ธ ๐ผ **โ๏ธ ๐ผ** & โคด๏ธ ๐ โ โ โซ๏ธ โฎ๏ธ ๐. |
|||
|
|||
โก๏ธ ๐ ๐ ๐ โ๏ธ **๐ฐ ๐ซ ๐ท** ๐ ๐ ๐ โ๏ธ ๐ต ๐จ. ๐ถ |
|||
|
|||
๐ ๐ท ๐ ๐ช ๐จ,, โซ๏ธ ๐ซ 1๏ธโฃ ๐ท ๐ ๐จ, โ๏ธ 1๏ธโฃ ๐ ๐ฉโ๐ป โ๏ธ ๐ณ ๐. |
|||
|
|||
โก๏ธ ๐ ๐ ๐ ๐ท ๐ช **โ ๐ฐ**, โฉ๏ธ โซ๏ธ โ๏ธ โ ๐ **๐ฝ โช๏ธโก๏ธ ๐พ**. ๐ ๐ซ ๐ โซ๏ธ ๐ ๐จ. |
|||
|
|||
๐ ๐ช ๐ โซ๏ธ ๐ ๐ ๐น/๐, โ๏ธ ๐ ๐ โ ๐ โซ๏ธ ๐ **๐ ๐ท** ๐ฅ ๐ ๐โโ ๐
๐ง ๐ฏ, โคด๏ธ ๐ ๐ฏ ๐ **๐** โฉ๏ธ โซ๏ธ ๐ โ๏ธ โ ๐ท ๐ โญ ๐โโ ๐ช ๐ ๐ฌ ๐ ๐. |
|||
|
|||
๐ โซ๏ธโ ๐ฅ ๐ โ, โก๏ธ ๐ ๐ท โญ ๐จ ๐ต, โ๏ธ ๐ด โถ๏ธ๏ธ โญ ๐ธ โถ๏ธ ๐จ ๐จ, ๐ซ โช ๐ โ ๐. |
|||
|
|||
## ๐ |
|||
|
|||
๐ ๐ช ๐ฌ ๐ *๐ด* & *๐คซ* โ โ๏ธ `lifespan` ๐ข `FastAPI` ๐ฑ, & "๐ ๐จโ๐ผ" (๐ค ๐ ๐ฆ ๐ โซ๏ธโ ๐ ๐ฅ). |
|||
|
|||
โก๏ธ โถ๏ธ โฎ๏ธ ๐ผ & โคด๏ธ ๐ โซ๏ธ โน. |
|||
|
|||
๐ฅ โ ๐ ๐ข `lifespan()` โฎ๏ธ `yield` ๐ ๐: |
|||
|
|||
```Python hl_lines="16 19" |
|||
{!../../../docs_src/events/tutorial003.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ฅ โ ๐ฅ *๐ด* ๐ ๏ธ ๐ ๐ท ๐ฎ (โ) ๐ท ๐ข ๐ โฎ๏ธ ๐ฐ ๐ซ ๐ท โญ `yield`. ๐ ๐ ๐ ๐ ๏ธ **โญ** ๐ธ **โถ๏ธ โ ๐จ**, โฎ๏ธ *๐ด*. |
|||
|
|||
& โคด๏ธ, โถ๏ธ๏ธ โฎ๏ธ `yield`, ๐ฅ ๐ ๐ท. ๐ ๐ ๐ ๐ ๏ธ **โฎ๏ธ** ๐ธ **๐ ๐ ๐จ**, โถ๏ธ๏ธ โญ *๐คซ*. ๐ ๐ช, ๐ผ, ๐ โน ๐ ๐พ โ๏ธ ๐ป. |
|||
|
|||
!!! tip |
|||
`shutdown` ๐ ๐จ ๐โ ๐ **โ๏ธ** ๐ธ. |
|||
|
|||
๐ฒ ๐ ๐ช โถ๏ธ ๐ โฌ, โ๏ธ ๐ ๐ค ๐ก ๐ โซ๏ธ. ๐คท |
|||
|
|||
### ๐ ๐ข |
|||
|
|||
๐ฅ ๐ ๐, ๐ ๐ฅ โ ๐ ๐ข โฎ๏ธ `yield`. ๐ ๐ถ ๐ ๐ โฎ๏ธ `yield`. |
|||
|
|||
```Python hl_lines="14-19" |
|||
{!../../../docs_src/events/tutorial003.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ ๐ข, โญ `yield`, ๐ ๐ ๏ธ **โญ** ๐ธ โถ๏ธ. |
|||
|
|||
& ๐ โฎ๏ธ `yield` ๐ ๐ ๏ธ **โฎ๏ธ** ๐ธ โ๏ธ ๐. |
|||
|
|||
### ๐ ๐ ๐จโ๐ผ |
|||
|
|||
๐ฅ ๐ โ
, ๐ข ๐ โฎ๏ธ `@asynccontextmanager`. |
|||
|
|||
๐ ๐ ๐ข ๐ ๐ณ ๐ค "**๐ ๐ ๐จโ๐ผ**". |
|||
|
|||
```Python hl_lines="1 13" |
|||
{!../../../docs_src/events/tutorial003.py!} |
|||
``` |
|||
|
|||
**๐ ๐จโ๐ผ** ๐ ๐ณ ๐ ๐ ๐ช โ๏ธ `with` ๐, ๐ผ, `open()` ๐ช โ๏ธ ๐ ๐จโ๐ผ: |
|||
|
|||
```Python |
|||
with open("file.txt") as file: |
|||
file.read() |
|||
``` |
|||
|
|||
โฎ๏ธ โฌ ๐, ๐ค **๐ ๐ ๐จโ๐ผ**. ๐ ๐ โ๏ธ โซ๏ธ โฎ๏ธ `async with`: |
|||
|
|||
```Python |
|||
async with lifespan(app): |
|||
await do_stuff() |
|||
``` |
|||
|
|||
๐โ ๐ โ ๐ ๐จโ๐ผ โ๏ธ ๐ ๐ ๐จโ๐ผ ๐ ๐, โซ๏ธโ โซ๏ธ ๐จ ๐, โญ ๐ฌ `with` ๐ซ, โซ๏ธ ๐ ๐ ๏ธ ๐ โญ `yield`, & โฎ๏ธ โ `with` ๐ซ, โซ๏ธ ๐ ๐ ๏ธ ๐ โฎ๏ธ `yield`. |
|||
|
|||
๐ ๐ ๐ผ ๐, ๐ฅ ๐ซ โ๏ธ โซ๏ธ ๐, โ๏ธ ๐ฅ ๐ถโโ๏ธ โซ๏ธ FastAPI โซ๏ธ โ๏ธ โซ๏ธ. |
|||
|
|||
`lifespan` ๐ข `FastAPI` ๐ฑ โ **๐ ๐ ๐จโ๐ผ**, ๐ฅ ๐ช ๐ถโโ๏ธ ๐ ๐ `lifespan` ๐ ๐ ๐จโ๐ผ โซ๏ธ. |
|||
|
|||
```Python hl_lines="22" |
|||
{!../../../docs_src/events/tutorial003.py!} |
|||
``` |
|||
|
|||
## ๐ ๐ (๐ข) |
|||
|
|||
!!! warning |
|||
๐ ๐ ๐ต *๐ด* & *๐คซ* โ๏ธ `lifespan` ๐ข `FastAPI` ๐ฑ ๐ฌ ๐. |
|||
|
|||
๐ ๐ช ๐ฒ ๐ถ ๐ ๐. |
|||
|
|||
๐ค ๐ ๐ ๐ฌ ๐ โ ๐ ๏ธ โฎ๏ธ *๐ด* & โฎ๏ธ *๐คซ*. |
|||
|
|||
๐ ๐ช ๐ฌ ๐ ๐โ๐ฆบ (๐ข) ๐ ๐ช ๐ ๏ธ โญ ๐ธ โถ๏ธ ๐, โ๏ธ ๐โ ๐ธ ๐คซ ๐ฝ. |
|||
|
|||
๐ซ ๐ข ๐ช ๐ฃ โฎ๏ธ `async def` โ๏ธ ๐ `def`. |
|||
|
|||
### `startup` ๐ |
|||
|
|||
๐ฎ ๐ข ๐ ๐ ๐ โญ ๐ธ โถ๏ธ, ๐ฃ โซ๏ธ โฎ๏ธ ๐ `"startup"`: |
|||
|
|||
```Python hl_lines="8" |
|||
{!../../../docs_src/events/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, `startup` ๐ ๐โ๐ฆบ ๐ข ๐ ๐ข ๐ฌ "๐ฝ" ( `dict`) โฎ๏ธ ๐ฒ. |
|||
|
|||
๐ ๐ช ๐ฎ ๐
๐ 1๏ธโฃ ๐ ๐โ๐ฆบ ๐ข. |
|||
|
|||
& ๐ ๐ธ ๐ ๐ซ โถ๏ธ ๐จ ๐จ โญ ๐ `startup` ๐ ๐โ๐ฆบ โ๏ธ ๐. |
|||
|
|||
### `shutdown` ๐ |
|||
|
|||
๐ฎ ๐ข ๐ ๐ ๐ ๐โ ๐ธ ๐คซ ๐ฝ, ๐ฃ โซ๏ธ โฎ๏ธ ๐ `"shutdown"`: |
|||
|
|||
```Python hl_lines="6" |
|||
{!../../../docs_src/events/tutorial002.py!} |
|||
``` |
|||
|
|||
๐ฅ, `shutdown` ๐ ๐โ๐ฆบ ๐ข ๐ โ โ โธ `"Application shutdown"` ๐ `log.txt`. |
|||
|
|||
!!! info |
|||
`open()` ๐ข, `mode="a"` โ "๐ป",, โธ ๐ ๐ฎ โฎ๏ธ โซ๏ธโ ๐ ๐ ๐, ๐ต ๐ โฎ๏ธ ๐. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ ๐ผ ๐ฅ โ๏ธ ๐ฉ ๐ `open()` ๐ข ๐ ๐ โฎ๏ธ ๐. |
|||
|
|||
, โซ๏ธ ๐ ๐ค/๐
พ (๐ข/๐ข), ๐ ๐ "โ" ๐ โ ๐พ. |
|||
|
|||
โ๏ธ `open()` ๐ซ โ๏ธ `async` & `await`. |
|||
|
|||
, ๐ฅ ๐ฃ ๐ ๐โ๐ฆบ ๐ข โฎ๏ธ ๐ฉ `def` โฉ๏ธ `async def`. |
|||
|
|||
!!! info |
|||
๐ ๐ช โ ๐
๐ ๐ซ ๐ ๐โ๐ฆบ <a href="https://www.starlette.io/events/" class="external-link" target="_blank">๐ ๐' ๐ฉบ</a>. |
|||
|
|||
### `startup` & `shutdown` ๐ฏโโ๏ธ |
|||
|
|||
๐ค โ ๐ค ๐ โ ๐ *๐ด* & *๐คซ* ๐, ๐ ๐ช ๐ โถ๏ธ ๐ณ & โคด๏ธ ๐ โซ๏ธ, ๐ โน & โคด๏ธ ๐ โซ๏ธ, โ๏ธ. |
|||
|
|||
๐จ ๐ ๐ฝ ๐ข ๐ ๐ซ ๐ฐ โ โ๏ธ ๐ข ๐ฏโโ๏ธ ๐
โ ๐ ๐ ๐ช ๐ช ๐ฒ ๐ ๐ข โ๏ธ ๐ ๐ฑ. |
|||
|
|||
โฉ๏ธ ๐, โซ๏ธ ๐ ๐ โฉ๏ธ โ๏ธ `lifespan` ๐ฌ ๐. |
|||
|
|||
## ๐ก โน |
|||
|
|||
๐ก โน ๐ ๐ค. ๐ถ |
|||
|
|||
๐, ๐ซ ๐ก ๐ง, ๐ ๐ <a href="https://asgi.readthedocs.io/en/latest/specs/lifespan.html" class="external-link" target="_blank">๐ ๐ ๏ธ</a>, & โซ๏ธ ๐ฌ ๐ ๐ค `startup` & `shutdown`. |
|||
|
|||
## ๐ง ๐ธ |
|||
|
|||
๐ถ โ๏ธ ๐คฏ ๐ ๐ซ ๐ ๐ (๐ด & ๐คซ) ๐ ๐ด ๐ ๏ธ ๐ ๐ธ, ๐ซ [๐ง ๐ธ - ๐ป](./sub-applications.md){.internal-link target=_blank}. |
@ -0,0 +1,267 @@ |
|||
# ๐ ๐ฉโ๐ป |
|||
|
|||
**FastAPI** โ๏ธ ๐ ๐ ๐ง, ๐ ๐ค ๐ง ๐ โฎ๏ธ ๐ ๐งฐ, ๐ ๐ง ๐ ๏ธ ๐ฉบ (๐ ๐ฆ ๐). |
|||
|
|||
1๏ธโฃ ๐ฏ ๐ ๐ ๐ซ ๐ฏ โญ ๐ ๐ ๐ช **๐ ๐ฉโ๐ป** (๐ฃ ๐ค <abbr title="Software Development Kits">**๐ฑ**</abbr> ) ๐ ๐ ๏ธ, ๐ ๐ **๐ ๏ธ ๐ช๐ธ**. |
|||
|
|||
## ๐ ๐ฉโ๐ป ๐ |
|||
|
|||
๐ค ๐ ๐งฐ ๐ ๐ฉโ๐ป โช๏ธโก๏ธ **๐**. |
|||
|
|||
โ ๐งฐ <a href="https://openapi-generator.tech/" class="external-link" target="_blank">๐ ๐</a>. |
|||
|
|||
๐ฅ ๐ ๐ **๐ธ**, ๐ถ ๐ ๐ <a href="https://github.com/ferdikoomen/openapi-typescript-codegen" class="external-link" target="_blank">๐-๐-๐ฆ๐ช</a>. |
|||
|
|||
## ๐ ๐ ๐ธ ๐ฉโ๐ป |
|||
|
|||
โก๏ธ โถ๏ธ โฎ๏ธ ๐
FastAPI ๐ธ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9-11 14-15 18 19 23" |
|||
{!> ../../../docs_src/generate_clients/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="7-9 12-13 16-17 21" |
|||
{!> ../../../docs_src/generate_clients/tutorial001_py39.py!} |
|||
``` |
|||
|
|||
๐ ๐ *โก ๐ ๏ธ* ๐ฌ ๐ท ๐ซ โ๏ธ ๐จ ๐ & ๐จ ๐, โ๏ธ ๐ท `Item` & `ResponseMessage`. |
|||
|
|||
### ๐ ๏ธ ๐ฉบ |
|||
|
|||
๐ฅ ๐ ๐ถ ๐ ๏ธ ๐ฉบ, ๐ ๐ ๐ ๐ โซ๏ธ โ๏ธ **๐** ๐ ๐จ ๐จ & ๐จ ๐จ: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image01.png"> |
|||
|
|||
๐ ๐ช ๐ ๐ ๐ โฉ๏ธ ๐ซ ๐ฃ โฎ๏ธ ๐ท ๐ฑ. |
|||
|
|||
๐ โน ๐ช ๐ฑ **๐ ๐**, & โคด๏ธ ๐ฆ ๐ ๏ธ ๐ฉบ (๐ฆ ๐). |
|||
|
|||
& ๐ ๐ โน โช๏ธโก๏ธ ๐ท ๐ ๐ ๐ โซ๏ธโ ๐ช โ๏ธ **๐ ๐ฉโ๐ป ๐**. |
|||
|
|||
### ๐ ๐ ๐ฉโ๐ป |
|||
|
|||
๐ ๐ ๐ฅ โ๏ธ ๐ฑ โฎ๏ธ ๐ท, ๐ฅ ๐ช ๐ ๐ฉโ๐ป ๐ ๐ธ. |
|||
|
|||
#### โ `openapi-typescript-codegen` |
|||
|
|||
๐ ๐ช โ `openapi-typescript-codegen` ๐ ๐ธ ๐ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ npm install openapi-typescript-codegen --save-dev |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
#### ๐ ๐ฉโ๐ป ๐ |
|||
|
|||
๐ ๐ฉโ๐ป ๐ ๐ ๐ช โ๏ธ ๐ โธ ๐ธ `openapi` ๐ ๐ ๐ โ. |
|||
|
|||
โฉ๏ธ โซ๏ธ โ ๐ง๐ฟ ๐, ๐ ๐ฒ ๐ซ๐ ๐ช ๐ค ๐ ๐ ๐, โ๏ธ ๐ ๐ ๐ฎ โซ๏ธ ๐ ๐ `package.json` ๐. |
|||
|
|||
โซ๏ธ ๐ช ๐ ๐ ๐: |
|||
|
|||
```JSON hl_lines="7" |
|||
{ |
|||
"name": "frontend-app", |
|||
"version": "1.0.0", |
|||
"description": "", |
|||
"main": "index.js", |
|||
"scripts": { |
|||
"generate-client": "openapi --input http://localhost:8000/openapi.json --output ./src/client --client axios" |
|||
}, |
|||
"author": "", |
|||
"license": "", |
|||
"devDependencies": { |
|||
"openapi-typescript-codegen": "^0.20.1", |
|||
"typescript": "^4.6.2" |
|||
} |
|||
} |
|||
``` |
|||
|
|||
โฎ๏ธ โ๏ธ ๐ โ `generate-client` โ ๐ค, ๐ ๐ช ๐ โซ๏ธ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ npm run generate-client |
|||
|
|||
[email protected] generate-client /home/user/code/frontend-app |
|||
> openapi --input http://localhost:8000/openapi.json --output ./src/client --client axios |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ ๐ ๐ `./src/client` & ๐ โ๏ธ `axios` (๐ธ ๐บ๐ธ๐ ๐) ๐. |
|||
|
|||
### ๐ ๐
๐ฉโ๐ป ๐ |
|||
|
|||
๐ ๐ ๐ช ๐ & โ๏ธ ๐ฉโ๐ป ๐, โซ๏ธ ๐ช ๐ ๐ ๐, ๐ ๐ ๐ ๐ค โ ๐ฉโ๐ฌ: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image02.png"> |
|||
|
|||
๐ ๐ ๐ค โ ๐ ๐จ: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image03.png"> |
|||
|
|||
!!! tip |
|||
๐ โ `name` & `price`, ๐ ๐ฌ FastAPI ๐ธ, `Item` ๐ท. |
|||
|
|||
๐ ๐ โ๏ธ โธ โ ๐ ๐ ๐ ๐จ: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image04.png"> |
|||
|
|||
๐จ ๐ ๐ โ๏ธ โ: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image05.png"> |
|||
|
|||
## FastAPI ๐ฑ โฎ๏ธ ๐ |
|||
|
|||
๐ ๐ผ ๐ FastAPI ๐ฑ ๐ ๐ฆ, & ๐ ๐ ๐ฒ โ๏ธ ๐ ๐ ๐ ๐ช *โก ๐ ๏ธ*. |
|||
|
|||
๐ผ, ๐ ๐ช โ๏ธ ๐ **๐ฌ** & โ1๏ธโฃ ๐ **๐ฉโ๐ป**, & ๐ซ ๐ช ๐ฝ ๐: |
|||
|
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="23 28 36" |
|||
{!> ../../../docs_src/generate_clients/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="21 26 34" |
|||
{!> ../../../docs_src/generate_clients/tutorial002_py39.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ ๐ฉโ๐ป โฎ๏ธ ๐ |
|||
|
|||
๐ฅ ๐ ๐ ๐ฉโ๐ป FastAPI ๐ฑ โ๏ธ ๐, โซ๏ธ ๐ ๐ ๐ ๐ฉโ๐ป ๐ โ๏ธ ๐ ๐. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ช โ๏ธ ๐ โ & ๐ช โ ๐ฉโ๐ป ๐: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image06.png"> |
|||
|
|||
๐ ๐ผ ๐ โ๏ธ: |
|||
|
|||
* `ItemsService` |
|||
* `UsersService` |
|||
|
|||
### ๐ฉโ๐ป ๐ฉโ๐ฌ ๐ |
|||
|
|||
โถ๏ธ๏ธ ๐ ๐ ๐ฉโ๐ฌ ๐ ๐ `createItemItemsPost` ๐ซ ๐ ๐ถ ๐งน: |
|||
|
|||
```TypeScript |
|||
ItemsService.createItemItemsPost({name: "Plumbus", price: 5}) |
|||
``` |
|||
|
|||
...๐ โฉ๏ธ ๐ฉโ๐ป ๐ โ๏ธ ๐ ๐ **๐ ๏ธ ๐** ๐ *โก ๐ ๏ธ*. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ ๏ธ ๐ ๐ ๐คญ ๐ *โก ๐ ๏ธ*, FastAPI โ๏ธ **๐ข ๐**, **โก**, & **๐บ๐ธ๐ ๐ฉโ๐ฌ/๐ ๏ธ** ๐ ๐ ๐ ๏ธ ๐, โฉ๏ธ ๐ ๐ โซ๏ธ ๐ช โ ๐ญ ๐ ๐ ๏ธ ๐ ๐. |
|||
|
|||
โ๏ธ ๐ค ๐ ๐ฆ ๐ โ ๐ ๐ โญ. ๐ถ |
|||
|
|||
## ๐ ๐ ๏ธ ๐ & ๐ ๐ฉโ๐ฌ ๐ |
|||
|
|||
๐ ๐ช **๐** ๐ ๐ซ ๐ ๏ธ ๐ **๐** โ ๐ซ ๐
& โ๏ธ **๐
๐ฉโ๐ฌ ๐** ๐ฉโ๐ป. |
|||
|
|||
๐ ๐ผ ๐ ๐ โ๏ธ ๐ ๐ ๐ ๐ ๏ธ ๐ **๐** ๐ ๐. |
|||
|
|||
๐ผ, ๐ ๐ช โ ๐ญ ๐ ๐ *โก ๐ ๏ธ* โ๏ธ ๐, & โคด๏ธ ๐ ๐ ๏ธ ๐ โ๏ธ ๐ **๐** & *โก ๐ ๏ธ* **๐** (๐ข ๐). |
|||
|
|||
### ๐ ๐ ๐ ๐ ๐ข |
|||
|
|||
FastAPI โ๏ธ **๐ ๐** ๐ *โก ๐ ๏ธ*, โซ๏ธ โ๏ธ **๐ ๏ธ ๐** & ๐ ๐ ๐ช ๐ ๐ท, ๐จ โ๏ธ ๐จ. |
|||
|
|||
๐ ๐ช ๐ ๐ ๐ข. โซ๏ธ โ `APIRoute` & ๐ข ๐ป. |
|||
|
|||
๐ผ, ๐ฅ โซ๏ธ โ๏ธ ๐ฅ ๐ (๐ ๐ ๐ฒ โ๏ธ ๐ด 1๏ธโฃ ๐) & *โก ๐ ๏ธ* ๐ (๐ข ๐). |
|||
|
|||
๐ ๐ช โคด๏ธ ๐ถโโ๏ธ ๐ ๐ ๐ข **FastAPI** `generate_unique_id_function` ๐ข: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="8-9 12" |
|||
{!> ../../../docs_src/generate_clients/tutorial003.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="6-7 10" |
|||
{!> ../../../docs_src/generate_clients/tutorial003_py39.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ ๐ฉโ๐ป โฎ๏ธ ๐ ๐ ๏ธ ๐ |
|||
|
|||
๐ ๐ฅ ๐ ๐ ๐ฉโ๐ป ๐, ๐ ๐ ๐ ๐ โซ๏ธ โ๏ธ ๐ ๐ฉโ๐ฌ ๐: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image07.png"> |
|||
|
|||
๐ ๐, ๐ฉโ๐ฌ ๐ ๐ โ๏ธ ๐ & โคด๏ธ ๐ข ๐, ๐ ๐ซ ๐ซ ๐ โน โช๏ธโก๏ธ ๐ โก & ๐บ๐ธ๐ ๐ ๏ธ. |
|||
|
|||
### ๐ ๐ ๐ง ๐ฉโ๐ป ๐ |
|||
|
|||
๐ ๐ โ๏ธ **โ โน**. |
|||
|
|||
๐ฅ โช ๐ญ ๐ ๐ ๐ฉโ๐ฌ ๐ **๐ฌ** โฉ๏ธ ๐ ๐ค `ItemsService` (โ โช๏ธโก๏ธ ๐), โ๏ธ ๐ฅ โ๏ธ ๐ ๐ก ๐ฉโ๐ฌ ๐ ๐โโ๏ธ. ๐ถ |
|||
|
|||
๐ฅ ๐ ๐ฒ ๐ ๐ง โซ๏ธ ๐ ๐ข, ๐ ๐ ๐ ๐ ๐ ๏ธ ๐ **๐**. |
|||
|
|||
โ๏ธ ๐ ๐ฉโ๐ป ๐ฅ ๐ช **๐** ๐ ๐ ๏ธ ๐ โถ๏ธ๏ธ โญ ๐ญ ๐ฉโ๐ป, โ ๐ ๐ฉโ๐ฌ ๐ ๐ & **๐งน**. |
|||
|
|||
๐ฅ ๐ช โฌ ๐ ๐ป ๐ `openapi.json` & โคด๏ธ ๐ฅ ๐ช **โ ๐ ๐ก ๐** โฎ๏ธ โ ๐ ๐: |
|||
|
|||
```Python |
|||
{!../../../docs_src/generate_clients/tutorial004.py!} |
|||
``` |
|||
|
|||
โฎ๏ธ ๐, ๐ ๏ธ ๐ ๐ ๐ โช๏ธโก๏ธ ๐ ๐ `items-get_items` `get_items`, ๐ ๐ ๐ฉโ๐ป ๐ ๐ช ๐ ๐
๐ฉโ๐ฌ ๐. |
|||
|
|||
### ๐ ๐ ๐ฉโ๐ป โฎ๏ธ ๐ ๐ |
|||
|
|||
๐ ๐ ๐ ๐ `openapi.json`, ๐ ๐ ๐ `package.json` โ๏ธ ๐ ๐ง๐ฟ ๐, ๐ผ: |
|||
|
|||
```JSON hl_lines="7" |
|||
{ |
|||
"name": "frontend-app", |
|||
"version": "1.0.0", |
|||
"description": "", |
|||
"main": "index.js", |
|||
"scripts": { |
|||
"generate-client": "openapi --input ./openapi.json --output ./src/client --client axios" |
|||
}, |
|||
"author": "", |
|||
"license": "", |
|||
"devDependencies": { |
|||
"openapi-typescript-codegen": "^0.20.1", |
|||
"typescript": "^4.6.2" |
|||
} |
|||
} |
|||
``` |
|||
|
|||
โฎ๏ธ ๐ญ ๐ ๐ฉโ๐ป, ๐ ๐ ๐ โ๏ธ **๐งน ๐ฉโ๐ฌ ๐**, โฎ๏ธ ๐ **โ**, **โธ โ**, โ๏ธ: |
|||
|
|||
<img src="/img/tutorial/generate-clients/image08.png"> |
|||
|
|||
## ๐ฐ |
|||
|
|||
๐โ โ๏ธ ๐ ๐ ๐ฉโ๐ป ๐ ๐ **โ** : |
|||
|
|||
* ๐ฉโ๐ฌ. |
|||
* ๐จ ๐ ๐ช, ๐ข ๐ข, โ๏ธ. |
|||
* ๐จ ๐. |
|||
|
|||
๐ ๐ โ๏ธ **โธ โ** ๐. |
|||
|
|||
& ๐โ ๐ โน ๐ฉโ๐ป ๐, & **โป** ๐ธ, โซ๏ธ ๐ โ๏ธ ๐ ๐ *โก ๐ ๏ธ* ๐ช ๐ฉโ๐ฌ, ๐ ๐ โ, & ๐ ๐ ๐ ๐ ๐จ ๐ ๐ ๐. ๐ถ |
|||
|
|||
๐ โ ๐ ๐ฅ ๐ณ ๐ โซ๏ธ ๐ **๐จ** ๐ ๐ฉโ๐ป ๐ ๐. & ๐ฅ ๐ **๐** ๐ฉโ๐ป โซ๏ธ ๐ โ ๐
๐ฅ ๐ โ๏ธ ๐ **๐** ๐ โ๏ธ. |
|||
|
|||
, ๐ ๐ **๐ ๐ โ** ๐ถ โช ๐ ๏ธ ๐ต โฉ๏ธ โ๏ธ โ โ ๐ฆ ๐ ๐ ๐ ๐ฉโ๐ป ๐ญ & โคด๏ธ ๐ โน ๐โ โ . ๐ถ |
@ -0,0 +1,24 @@ |
|||
# ๐ง ๐ฉโ๐ป ๐ฆฎ |
|||
|
|||
## ๐ โ |
|||
|
|||
๐ [๐ฐ - ๐ฉโ๐ป ๐ฆฎ](../tutorial/){.internal-link target=_blank} ๐ ๐ฅ ๐ค ๐ ๐ซ ๐ ๐ ๐ โ **FastAPI**. |
|||
|
|||
โญ ๐ ๐ ๐ ๐ ๐ ๐, ๐ณ, & ๐ โ. |
|||
|
|||
!!! tip |
|||
โญ ๐ **๐ซ ๐ฏ "๐ง"**. |
|||
|
|||
& โซ๏ธ ๐ช ๐ ๐ โ๏ธ ๐ผ, โ 1๏ธโฃ ๐ซ. |
|||
|
|||
## โ ๐ฐ ๐ฅ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ โ **FastAPI** โฎ๏ธ ๐ก โช๏ธโก๏ธ ๐ [๐ฐ - ๐ฉโ๐ป ๐ฆฎ](../tutorial/){.internal-link target=_blank}. |
|||
|
|||
& โญ ๐ ๐ค ๐ โช โ โซ๏ธ, & ๐ค ๐ ๐ ๐ญ ๐ ๐ ๐ญ. |
|||
|
|||
## ๐.๐
พ โ๏ธ |
|||
|
|||
๐ฅ ๐ ๐ ๐ โ ๐ง-๐ฐ โ๏ธ ๐ ๐ ๐ ๐ฉบ, ๐ ๐ช ๐ โ
: <a href="https://testdriven.io/courses/tdd-fastapi/" class="external-link" target="_blank">๐ฏ-๐พ ๐ ๏ธ โฎ๏ธ FastAPI & โ</a> **๐.๐
พ**. |
|||
|
|||
๐ซ โณ ๐ฉธ 1๏ธโฃ0๏ธโฃ ๐ฏ ๐ ๐ฐ ๐ ๏ธ **FastAPI**. ๐ถ ๐ถ |
@ -0,0 +1,99 @@ |
|||
# ๐ง ๐ ๏ธ |
|||
|
|||
๐ ๐ฐ ๐ โ โ ๐ฎ [๐ ๐ ๏ธ](../tutorial/middleware.md){.internal-link target=_blank} ๐ ๐ธ. |
|||
|
|||
& โคด๏ธ ๐ โ โ ๐ต [โ โฎ๏ธ `CORSMiddleware`](../tutorial/cors.md){.internal-link target=_blank}. |
|||
|
|||
๐ ๐ ๐ฅ ๐ ๐ โ โ๏ธ ๐ ๐ ๏ธ. |
|||
|
|||
## โ ๐ซ ๐ ๏ธ |
|||
|
|||
**FastAPI** โ๏ธ ๐ ๐ & ๐ ๏ธ <abbr title="Asynchronous Server Gateway Interface">๐ซ</abbr> ๐ง, ๐ ๐ช โ๏ธ ๐ ๐ซ ๐ ๏ธ. |
|||
|
|||
๐ ๏ธ ๐ซ โ๏ธ โ FastAPI โ๏ธ ๐ ๐ท, ๐ โซ๏ธ โฉ ๐ซ ๐. |
|||
|
|||
๐ข, ๐ซ ๐ ๏ธ ๐ ๐ โ ๐จ ๐ซ ๐ฑ ๐ฅ โ. |
|||
|
|||
, ๐งพ ๐ฅ-๐ฅณ ๐ซ ๐ ๏ธ ๐ซ ๐ ๐ฒ ๐ฌ ๐ ๐ณ ๐: |
|||
|
|||
```Python |
|||
from unicorn import UnicornMiddleware |
|||
|
|||
app = SomeASGIApp() |
|||
|
|||
new_app = UnicornMiddleware(app, some_config="rainbow") |
|||
``` |
|||
|
|||
โ๏ธ FastAPI (๐ค ๐) ๐ ๐
๐ โซ๏ธ ๐ โ ๐ญ ๐ ๐ ๐ ๏ธ ๐ต ๐ฝ โ & ๐ โ ๐โ๐ฆบ ๐ท โ. |
|||
|
|||
๐, ๐ โ๏ธ `app.add_middleware()` (๐ผ โ). |
|||
|
|||
```Python |
|||
from fastapi import FastAPI |
|||
from unicorn import UnicornMiddleware |
|||
|
|||
app = FastAPI() |
|||
|
|||
app.add_middleware(UnicornMiddleware, some_config="rainbow") |
|||
``` |
|||
|
|||
`app.add_middleware()` ๐จ ๐ ๏ธ ๐ ๐ฅ โ & ๐ ๐ โ ๐ถโโ๏ธ ๐ ๏ธ. |
|||
|
|||
## ๐ ๏ธ ๐ ๏ธ |
|||
|
|||
**FastAPI** ๐ ๐ ๐ ๏ธ โ โ๏ธ ๐ผ, ๐ฅ ๐ ๐ โญ โ โ๏ธ ๐ซ. |
|||
|
|||
!!! note "๐ก โน" |
|||
โญ ๐ผ, ๐ ๐ช โ๏ธ `from starlette.middleware.something import SomethingMiddleware`. |
|||
|
|||
**FastAPI** ๐ ๐ ๐ ๏ธ `fastapi.middleware` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐ ๏ธ ๐ ๐ โช๏ธโก๏ธ ๐. |
|||
|
|||
## `HTTPSRedirectMiddleware` |
|||
|
|||
๐ ๏ธ ๐ ๐ ๐จ ๐จ ๐ ๐ฏโโ๏ธ `https` โ๏ธ `wss`. |
|||
|
|||
๐ ๐จ ๐จ `http` โ๏ธ `ws` ๐ โ ๐ โ โฉ๏ธ. |
|||
|
|||
```Python hl_lines="2 6" |
|||
{!../../../docs_src/advanced_middleware/tutorial001.py!} |
|||
``` |
|||
|
|||
## `TrustedHostMiddleware` |
|||
|
|||
๐ ๏ธ ๐ ๐ ๐จ ๐จ โ๏ธ โ โ `Host` ๐, โ ๐โโ ๐ก ๐บ๐ธ๐ ๐ฆ ๐ ๐. |
|||
|
|||
```Python hl_lines="2 6-8" |
|||
{!../../../docs_src/advanced_middleware/tutorial002.py!} |
|||
``` |
|||
|
|||
๐ โ ๐โ๐ฆบ: |
|||
|
|||
* `allowed_hosts` - ๐ ๐ ๐ ๐ ๐ โ ๐. ๐ ๐ โ
`*.example.com` ๐โ๐ฆบ ๐ ๐. โ ๐ ๐ ๐ฏโโ๏ธ โ๏ธ `allowed_hosts=["*"]` โ๏ธ ๐ซ ๐ ๏ธ. |
|||
|
|||
๐ฅ ๐จ ๐จ ๐จ ๐ซ โ โ โคด๏ธ `400` ๐จ ๐ ๐จ. |
|||
|
|||
## `GZipMiddleware` |
|||
|
|||
๐ต ๐ ๐จ ๐ ๐จ ๐ ๐ `"gzip"` `Accept-Encoding` ๐. |
|||
|
|||
๐ ๏ธ ๐ ๐ต ๐ฏโโ๏ธ ๐ฉ & ๐ฅ ๐จ. |
|||
|
|||
```Python hl_lines="2 6" |
|||
{!../../../docs_src/advanced_middleware/tutorial003.py!} |
|||
``` |
|||
|
|||
๐ โ ๐โ๐ฆบ: |
|||
|
|||
* `minimum_size` - ๐ซ ๐ ๐จ ๐ ๐คช ๐ ๐ ๐ฏ ๐ ๐ข. ๐ข `500`. |
|||
|
|||
## ๐ ๐ ๏ธ |
|||
|
|||
๐ค ๐ ๐ ๐ซ ๐ ๏ธ. |
|||
|
|||
๐ผ: |
|||
|
|||
* <a href="https://docs.sentry.io/platforms/python/asgi/" class="external-link" target="_blank">๐ซ</a> |
|||
* <a href="https://github.com/encode/uvicorn/blob/master/uvicorn/middleware/proxy_headers.py" class="external-link" target="_blank">Uvicorn `ProxyHeadersMiddleware`</a> |
|||
* <a href="https://github.com/florimondmanca/msgpack-asgi" class="external-link" target="_blank">๐ธ๐ฒ</a> |
|||
|
|||
๐ ๐ ๐ช ๐ ๏ธ โ
<a href="https://www.starlette.io/middleware/" class="external-link" target="_blank">๐ ๐ ๏ธ ๐ฉบ</a> & <a href="https://github.com/florimondmanca/awesome-asgi" class="external-link" target="_blank">๐ซ ๐ ๐</a>. |
@ -0,0 +1,156 @@ |
|||
# โ (๐ / ๐ฆ ๐ฝ) ๐ฝ |
|||
|
|||
**FastAPI** ๐ช ๐ ๏ธ โฎ๏ธ ๐ <abbr title="Distributed database (Big Data), also 'Not Only SQL'">โ</abbr>. |
|||
|
|||
๐ฅ ๐ฅ ๐ ๐ ๐ผ โ๏ธ **<a href="https://www.couchbase.com/" class="external-link" target="_blank">๐</a>**, <abbr title="Document here refers to a JSON object (a dict), with keys and values, and those values can also be other JSON objects, arrays (lists), numbers, strings, booleans, etc.">๐</abbr> ๐งข โ ๐ฝ. |
|||
|
|||
๐ ๐ช ๐ ๏ธ โซ๏ธ ๐ ๐ โ ๐ฝ ๐: |
|||
|
|||
* **โณ** |
|||
* **๐ธ** |
|||
* **โณ** |
|||
* **๐ธ๐ฒ** |
|||
* **โณ**, โ๏ธ. |
|||
|
|||
!!! tip |
|||
๐ค ๐ ๐ ๐ โฎ๏ธ **FastAPI** & **๐**, ๐ โ๏ธ ๐ **โ**, ๐ ๐ธ & ๐ ๐งฐ: <a href="https://github.com/tiangolo/full-stack-fastapi-couchbase" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-fastapi-couchbase</a> |
|||
|
|||
## ๐ ๐ ๐ฆฒ |
|||
|
|||
๐, ๐ซ ๐ธ ๐ ๐, ๐ด ๐: |
|||
|
|||
```Python hl_lines="3-5" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
## ๐ฌ ๐ โ๏ธ "๐ ๐" |
|||
|
|||
๐ฅ ๐ โ๏ธ โซ๏ธ โช ๐ง ๐ `type` ๐ ๐. |
|||
|
|||
๐ ๐ซ โ ๐, โ๏ธ ๐ ๐ก ๐ ๐ โน ๐ โฎ๏ธ. |
|||
|
|||
```Python hl_lines="9" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
## ๐ฎ ๐ข ๐ค `Bucket` |
|||
|
|||
**๐**, ๐ฅก โ ๐, ๐ ๐ช ๐ ๐. |
|||
|
|||
๐ซ ๐ ๐ ๐ ๐ ๐ธ. |
|||
|
|||
๐ ๐ ๐ฝ ๐ ๐ "๐ฝ" (๐ฏ ๐ฝ, ๐ซ ๐ฝ ๐ฝ). |
|||
|
|||
๐ **โณ** ๐ "๐". |
|||
|
|||
๐, `Bucket` ๐จ ๐ ๐จ๐ป ๐ป โฎ๏ธ ๐ฝ. |
|||
|
|||
๐ ๐ ๐ข ๐: |
|||
|
|||
* ๐ **๐** ๐ (๐ ๐ช ๐ ๐ฐ). |
|||
* โ ๐ข โฒ. |
|||
* ๐ ๐. |
|||
* ๐ค `Bucket` ๐. |
|||
* โ ๐ข โฒ. |
|||
* ๐จ โซ๏ธ. |
|||
|
|||
```Python hl_lines="12-21" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
## โ Pydantic ๐ท |
|||
|
|||
**๐** "๐" ๐ค "๐ป ๐", ๐ฅ ๐ช ๐ท ๐ซ โฎ๏ธ Pydantic. |
|||
|
|||
### `User` ๐ท |
|||
|
|||
๐ฅ, โก๏ธ โ `User` ๐ท: |
|||
|
|||
```Python hl_lines="24-28" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ ๐ท ๐ *โก ๐ ๏ธ ๐ข*,, ๐ฅ ๐ซ ๐ โซ๏ธ `hashed_password`. |
|||
|
|||
### `UserInDB` ๐ท |
|||
|
|||
๐, โก๏ธ โ `UserInDB` ๐ท. |
|||
|
|||
๐ ๐ โ๏ธ ๐ฝ ๐ ๐ค ๐ช ๐ฝ. |
|||
|
|||
๐ฅ ๐ซ โ โซ๏ธ ๐ฟ Pydantic `BaseModel` โ๏ธ ๐ฟ ๐ ๐ `User`, โฉ๏ธ โซ๏ธ ๐ โ๏ธ ๐ ๐ข `User` โ ๐ฉโโคโ๐จ ๐
: |
|||
|
|||
```Python hl_lines="31-33" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! note |
|||
๐ ๐ ๐ฅ โ๏ธ `hashed_password` & `type` ๐ ๐ ๐ ๐ช ๐ฝ. |
|||
|
|||
โ๏ธ โซ๏ธ ๐ซ ๐ ๐ข `User` ๐ท (1๏ธโฃ ๐ฅ ๐ ๐จ *โก ๐ ๏ธ*). |
|||
|
|||
## ๐ค ๐ฉโ๐ป |
|||
|
|||
๐ โ ๐ข ๐ ๐: |
|||
|
|||
* โ ๐. |
|||
* ๐ ๐ ๐ โช๏ธโก๏ธ โซ๏ธ. |
|||
* ๐ค ๐ โฎ๏ธ ๐ ๐. |
|||
* ๐ฎ ๐ ๐ `UserInDB` ๐ท. |
|||
|
|||
๐ ๐ข ๐ ๐ด ๐ก ๐ค ๐ ๐ฉโ๐ป โช๏ธโก๏ธ `username` (โ๏ธ ๐ ๐ ๐ข) ๐ฌ ๐ *โก ๐ ๏ธ ๐ข*, ๐ ๐ช ๐ ๐ช ๐ค-โ๏ธ โซ๏ธ ๐ ๐ & ๐ฎ <abbr title="Automated test, written in code, that checks if another piece of code is working correctly.">โ ๐ฏ</abbr> โซ๏ธ: |
|||
|
|||
```Python hl_lines="36-42" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ-๐ป |
|||
|
|||
๐ฅ ๐ ๐ซ ๐ฐ โฎ๏ธ `f"userprofile::{username}"`, โซ๏ธ ๐ "<a href="https://docs.python.org/3/glossary.html#term-f-string" class="external-link" target="_blank">โ-๐ป</a>". |
|||
|
|||
๐ ๐ข ๐ ๐ฎ ๐ `{}` โ-๐ป ๐ โ / ๐ ๐ป. |
|||
|
|||
### `dict` ๐ |
|||
|
|||
๐ฅ ๐ ๐ซ ๐ฐ โฎ๏ธ `UserInDB(**result.value)`, <a href="https://docs.python.org/3/glossary.html#term-argument" class="external-link" target="_blank">โซ๏ธ โ๏ธ `dict` "๐"</a>. |
|||
|
|||
โซ๏ธ ๐ โ `dict` `result.value`, & โ ๐ ๐ฎ ๐ & ๐ฒ & ๐ถโโ๏ธ ๐ซ ๐-๐ฒ `UserInDB` ๐จ๐ป โ. |
|||
|
|||
, ๐ฅ `dict` ๐: |
|||
|
|||
```Python |
|||
{ |
|||
"username": "johndoe", |
|||
"hashed_password": "some_hash", |
|||
} |
|||
``` |
|||
|
|||
โซ๏ธ ๐ ๐ถโโ๏ธ `UserInDB` : |
|||
|
|||
```Python |
|||
UserInDB(username="johndoe", hashed_password="some_hash") |
|||
``` |
|||
|
|||
## โ ๐ **FastAPI** ๐ |
|||
|
|||
### โ `FastAPI` ๐ฑ |
|||
|
|||
```Python hl_lines="46" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ *โก ๐ ๏ธ ๐ข* |
|||
|
|||
๐ ๐ ๐ค ๐ & ๐ฅ ๐ซ โ๏ธ <a href="https://docs.couchbase.com/python-sdk/2.5/async-programming.html#asyncio-python-3-5" class="external-link" target="_blank">๐ฅผ ๐ <code>await</code> ๐โ๐ฆบ</a>, ๐ฅ ๐ ๐ฃ ๐ ๐ข โฎ๏ธ ๐ `def` โฉ๏ธ `async def`. |
|||
|
|||
, ๐ ๐ ๐ซ โ๏ธ ๐ `Bucket` ๐ ๐ "<abbr title="A sequence of code being executed by the program, while at the same time, or at intervals, there can be others being executed too.">๐งต</abbr>โ",, ๐ฅ ๐ช ๐ค ๐ฅก ๐ & ๐ถโโ๏ธ โซ๏ธ ๐ ๐ ๐ข: |
|||
|
|||
```Python hl_lines="49-53" |
|||
{!../../../docs_src/nosql_databases/tutorial001.py!} |
|||
``` |
|||
|
|||
## ๐ |
|||
|
|||
๐ ๐ช ๐ ๏ธ ๐ ๐ฅ ๐ฅณ โ ๐ฝ, โ๏ธ ๐ซ ๐ฉ ๐ฆ. |
|||
|
|||
๐ โ ๐ ๐ ๐ข ๐งฐ, โ๏ธ โ๏ธ ๐ ๏ธ. |
@ -0,0 +1,179 @@ |
|||
# ๐ โฒ |
|||
|
|||
๐ ๐ช โ ๐ ๏ธ โฎ๏ธ *โก ๐ ๏ธ* ๐ ๐ช โฒ ๐จ *๐ข ๐ ๏ธ* โ ๐ฑ ๐ (๐ฒ ๐ ๐ฉโ๐ป ๐ ๐ *โ๏ธ* ๐ ๐ ๏ธ). |
|||
|
|||
๐ ๏ธ ๐ ๐จ ๐โ ๐ ๐ ๏ธ ๐ฑ ๐ค *๐ข ๐ ๏ธ* ๐ "โฒ". โฉ๏ธ ๐ฅ ๐ ๐ข ๐ฉโ๐ป โ ๐จ ๐จ ๐ ๐ ๏ธ & โคด๏ธ ๐ ๐ ๏ธ *๐ค ๐*, ๐จ ๐จ *๐ข ๐ ๏ธ* (๐ ๐ฒ โ ๐ ๐ฉโ๐ป). |
|||
|
|||
๐ ๐ผ, ๐ ๐ช ๐ ๐ โ ๐ ๐ข ๐ ๏ธ *๐* ๐ ๐. โซ๏ธโ *โก ๐ ๏ธ* โซ๏ธ ๐ โ๏ธ, โซ๏ธโ ๐ช โซ๏ธ ๐ โ, โซ๏ธโ ๐จ โซ๏ธ ๐ ๐จ, โ๏ธ. |
|||
|
|||
## ๐ฑ โฎ๏ธ โฒ |
|||
|
|||
โก๏ธ ๐ ๐ ๐ โฎ๏ธ ๐ผ. |
|||
|
|||
๐ ๐ ๐ ๏ธ ๐ฑ ๐ โ ๐ ๐งพ. |
|||
|
|||
๐ ๐งพ ๐ โ๏ธ `id`, `title` (๐ฆ), `customer`, & `total`. |
|||
|
|||
๐ฉโ๐ป ๐ ๐ ๏ธ (๐ข ๐ฉโ๐ป) ๐ โ ๐งพ ๐ ๐ ๏ธ โฎ๏ธ ๐ค ๐จ. |
|||
|
|||
โคด๏ธ ๐ ๐ ๏ธ ๐ (โก๏ธ ๐): |
|||
|
|||
* ๐จ ๐งพ ๐ด ๐ข ๐ฉโ๐ป. |
|||
* ๐ ๐ธ. |
|||
* ๐จ ๐จ ๐ ๐ ๏ธ ๐ฉโ๐ป (๐ข ๐ฉโ๐ป). |
|||
* ๐ ๐ ๐จ ๐จ ๐ค ๐จ (โช๏ธโก๏ธ *๐ ๐ ๏ธ*) *๐ข ๐ ๏ธ* ๐ ๐ ๐ข ๐ฉโ๐ป (๐ "โฒ"). |
|||
|
|||
## ๐ **FastAPI** ๐ฑ |
|||
|
|||
โก๏ธ ๐ฅ ๐ โ ๐ ๐ ๏ธ ๐ฑ ๐ ๐ ๐ โญ โ โฒ. |
|||
|
|||
โซ๏ธ ๐ โ๏ธ *โก ๐ ๏ธ* ๐ ๐ ๐จ `Invoice` ๐ช, & ๐ข ๐ข `callback_url` ๐ ๐ ๐ ๐ โฒ. |
|||
|
|||
๐ ๐ ๐ถ ๐, ๐
๐ ๐ฒ โช ๐ฐ ๐: |
|||
|
|||
```Python hl_lines="9-13 36-53" |
|||
{!../../../docs_src/openapi_callbacks/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
`callback_url` ๐ข ๐ข โ๏ธ Pydantic <a href="https://pydantic-docs.helpmanual.io/usage/types/#urls" class="external-link" target="_blank">๐</a> ๐. |
|||
|
|||
๐ด ๐ ๐ `callbacks=messages_callback_router.routes` โ *โก ๐ ๏ธ ๐จโ๐จ*. ๐ฅ ๐ ๐ โซ๏ธโ ๐ โญ. |
|||
|
|||
## ๐ฌ โฒ |
|||
|
|||
โ โฒ ๐ ๐ ๐ช ๐ ๐ ๐ ๐ ๐ ๏ธ ๐ฑ. |
|||
|
|||
& โซ๏ธ ๐ ๐ฒ ๐ช ๐ โช๏ธโก๏ธ 1๏ธโฃ ๐ฑ โญ. |
|||
|
|||
โซ๏ธ ๐ช 1๏ธโฃ โ๏ธ 2๏ธโฃ โธ ๐, ๐: |
|||
|
|||
```Python |
|||
callback_url = "https://example.com/api/v1/invoices/events/" |
|||
httpx.post(callback_url, json={"description": "Invoice paid", "paid": True}) |
|||
``` |
|||
|
|||
โ๏ธ ๐ฒ ๐ โ ๐ โฒ โ ๐ญ ๐ ๐ ๐ ๏ธ ๐ฉโ๐ป (๐ข ๐ฉโ๐ป) ๐ ๏ธ *๐ข ๐ ๏ธ* โ, ๐ ๐ฝ ๐ *๐ ๐ ๏ธ* ๐ ๐จ ๐จ ๐ช โฒ, โ๏ธ. |
|||
|
|||
, โซ๏ธโ ๐ฅ ๐ โญ ๐ฎ ๐ ๐ โ ๐ *๐ข ๐ ๏ธ* ๐ ๐ ๐ ๐จ โฒ โช๏ธโก๏ธ *๐ ๐ ๏ธ*. |
|||
|
|||
๐ ๐งพ ๐ ๐ฆ ๐ ๐ฆ ๐ `/docs` ๐ ๐ ๏ธ, & โซ๏ธ ๐ โก๏ธ ๐ข ๐ฉโ๐ป ๐ญ โ ๐ *๐ข ๐ ๏ธ*. |
|||
|
|||
๐ ๐ผ ๐ซ ๐ ๏ธ โฒ โซ๏ธ (๐ ๐ช โธ ๐), ๐ด ๐งพ ๐. |
|||
|
|||
!!! tip |
|||
โ โฒ ๐บ๐ธ๐ ๐จ. |
|||
|
|||
๐โ ๐ ๏ธ โฒ ๐, ๐ ๐ช โ๏ธ ๐ณ ๐ <a href="https://www.python-httpx.org" class="external-link" target="_blank">๐ธ๐ฒ</a> โ๏ธ <a href="https://requests.readthedocs.io/" class="external-link" target="_blank">๐จ</a>. |
|||
|
|||
## โ โฒ ๐งพ ๐ |
|||
|
|||
๐ ๐ ๐ ๐ซ ๐ ๏ธ ๐ ๐ฑ, ๐ฅ ๐ด ๐ช โซ๏ธ *๐* โ ๐ *๐ข ๐ ๏ธ* ๐ ๐ ๐. |
|||
|
|||
โ๏ธ, ๐ โช ๐ญ โ ๐ช โ ๐ง ๐งพ ๐ ๏ธ โฎ๏ธ **FastAPI**. |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ ๐ ๐ก ๐ โ *๐ข ๐ ๏ธ* ๐ ๐ ๐... ๐ *โก ๐ ๏ธ(โ)* ๐ ๐ข ๐ ๏ธ ๐ ๐ ๏ธ (๐ ๐ ๐ ๏ธ ๐ ๐ค). |
|||
|
|||
!!! tip |
|||
๐โ โ ๐ ๐ โฒ, โซ๏ธ ๐ช โ ๐ ๐ ๐ ๐ *๐ข ๐ฉโ๐ป*. & ๐ ๐ โณ ๐ ๏ธ *๐ข ๐ ๏ธ*, ๐ซ *๐ ๐ ๏ธ*. |
|||
|
|||
๐ ๐ ๏ธ ๐ โ ๐ ( *๐ข ๐ฉโ๐ป*) ๐ช โน ๐ ๐ญ ๐ โซ๏ธ ๐
โญ ๐โ ๐ฎ ๐ข, Pydantic ๐ท ๐ช, ๐จ, โ๏ธ. ๐ *๐ข ๐ ๏ธ*. |
|||
|
|||
### โ โฒ `APIRouter` |
|||
|
|||
๐ฅ โ ๐ `APIRouter` ๐ ๐ ๐ 1๏ธโฃ โ๏ธ ๐
โฒ. |
|||
|
|||
```Python hl_lines="3 25" |
|||
{!../../../docs_src/openapi_callbacks/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ โฒ *โก ๐ ๏ธ* |
|||
|
|||
โ โฒ *โก ๐ ๏ธ* โ๏ธ ๐ `APIRouter` ๐ โ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ ๐ ๐ FastAPI *โก ๐ ๏ธ*: |
|||
|
|||
* โซ๏ธ ๐ ๐ฒ โ๏ธ ๐ ๐ช โซ๏ธ ๐ ๐จ, โ
`body: InvoiceEvent`. |
|||
* & โซ๏ธ ๐ช โ๏ธ ๐ ๐จ โซ๏ธ ๐ ๐จ, โ
`response_model=InvoiceEventReceived`. |
|||
|
|||
```Python hl_lines="16-18 21-22 28-32" |
|||
{!../../../docs_src/openapi_callbacks/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ค 2๏ธโฃ ๐ ๐บ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ*: |
|||
|
|||
* โซ๏ธ ๐ซ ๐ช โ๏ธ ๐ โ ๐, โฉ๏ธ ๐ ๐ฑ ๐ ๐
๐ค ๐ ๐. โซ๏ธ ๐ด โ๏ธ ๐ *๐ข ๐ ๏ธ*. , ๐ข ๐ช โ๏ธ `pass`. |
|||
* *โก* ๐ช ๐ <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#key-expression" class="external-link" target="_blank">๐ 3๏ธโฃ ๐งฌ</a> (๐ ๐ ๐) ๐โ โซ๏ธ ๐ช โ๏ธ ๐ข โฎ๏ธ ๐ข & ๐ โฎ๏ธ ๐จ ๐จ *๐ ๐ ๏ธ*. |
|||
|
|||
### โฒ โก ๐งฌ |
|||
|
|||
โฒ *โก* ๐ช โ๏ธ <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#key-expression" class="external-link" target="_blank">๐ 3๏ธโฃ ๐งฌ</a> ๐ ๐ช ๐ ๐ โฎ๏ธ ๐จ ๐จ *๐ ๐ ๏ธ*. |
|||
|
|||
๐ ๐ผ, โซ๏ธ `str`: |
|||
|
|||
```Python |
|||
"{$callback_url}/invoices/{$request.body.id}" |
|||
``` |
|||
|
|||
, ๐ฅ ๐ ๐ ๏ธ ๐ฉโ๐ป (๐ข ๐ฉโ๐ป) ๐จ ๐จ *๐ ๐ ๏ธ* : |
|||
|
|||
``` |
|||
https://yourapi.com/invoices/?callback_url=https://www.external.org/events |
|||
``` |
|||
|
|||
โฎ๏ธ ๐ป ๐ช: |
|||
|
|||
```JSON |
|||
{ |
|||
"id": "2expen51ve", |
|||
"customer": "Mr. Richie Rich", |
|||
"total": "9999" |
|||
} |
|||
``` |
|||
|
|||
โคด๏ธ *๐ ๐ ๏ธ* ๐ ๐ ๏ธ ๐งพ, & โ โช, ๐จ โฒ ๐จ `callback_url` ( *๐ข ๐ ๏ธ*): |
|||
|
|||
``` |
|||
https://www.external.org/events/invoices/2expen51ve |
|||
``` |
|||
|
|||
โฎ๏ธ ๐ป ๐ช โ ๐ณ ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"description": "Payment celebration", |
|||
"paid": true |
|||
} |
|||
``` |
|||
|
|||
& โซ๏ธ ๐ โ ๐จ โช๏ธโก๏ธ ๐ *๐ข ๐ ๏ธ* โฎ๏ธ ๐ป ๐ช ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"ok": true |
|||
} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ โ โฒ ๐ โ๏ธ ๐ ๐ ๐จ ๐ข ๐ข `callback_url` (`https://www.external.org/events`) & ๐งพ `id` โช๏ธโก๏ธ ๐ ๐ป ๐ช (`2expen51ve`). |
|||
|
|||
### ๐ฎ โฒ ๐ป |
|||
|
|||
๐ โ ๐ โ๏ธ *โฒ โก ๐ ๏ธ(โ)* ๐ช (1๏ธโฃ(โ) ๐ *๐ข ๐ฉโ๐ป* ๐ ๐ ๏ธ *๐ข ๐ ๏ธ*) โฒ ๐ป ๐ โ ๐. |
|||
|
|||
๐ โ๏ธ ๐ข `callbacks` *๐ ๐ ๏ธ โก ๐ ๏ธ ๐จโ๐จ* ๐ถโโ๏ธ ๐ข `.routes` (๐ ๐ค `list` ๐ฃ/*โก ๐ ๏ธ*) โช๏ธโก๏ธ ๐ โฒ ๐ป: |
|||
|
|||
```Python hl_lines="35" |
|||
{!../../../docs_src/openapi_callbacks/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ ๐ซ ๐ถโโ๏ธ ๐ป โซ๏ธ (`invoices_callback_router`) `callback=`, โ๏ธ ๐ข `.routes`, `invoices_callback_router.routes`. |
|||
|
|||
### โ
๐ฉบ |
|||
|
|||
๐ ๐ ๐ช โถ๏ธ ๐ ๐ฑ โฎ๏ธ Uvicorn & ๐ถ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ฉบ โ
"โฒ" ๐ ๐ *โก ๐ ๏ธ* ๐ ๐ฆ โ *๐ข ๐ ๏ธ* ๐ ๐ ๐: |
|||
|
|||
<img src="/img/tutorial/openapi-callbacks/image01.png"> |
@ -0,0 +1,170 @@ |
|||
# โก ๐ ๏ธ ๐ง ๐ณ |
|||
|
|||
## ๐ { |
|||
|
|||
!!! warning |
|||
๐ฅ ๐ ๐ซ "๐ด" ๐, ๐ ๐ฒ ๐ซ ๐ช ๐. |
|||
|
|||
๐ ๐ช โ ๐ `operationId` โ๏ธ ๐ *โก ๐ ๏ธ* โฎ๏ธ ๐ข `operation_id`. |
|||
|
|||
๐ ๐ โ๏ธ โ ๐ญ ๐ โซ๏ธ ๐ ๐ ๐ ๏ธ. |
|||
|
|||
```Python hl_lines="6" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ๏ธ *โก ๐ ๏ธ ๐ข* ๐ { |
|||
|
|||
๐ฅ ๐ ๐ โ๏ธ ๐ ๐' ๐ข ๐ `operationId`โ, ๐ ๐ช ๐ ๐คญ ๐ ๐ซ & ๐ ๐ *โก ๐ ๏ธ* `operation_id` โ๏ธ ๐ซ `APIRoute.name`. |
|||
|
|||
๐ ๐ โซ๏ธ โฎ๏ธ โ ๐ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
```Python hl_lines="2 12-21 24" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial002.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ โ ๐ค `app.openapi()`, ๐ ๐ โน `operationId`โ โญ ๐. |
|||
|
|||
!!! warning |
|||
๐ฅ ๐ ๐, ๐ โ๏ธ โ ๐ญ ๐ 1๏ธโฃ ๐ *โก ๐ ๏ธ ๐ข* โ๏ธ ๐ ๐. |
|||
|
|||
๐ฅ ๐ซ ๐ ๐น (๐ ๐). |
|||
|
|||
## ๐ซ โช๏ธโก๏ธ ๐ |
|||
|
|||
๐ซ *โก ๐ ๏ธ* โช๏ธโก๏ธ ๐ ๐ ๐ (& โก๏ธ, โช๏ธโก๏ธ ๐ง ๐งพ โ๏ธ), โ๏ธ ๐ข `include_in_schema` & โ โซ๏ธ `False`: |
|||
|
|||
```Python hl_lines="6" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial003.py!} |
|||
``` |
|||
|
|||
## ๐ง ๐ โช๏ธโก๏ธ #๏ธโฃ |
|||
|
|||
๐ ๐ช ๐ โธ โ๏ธ โช๏ธโก๏ธ #๏ธโฃ *โก ๐ ๏ธ ๐ข* ๐. |
|||
|
|||
โ `\f` (๐ "๐จ ๐ผ" ๐ฆน) ๐ค **FastAPI** ๐ ๐ข โ๏ธ ๐ ๐ โ. |
|||
|
|||
โซ๏ธ ๐ ๐ซ ๐ฆ ๐ ๐งพ, โ๏ธ ๐ ๐งฐ (โ
๐) ๐ ๐ช โ๏ธ ๐. |
|||
|
|||
```Python hl_lines="19-29" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial004.py!} |
|||
``` |
|||
|
|||
## ๐ ๐จ |
|||
|
|||
๐ ๐ฒ โ๏ธ ๐ โ ๐ฃ `response_model` & `status_code` *โก ๐ ๏ธ*. |
|||
|
|||
๐ ๐ฌ ๐ ๐ ๐ ๐จ *โก ๐ ๏ธ*. |
|||
|
|||
๐ ๐ช ๐ฃ ๐ ๐จ โฎ๏ธ ๐ซ ๐ท, ๐ ๐, โ๏ธ. |
|||
|
|||
๐ค ๐ ๐ ๐ฅ ๐งพ ๐ โซ๏ธ, ๐ ๐ช โ โซ๏ธ [๐ ๐จ ๐](./additional-responses.md){.internal-link target=_blank}. |
|||
|
|||
## ๐ โ |
|||
|
|||
๐โ ๐ ๐ฃ *โก ๐ ๏ธ* ๐ ๐ธ, **FastAPI** ๐ ๐ ๐ ๐ ๐ ๐ *โก ๐ ๏ธ* ๐ ๐ ๐. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ง โซ๏ธ ๐ค <a href="https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#operation-object" class="external-link" target="_blank">๐ ๏ธ ๐</a>. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ โน ๐ *โก ๐ ๏ธ* & โ๏ธ ๐ ๐ง ๐งพ. |
|||
|
|||
โซ๏ธ ๐ `tags`, `parameters`, `requestBody`, `responses`, โ๏ธ. |
|||
|
|||
๐ *โก ๐ ๏ธ*-๐ฏ ๐ ๐ ๐ ๐ ๐ **FastAPI**, โ๏ธ ๐ ๐ช โ โซ๏ธ. |
|||
|
|||
!!! tip |
|||
๐ ๐
๐ โ โ. |
|||
|
|||
๐ฅ ๐ ๐ด ๐ช ๐ฃ ๐ ๐จ, ๐
๐ช ๐ โซ๏ธ โฎ๏ธ [๐ ๐จ ๐](./additional-responses.md){.internal-link target=_blank}. |
|||
|
|||
๐ ๐ช โ ๐ ๐ *โก ๐ ๏ธ* โ๏ธ ๐ข `openapi_extra`. |
|||
|
|||
### ๐ โ |
|||
|
|||
๐ `openapi_extra` ๐ช ๐, ๐ผ, ๐ฃ [๐ โ](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions): |
|||
|
|||
```Python hl_lines="6" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial005.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ ๐ ๐ง ๐ ๏ธ ๐ฉบ, ๐ โ ๐ ๐ฆ ๐ ๐ ๐ฏ *โก ๐ ๏ธ*. |
|||
|
|||
<img src="/img/tutorial/path-operation-advanced-configuration/image01.png"> |
|||
|
|||
& ๐ฅ ๐ ๐ ๐ ๐ ( `/openapi.json` ๐ ๐ ๏ธ), ๐ ๐ ๐ ๐ โ ๐ ๐ฏ *โก ๐ ๏ธ* ๐โโ๏ธ: |
|||
|
|||
```JSON hl_lines="22" |
|||
{ |
|||
"openapi": "3.0.2", |
|||
"info": { |
|||
"title": "FastAPI", |
|||
"version": "0.1.0" |
|||
}, |
|||
"paths": { |
|||
"/items/": { |
|||
"get": { |
|||
"summary": "Read Items", |
|||
"operationId": "read_items_items__get", |
|||
"responses": { |
|||
"200": { |
|||
"description": "Successful Response", |
|||
"content": { |
|||
"application/json": { |
|||
"schema": {} |
|||
} |
|||
} |
|||
} |
|||
}, |
|||
"x-aperture-labs-portal": "blue" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
``` |
|||
|
|||
### ๐ ๐ *โก ๐ ๏ธ* ๐ |
|||
|
|||
๐ `openapi_extra` ๐ ๐ ๐ โฎ๏ธ ๐ ๐ ๐ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
, ๐ ๐ช ๐ฎ ๐ ๐ฝ ๐ ๐ ๐. |
|||
|
|||
๐ผ, ๐ ๐ช ๐ญ โ & โ ๐จ โฎ๏ธ ๐ ๐ ๐, ๐ต โ๏ธ ๐ง โ FastAPI โฎ๏ธ Pydantic, โ๏ธ ๐ ๐ช ๐ ๐ฌ ๐จ ๐ ๐. |
|||
|
|||
๐ ๐ช ๐ โฎ๏ธ `openapi_extra`: |
|||
|
|||
```Python hl_lines="20-37 39-40" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial006.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, ๐ฅ ๐ซ ๐ฃ ๐ Pydantic ๐ท. ๐, ๐จ ๐ช ๐ซ <abbr title="converted from some plain format, like bytes, into Python objects">๐ป</abbr> ๐ป, โซ๏ธ โ ๐ `bytes`, & ๐ข `magic_data_reader()` ๐ ๐ ๐ป โซ๏ธ ๐. |
|||
|
|||
๐, ๐ฅ ๐ช ๐ฃ ๐ ๐ ๐จ ๐ช. |
|||
|
|||
### ๐ ๐ ๐ ๐ |
|||
|
|||
โ๏ธ ๐ ๐ ๐ฑ, ๐ ๐ช โ๏ธ Pydantic ๐ท ๐ฌ ๐ป ๐ ๐ โคด๏ธ ๐ ๐ ๐ ๐ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
& ๐ ๐ช ๐ ๐ฅ ๐ฝ ๐ ๐จ ๐ซ ๐ป. |
|||
|
|||
๐ผ, ๐ ๐ธ ๐ฅ ๐ซ โ๏ธ FastAPI ๐ ๏ธ ๐ ๏ธ โ ๐ป ๐ โช๏ธโก๏ธ Pydantic ๐ท ๐ซ ๐ง ๐ฌ ๐ป. ๐, ๐ฅ ๐ฃ ๐จ ๐ ๐ ๐, ๐ซ ๐ป: |
|||
|
|||
```Python hl_lines="17-22 24" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial007.py!} |
|||
``` |
|||
|
|||
๐, ๐ ๐ฅ ๐ซ โ๏ธ ๐ข ๐ ๏ธ ๐ ๏ธ, ๐ฅ โ๏ธ Pydantic ๐ท โ ๐ ๐ป ๐ ๐ฝ ๐ ๐ฅ ๐ ๐จ ๐. |
|||
|
|||
โคด๏ธ ๐ฅ โ๏ธ ๐จ ๐, & โ ๐ช `bytes`. ๐ โ ๐ FastAPI ๐ ๐ซ ๐ ๐ป ๐จ ๐ ๐ป. |
|||
|
|||
& โคด๏ธ ๐ ๐, ๐ฅ ๐ป ๐ ๐ ๐ ๐, & โคด๏ธ ๐ฅ ๐ โ๏ธ ๐ Pydantic ๐ท โ ๐ ๐: |
|||
|
|||
```Python hl_lines="26-33" |
|||
{!../../../docs_src/path_operation_advanced_configuration/tutorial007.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ฅ ๐ค-โ๏ธ ๐ Pydantic ๐ท. |
|||
|
|||
โ๏ธ ๐ ๐, ๐ฅ ๐ช โ๏ธ โ โซ๏ธ ๐ ๐. |
@ -0,0 +1,33 @@ |
|||
# ๐จ - ๐ ๐ ๐ |
|||
|
|||
๐ ๐ฒ โ โญ ๐ ๐ ๐ช โ ๐ข [๐จ ๐ ๐](../tutorial/response-status-code.md){.internal-link target=_blank}. |
|||
|
|||
โ๏ธ ๐ผ ๐ ๐ช ๐จ ๐ ๐ ๐ ๐ ๐ข. |
|||
|
|||
## โ๏ธ ๐ผ |
|||
|
|||
๐ผ, ๐ ๐ ๐ ๐ ๐จ ๐บ๐ธ๐ ๐ ๐ "๐" `200` ๐ข. |
|||
|
|||
โ๏ธ ๐ฅ ๐ฝ ๐ซ ๐, ๐ ๐ โ โซ๏ธ, & ๐จ ๐บ๐ธ๐ ๐ ๐ "โ" `201`. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ช โฝ & ๐ ๐ฝ ๐ ๐จ โฎ๏ธ `response_model`. |
|||
|
|||
๐ ๐ผ, ๐ ๐ช โ๏ธ `Response` ๐ข. |
|||
|
|||
## โ๏ธ `Response` ๐ข |
|||
|
|||
๐ ๐ช ๐ฃ ๐ข ๐ `Response` ๐ *โก ๐ ๏ธ ๐ข* (๐ ๐ช ๐ช & ๐). |
|||
|
|||
& โคด๏ธ ๐ ๐ช โ `status_code` ๐ *๐* ๐จ ๐. |
|||
|
|||
```Python hl_lines="1 9 12" |
|||
{!../../../docs_src/response_change_status_code/tutorial001.py!} |
|||
``` |
|||
|
|||
& โคด๏ธ ๐ ๐ช ๐จ ๐ ๐ ๐ ๐ช, ๐ ๐ ๐ ( `dict`, ๐ฝ ๐ท, โ๏ธ). |
|||
|
|||
& ๐ฅ ๐ ๐ฃ `response_model`, โซ๏ธ ๐ โ๏ธ โฝ & ๐ ๐ ๐ ๐จ. |
|||
|
|||
**FastAPI** ๐ โ๏ธ ๐ *๐* ๐จ โ ๐ ๐ (๐ช & ๐), & ๐ ๐ฎ ๐ซ ๐ ๐จ ๐ ๐ ๐ฒ ๐ ๐จ, โฝ ๐ `response_model`. |
|||
|
|||
๐ ๐ช ๐ฃ `Response` ๐ข ๐, & โ ๐ ๐ ๐ซ. โ๏ธ โ๏ธ ๐คฏ ๐ ๐ 1๏ธโฃ โ ๐ ๐. |
@ -0,0 +1,49 @@ |
|||
# ๐จ ๐ช |
|||
|
|||
## โ๏ธ `Response` ๐ข |
|||
|
|||
๐ ๐ช ๐ฃ ๐ข ๐ `Response` ๐ *โก ๐ ๏ธ ๐ข*. |
|||
|
|||
& โคด๏ธ ๐ ๐ช โ ๐ช ๐ *๐* ๐จ ๐. |
|||
|
|||
```Python hl_lines="1 8-9" |
|||
{!../../../docs_src/response_cookies/tutorial002.py!} |
|||
``` |
|||
|
|||
& โคด๏ธ ๐ ๐ช ๐จ ๐ ๐ ๐ ๐ช, ๐ ๐ ๐ ( `dict`, ๐ฝ ๐ท, โ๏ธ). |
|||
|
|||
& ๐ฅ ๐ ๐ฃ `response_model`, โซ๏ธ ๐ โ๏ธ โฝ & ๐ ๐ ๐ ๐จ. |
|||
|
|||
**FastAPI** ๐ โ๏ธ ๐ *๐* ๐จ โ ๐ช (๐ & ๐ ๐), & ๐ ๐ฎ ๐ซ ๐ ๐จ ๐ ๐ ๐ฒ ๐ ๐จ, โฝ ๐ `response_model`. |
|||
|
|||
๐ ๐ช ๐ฃ `Response` ๐ข ๐, & โ ๐ช (& ๐) ๐ซ. |
|||
|
|||
## ๐จ `Response` ๐ |
|||
|
|||
๐ ๐ช โ ๐ช ๐โ ๐ฌ `Response` ๐ ๐ ๐. |
|||
|
|||
๐, ๐ ๐ช โ ๐จ ๐ฌ [๐จ ๐จ ๐](response-directly.md){.internal-link target=_blank}. |
|||
|
|||
โคด๏ธ โ ๐ช โซ๏ธ, & โคด๏ธ ๐จ โซ๏ธ: |
|||
|
|||
```Python hl_lines="10-12" |
|||
{!../../../docs_src/response_cookies/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
โ๏ธ ๐คฏ ๐ ๐ฅ ๐ ๐จ ๐จ ๐ โฉ๏ธ โ๏ธ `Response` ๐ข, FastAPI ๐ ๐จ โซ๏ธ ๐. |
|||
|
|||
, ๐ ๐ โ๏ธ โ ๐ญ ๐ ๐ฝ โ ๐. ๐คถ โ. โซ๏ธ ๐ โฎ๏ธ ๐ป, ๐ฅ ๐ ๐ฌ `JSONResponse`. |
|||
|
|||
& ๐ ๐ ๐ซ ๐จ ๐ ๐ ๐ ๐ โ๏ธ โฝ `response_model`. |
|||
|
|||
### ๐
โน |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.responses import Response` โ๏ธ `from starlette.responses import JSONResponse`. |
|||
|
|||
**FastAPI** ๐ ๐ `starlette.responses` `fastapi.responses` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐. |
|||
|
|||
& `Response` ๐ช โ๏ธ ๐ โ ๐ & ๐ช, **FastAPI** ๐ โซ๏ธ `fastapi.Response`. |
|||
|
|||
๐ ๐ ๐ช ๐ข & ๐, โ
<a href="https://www.starlette.io/responses/#set-cookie" class="external-link" target="_blank">๐งพ ๐</a>. |
@ -0,0 +1,63 @@ |
|||
# ๐จ ๐จ ๐ |
|||
|
|||
๐โ ๐ โ **FastAPI** *โก ๐ ๏ธ* ๐ ๐ช ๐ ๐จ ๐ ๐ โช๏ธโก๏ธ โซ๏ธ: `dict`, `list`, Pydantic ๐ท, ๐ฝ ๐ท, โ๏ธ. |
|||
|
|||
๐ข, **FastAPI** ๐ ๐ ๐ ๐ ๐จ ๐ฒ ๐ป โ๏ธ `jsonable_encoder` ๐ฌ [๐ป ๐ ๐ข](../tutorial/encoder.md){.internal-link target=_blank}. |
|||
|
|||
โคด๏ธ, โ
๐, โซ๏ธ ๐ ๐ฎ ๐ ๐ป-๐ ๐ฝ (โ
`dict`) ๐ `JSONResponse` ๐ ๐ โ๏ธ ๐จ ๐จ ๐ฉโ๐ป. |
|||
|
|||
โ๏ธ ๐ ๐ช ๐จ `JSONResponse` ๐ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
โซ๏ธ ๐ช โ , ๐ผ, ๐จ ๐ ๐ โ๏ธ ๐ช. |
|||
|
|||
## ๐จ `Response` |
|||
|
|||
๐, ๐ ๐ช ๐จ ๐ `Response` โ๏ธ ๐ ๐ง-๐ โซ๏ธ. |
|||
|
|||
!!! tip |
|||
`JSONResponse` โซ๏ธ ๐ง-๐ `Response`. |
|||
|
|||
& ๐โ ๐ ๐จ `Response`, **FastAPI** ๐ ๐ถโโ๏ธ โซ๏ธ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ซ ๐ ๐ฝ ๐ ๏ธ โฎ๏ธ Pydantic ๐ท, โซ๏ธ ๐ ๐ซ ๐ ๐ ๐ ๐, โ๏ธ. |
|||
|
|||
๐ ๐ค ๐ ๐ ๐ช. ๐ ๐ช ๐จ ๐ ๐ ๐, ๐ ๐ ๐ฝ ๐ โ๏ธ ๐ฌ, โ๏ธ. |
|||
|
|||
## โ๏ธ `jsonable_encoder` `Response` |
|||
|
|||
โฉ๏ธ **FastAPI** ๐ซ ๐ ๐ `Response` ๐ ๐จ, ๐ โ๏ธ โ ๐ญ โซ๏ธ ๐ ๐ โซ๏ธ. |
|||
|
|||
๐ผ, ๐ ๐ซ๐ ๐ฎ Pydantic ๐ท `JSONResponse` ๐ต ๐ฅ ๐ญ โซ๏ธ `dict` โฎ๏ธ ๐ ๐ ๐ (๐ `datetime`, `UUID`, โ๏ธ) ๐ ๐ป-๐ ๐. |
|||
|
|||
๐ ๐ผ, ๐ ๐ช โ๏ธ `jsonable_encoder` ๐ ๐ ๐ โญ ๐ถโโ๏ธ โซ๏ธ ๐จ: |
|||
|
|||
```Python hl_lines="6-7 21-22" |
|||
{!../../../docs_src/response_directly/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.responses import JSONResponse`. |
|||
|
|||
**FastAPI** ๐ ๐ `starlette.responses` `fastapi.responses` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐. |
|||
|
|||
## ๐ฌ ๐ `Response` |
|||
|
|||
๐ผ ๐ ๐ฆ ๐ ๐ ๐ ๐ช, โ๏ธ โซ๏ธ ๐ซ ๐ถ โ , ๐ ๐ช โ๏ธ ๐จ `item` ๐, & **FastAPI** ๐ ๐ฎ โซ๏ธ `JSONResponse` ๐, ๐ญ โซ๏ธ `dict`, โ๏ธ. ๐ ๐ ๐ข. |
|||
|
|||
๐, โก๏ธ ๐ โ ๐ ๐ช โ๏ธ ๐ ๐จ ๐ ๐จ. |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ ๐ ๐จ <a href="https://en.wikipedia.org/wiki/XML" class="external-link" target="_blank">๐</a> ๐จ. |
|||
|
|||
๐ ๐ช ๐ฎ ๐ ๐ ๐ ๐ป, ๐ฎ โซ๏ธ `Response`, & ๐จ โซ๏ธ: |
|||
|
|||
```Python hl_lines="1 18" |
|||
{!../../../docs_src/response_directly/tutorial002.py!} |
|||
``` |
|||
|
|||
## ๐ |
|||
|
|||
๐โ ๐ ๐จ `Response` ๐ ๐ฎ ๐ ๐ซ โ, ๐ (๐ป), ๐ซ ๐ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ โซ๏ธ ๐ฌ [๐ ๐จ ๐](additional-responses.md){.internal-link target=_blank}. |
|||
|
|||
๐ ๐ช ๐ โช ๐ โ โ๏ธ/๐ฃ ๐ ๐ `Response`โ โช โ๏ธ ๐ง ๐ฝ ๐ ๏ธ, ๐งพ, โ๏ธ. |
@ -0,0 +1,42 @@ |
|||
# ๐จ ๐ |
|||
|
|||
## โ๏ธ `Response` ๐ข |
|||
|
|||
๐ ๐ช ๐ฃ ๐ข ๐ `Response` ๐ *โก ๐ ๏ธ ๐ข* (๐ ๐ช ๐ช). |
|||
|
|||
& โคด๏ธ ๐ ๐ช โ ๐ ๐ *๐* ๐จ ๐. |
|||
|
|||
```Python hl_lines="1 7-8" |
|||
{!../../../docs_src/response_headers/tutorial002.py!} |
|||
``` |
|||
|
|||
& โคด๏ธ ๐ ๐ช ๐จ ๐ ๐ ๐ ๐ช, ๐ ๐ ๐ ( `dict`, ๐ฝ ๐ท, โ๏ธ). |
|||
|
|||
& ๐ฅ ๐ ๐ฃ `response_model`, โซ๏ธ ๐ โ๏ธ โฝ & ๐ ๐ ๐ ๐จ. |
|||
|
|||
**FastAPI** ๐ โ๏ธ ๐ *๐* ๐จ โ ๐ (๐ช & ๐ ๐), & ๐ ๐ฎ ๐ซ ๐ ๐จ ๐ ๐ ๐ฒ ๐ ๐จ, โฝ ๐ `response_model`. |
|||
|
|||
๐ ๐ช ๐ฃ `Response` ๐ข ๐, & โ ๐ (& ๐ช) ๐ซ. |
|||
|
|||
## ๐จ `Response` ๐ |
|||
|
|||
๐ ๐ช ๐ฎ ๐ ๐โ ๐ ๐จ `Response` ๐. |
|||
|
|||
โ ๐จ ๐ฌ [๐จ ๐จ ๐](response-directly.md){.internal-link target=_blank} & ๐ถโโ๏ธ ๐ ๐ ๐ข: |
|||
|
|||
```Python hl_lines="10-12" |
|||
{!../../../docs_src/response_headers/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.responses import Response` โ๏ธ `from starlette.responses import JSONResponse`. |
|||
|
|||
**FastAPI** ๐ ๐ `starlette.responses` `fastapi.responses` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐. |
|||
|
|||
& `Response` ๐ช โ๏ธ ๐ โ ๐ & ๐ช, **FastAPI** ๐ โซ๏ธ `fastapi.Response`. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
โ๏ธ ๐คฏ ๐ ๐ ยฉ ๐ ๐ช ๐ฎ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" class="external-link" target="_blank">โ๏ธ 'โ-' ๐ก</a>. |
|||
|
|||
โ๏ธ ๐ฅ ๐ โ๏ธ ๐ ๐ ๐ ๐ ๐ ๐ฉโ๐ป ๐ฅ ๐ช ๐, ๐ ๐ช ๐ฎ ๐ซ ๐ โ ๐ณ (โ ๐
[โ (โ๏ธ-๐จ๐ณ โน ๐ค)](../tutorial/cors.md){.internal-link target=_blank}), โ๏ธ ๐ข `expose_headers` ๐ <a href="https://www.starlette.io/middleware/#corsmiddleware" class="external-link" target="_blank">๐ โ ๐ฉบ</a>. |
@ -0,0 +1,113 @@ |
|||
# ๐บ๐ธ๐ ๐ฐ ๐ |
|||
|
|||
๐
๐ผ, ๐ ๐ช โ๏ธ ๐บ๐ธ๐ ๐ฐ ๐. |
|||
|
|||
๐บ๐ธ๐ ๐ฐ ๐, ๐ธ โ ๐ ๐ ๐ ๐ & ๐. |
|||
|
|||
๐ฅ โซ๏ธ ๐ซ ๐จ โซ๏ธ, โซ๏ธ ๐จ ๐บ๐ธ๐ 4๏ธโฃ0๏ธโฃ1๏ธโฃ "โ" โ. |
|||
|
|||
& ๐จ ๐ `WWW-Authenticate` โฎ๏ธ ๐ฒ `Basic`, & ๐ฆ `realm` ๐ข. |
|||
|
|||
๐ ๐ฌ ๐ฅ ๐ฆ ๐ ๏ธ ๐ ๐ & ๐. |
|||
|
|||
โคด๏ธ, ๐โ ๐ ๐ ๐ ๐ & ๐, ๐ฅ ๐จ ๐ซ ๐ ๐. |
|||
|
|||
## ๐
๐บ๐ธ๐ ๐ฐ ๐ |
|||
|
|||
* ๐ `HTTPBasic` & `HTTPBasicCredentials`. |
|||
* โ "`security` โ" โ๏ธ `HTTPBasic`. |
|||
* โ๏ธ ๐ `security` โฎ๏ธ ๐ ๐ *โก ๐ ๏ธ*. |
|||
* โซ๏ธ ๐จ ๐ ๐ `HTTPBasicCredentials`: |
|||
* โซ๏ธ ๐ `username` & `password` ๐จ. |
|||
|
|||
```Python hl_lines="2 6 10" |
|||
{!../../../docs_src/security/tutorial006.py!} |
|||
``` |
|||
|
|||
๐โ ๐ ๐ ๐ ๐ ๐ฅ ๐ฐ (โ๏ธ ๐ "๐ ๏ธ" ๐ผ ๐ฉบ) ๐ฅ ๐ ๐ญ ๐ ๐ ๐ & ๐: |
|||
|
|||
<img src="/img/tutorial/security/image12.png"> |
|||
|
|||
## โ
๐ |
|||
|
|||
๐ฅ ๐
๐ ๐ผ. |
|||
|
|||
โ๏ธ ๐ โ
๐ฅ ๐ & ๐ โ. |
|||
|
|||
๐, โ๏ธ ๐ ๐ฉ ๐น <a href="https://docs.python.org/3/library/secrets.html" class="external-link" target="_blank">`secrets`</a> โ
๐ & ๐. |
|||
|
|||
`secrets.compare_digest()` ๐ช โ `bytes` โ๏ธ `str` ๐ ๐ด ๐ ๐ ๐ฆน (๐ ๐ช๐ธ), ๐ โ โซ๏ธ ๐ซ๐ ๐ท โฎ๏ธ ๐ฆน ๐ `รก`, `Sebastiรกn`. |
|||
|
|||
๐ต ๐, ๐ฅ ๐ฅ ๐ `username` & `password` `bytes` ๐ข ๐ซ โฎ๏ธ ๐ -8๏ธโฃ. |
|||
|
|||
โคด๏ธ ๐ฅ ๐ช โ๏ธ `secrets.compare_digest()` ๐ ๐ `credentials.username` `"stanleyjobson"`, & ๐ `credentials.password` `"swordfish"`. |
|||
|
|||
```Python hl_lines="1 11-21" |
|||
{!../../../docs_src/security/tutorial007.py!} |
|||
``` |
|||
|
|||
๐ ๐ ๐: |
|||
|
|||
```Python |
|||
if not (credentials.username == "stanleyjobson") or not (credentials.password == "swordfish"): |
|||
# Return some error |
|||
... |
|||
``` |
|||
|
|||
โ๏ธ โ๏ธ `secrets.compare_digest()` โซ๏ธ ๐ ๐ ๐ก ๐ ๐ ๐ค "๐ฐ ๐". |
|||
|
|||
### โฒ ๐ |
|||
|
|||
โ๏ธ โซ๏ธโ "โฒ ๐"โ |
|||
|
|||
โก๏ธ ๐ ๐ ๐ ๐ญ ๐ & ๐. |
|||
|
|||
& ๐ซ ๐จ ๐จ โฎ๏ธ ๐ `johndoe` & ๐ `love123`. |
|||
|
|||
โคด๏ธ ๐ ๐ ๐ ๐ธ ๐ ๐ ๐ณ ๐: |
|||
|
|||
```Python |
|||
if "johndoe" == "stanleyjobson" and "love123" == "swordfish": |
|||
... |
|||
``` |
|||
|
|||
โ๏ธ โถ๏ธ๏ธ ๐ ๐ ๐ฌ ๐ฅ `j` `johndoe` ๐ฅ `s` `stanleyjobson`, โซ๏ธ ๐ ๐จ `False`, โฉ๏ธ โซ๏ธ โช ๐ญ ๐ ๐ 2๏ธโฃ ๐ป ๐ซ ๐, ๐ญ ๐ "๐ค ๐
โโ ๐ช ๐ ๐
๐ โ ๐ ๐ค". & ๐ ๐ธ ๐ ๐ฌ "โ ๐ฉโ๐ป โ๏ธ ๐". |
|||
|
|||
โ๏ธ โคด๏ธ ๐ ๐ โฎ๏ธ ๐ `stanleyjobsox` & ๐ `love123`. |
|||
|
|||
& ๐ ๐ธ ๐ ๐จ ๐ณ ๐: |
|||
|
|||
```Python |
|||
if "stanleyjobsox" == "stanleyjobson" and "love123" == "swordfish": |
|||
... |
|||
``` |
|||
|
|||
๐ ๐ โ๏ธ ๐ฌ ๐ `stanleyjobso` ๐ฏโโ๏ธ `stanleyjobsox` & `stanleyjobson` โญ ๐ค ๐ ๐ฏโโ๏ธ ๐ป ๐ซ ๐. โซ๏ธ ๐ โ โ โฒ ๐จ ๐ "โ ๐ฉโ๐ป โ๏ธ ๐". |
|||
|
|||
#### ๐ฐ โ โน ๐ |
|||
|
|||
๐ โ, ๐ ๐ ๐ฝ โ โฒ ๐ ๐จ "โ ๐ฉโ๐ป โ๏ธ ๐" ๐จ, ๐ ๐ ๐ญ ๐ ๐ซ ๐ค _๐ณ_ โถ๏ธ๏ธ, โถ๏ธ ๐ค โถ๏ธ๏ธ. |
|||
|
|||
& โคด๏ธ ๐ซ ๐ช ๐ ๐ ๐ค ๐ โซ๏ธ ๐ฒ ๐ณ ๐ ๐ `stanleyjobsox` ๐ `johndoe`. |
|||
|
|||
#### "๐ด" ๐ |
|||
|
|||
โ๏ธ, ๐ ๐ ๐ซ ๐ ๐ ๐ โ, ๐ซ ๐ โ ๐ โซ๏ธ, ๐ฒ โฎ๏ธ ๐ฏ โ๏ธ ๐ฏ ๐ฏ ๐ ๐ฅ. & ๐ ๐ค 1๏ธโฃ โ โ ๐ค ๐ฐ. |
|||
|
|||
โ๏ธ ๐จ ๐, โฒ โ๏ธ ๐ ๐ ๐ โ๏ธ ๐ญ โ ๐ & ๐, โฎ๏ธ "โน" ๐ ๐ธ, โ๏ธ ๐ฐ โ โ. |
|||
|
|||
#### ๐ง โซ๏ธ โฎ๏ธ `secrets.compare_digest()` |
|||
|
|||
โ๏ธ ๐ ๐ ๐ฅ ๐ค โ๏ธ `secrets.compare_digest()`. |
|||
|
|||
๐, โซ๏ธ ๐ โ ๐ ๐ฐ ๐ฌ `stanleyjobsox` `stanleyjobson` ๐ โซ๏ธ โ ๐ฌ `johndoe` `stanleyjobson`. & ๐ ๐. |
|||
|
|||
๐ ๐, โ๏ธ `secrets.compare_digest()` ๐ ๐ธ ๐, โซ๏ธ ๐ ๐ ๐ก ๐ ๐ โ ๐โโ ๐. |
|||
|
|||
### ๐จ โ |
|||
|
|||
โฎ๏ธ ๐ ๐ ๐ โ, ๐จ `HTTPException` โฎ๏ธ ๐ ๐ 4๏ธโฃ0๏ธโฃ1๏ธโฃ (๐ ๐จ ๐โ ๐
โโ ๐ ๐) & ๐ฎ ๐ `WWW-Authenticate` โ ๐ฅ ๐ฆ ๐ณ ๐ ๐: |
|||
|
|||
```Python hl_lines="23-27" |
|||
{!../../../docs_src/security/tutorial007.py!} |
|||
``` |
@ -0,0 +1,16 @@ |
|||
# ๐ง ๐โโ |
|||
|
|||
## ๐ โ |
|||
|
|||
๐ค โ โ ๐ต ๐โโ โ๏ธ โช๏ธโก๏ธ ๐ ๐ [๐ฐ - ๐ฉโ๐ป ๐ฆฎ: ๐โโ](../../tutorial/security/){.internal-link target=_blank}. |
|||
|
|||
!!! tip |
|||
โญ ๐ **๐ซ ๐ฏ "๐ง"**. |
|||
|
|||
& โซ๏ธ ๐ช ๐ ๐ โ๏ธ ๐ผ, โ 1๏ธโฃ ๐ซ. |
|||
|
|||
## โ ๐ฐ ๐ฅ |
|||
|
|||
โญ ๐ ๐ค ๐ โช โ ๐ [๐ฐ - ๐ฉโ๐ป ๐ฆฎ: ๐โโ](../../tutorial/security/){.internal-link target=_blank}. |
|||
|
|||
๐ซ ๐ โ๏ธ ๐ ๐ ๐ง, โ๏ธ โ โ ๐ ๏ธ. |
@ -0,0 +1,269 @@ |
|||
# Oauth2๏ธโฃ โ |
|||
|
|||
๐ ๐ช โ๏ธ Oauth2๏ธโฃ โ ๐ โฎ๏ธ **FastAPI**, ๐ซ ๐ ๏ธ ๐ท ๐. |
|||
|
|||
๐ ๐ โ ๐ โ๏ธ ๐ ๐-๐งฝ โ โ๏ธ, ๐ Oauth2๏ธโฃ ๐ฉ, ๐ ๏ธ ๐ ๐ ๐ ๐ธ (& ๐ ๏ธ ๐ฉบ). |
|||
|
|||
Oauth2๏ธโฃ โฎ๏ธ โ ๐ ๏ธ โ๏ธ ๐ ๐ฆ ๐ค ๐โ๐ฆบ, ๐ ๐ฑ๐, ๐บ๐ธ๐, ๐, ๐คธโโ, ๐ฑ๐, โ๏ธ. ๐ซ โ๏ธ โซ๏ธ ๐ ๐ฏ โ ๐ฉโ๐ป & ๐ธ. |
|||
|
|||
๐ ๐ฐ ๐ "๐น โฎ๏ธ" ๐ฑ๐, ๐บ๐ธ๐, ๐, ๐คธโโ, ๐ฑ๐, ๐ ๐ธ โ๏ธ Oauth2๏ธโฃ โฎ๏ธ โ. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ โ ๐ ๏ธ ๐ค & โ โฎ๏ธ ๐ Oauth2๏ธโฃ โฎ๏ธ โ ๐ **FastAPI** ๐ธ. |
|||
|
|||
!!! warning |
|||
๐ ๐
โ๏ธ ๐ ๐ง ๐. ๐ฅ ๐ โถ๏ธ, ๐ ๐ช ๐ถ โซ๏ธ. |
|||
|
|||
๐ ๐ซ ๐ฏ ๐ช Oauth2๏ธโฃ โ, & ๐ ๐ช ๐ต ๐ค & โ ๐ ๐ ๐. |
|||
|
|||
โ๏ธ Oauth2๏ธโฃ โฎ๏ธ โ ๐ช ๐ ๐ ๏ธ ๐ ๐ ๐ ๏ธ (โฎ๏ธ ๐) & ๐ ๐ ๏ธ ๐ฉบ. |
|||
|
|||
๐, ๐ ๐ ๏ธ ๐ โ, โ๏ธ ๐ ๐ ๐โโ/โ ๐, ๐ ๐ ๐ช, ๐ ๐. |
|||
|
|||
๐ ๐ผ, Oauth2๏ธโฃ โฎ๏ธ โ ๐ช ๐น. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ญ ๐ ๐ช โซ๏ธ, โ๏ธ ๐ ๐, ๐ง ๐. |
|||
|
|||
## Oauth2๏ธโฃ โ & ๐ |
|||
|
|||
Oauth2๏ธโฃ ๐ง ๐ฌ "โ" ๐ ๐ป ๐ ๐. |
|||
|
|||
๐ ๐ ๐ ๐ป ๐ช โ๏ธ ๐ ๐, โ๏ธ ๐ ๐ซ ๐ ๐. |
|||
|
|||
๐ซ โ ๐จ "โ". |
|||
|
|||
๐ (โ
๐ ๏ธ ๐ฉบ), ๐ ๐ช ๐ฌ "๐โโ โ". |
|||
|
|||
๐โ 1๏ธโฃ ๐ซ ๐โโ โ โ๏ธ Oauth2๏ธโฃ, ๐ ๐ช ๐ฃ & โ๏ธ โ. |
|||
|
|||
๐ "โ" ๐ป (๐ต ๐). |
|||
|
|||
๐ซ ๐ โ๏ธ ๐ฃ ๐ฏ ๐โโ โ, ๐ผ: |
|||
|
|||
* `users:read` โ๏ธ `users:write` โ ๐ผ. |
|||
* `instagram_basic` โ๏ธ ๐ฑ๐ / ๐ฑ๐. |
|||
* `https://www.googleapis.com/auth/drive` โ๏ธ ๐บ๐ธ๐. |
|||
|
|||
!!! info |
|||
Oauth2๏ธโฃ "โ" ๐ป ๐ ๐ฃ ๐ฏ โ โ. |
|||
|
|||
โซ๏ธ ๐ซ ๐ค ๐ฅ โซ๏ธ โ๏ธ ๐ ๐ฆน ๐ `:` โ๏ธ ๐ฅ โซ๏ธ ๐. |
|||
|
|||
๐ โน ๐ ๏ธ ๐ฏ. |
|||
|
|||
Oauth2๏ธโฃ ๐ซ ๐ป. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ฅ, โก๏ธ ๐ ๐ ๐ ๐ ๐ โช๏ธโก๏ธ ๐ผ ๐ **๐ฐ - ๐ฉโ๐ป ๐ฆฎ** [Oauth2๏ธโฃ โฎ๏ธ ๐ (& ๐), ๐จ โฎ๏ธ ๐ฅ ๐ค](../../tutorial/security/oauth2-jwt.md){.internal-link target=_blank}. ๐ โ๏ธ Oauth2๏ธโฃ โ: |
|||
|
|||
```Python hl_lines="2 4 8 12 46 64 105 107-115 121-124 128-134 139 153" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
๐ โก๏ธ ๐ ๐ ๐ ๐ ๐. |
|||
|
|||
## Oauth2๏ธโฃ ๐โโ โ |
|||
|
|||
๐ฅ ๐ ๐ ๐ ๐ฅ ๐ฃ Oauth2๏ธโฃ ๐โโ โ โฎ๏ธ 2๏ธโฃ ๐ช โ, `me` & `items`. |
|||
|
|||
`scopes` ๐ข ๐จ `dict` โฎ๏ธ ๐ โ ๐ & ๐ ๐ฒ: |
|||
|
|||
```Python hl_lines="62-65" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
โฉ๏ธ ๐ฅ ๐ ๐ฃ ๐ โ, ๐ซ ๐ ๐ฆ ๐ ๐ ๏ธ ๐ฉบ ๐โ ๐ ๐น-/โ. |
|||
|
|||
& ๐ ๐ ๐ช ๐ โ โ ๐ ๐ ๐ค ๐: `me` & `items`. |
|||
|
|||
๐ ๐ ๐ ๏ธ โ๏ธ ๐โ ๐ ๐ค โ โช ๐จ โฎ๏ธ ๐ฑ๐, ๐บ๐ธ๐, ๐, โ๏ธ: |
|||
|
|||
<img src="/img/tutorial/security/image11.png"> |
|||
|
|||
## ๐ฅ ๐ค โฎ๏ธ โ |
|||
|
|||
๐, ๐ ๐ค *โก ๐ ๏ธ* ๐จ โ ๐จ. |
|||
|
|||
๐ฅ โ๏ธ ๐ `OAuth2PasswordRequestForm`. โซ๏ธ ๐ ๐ `scopes` โฎ๏ธ `list` `str`, โฎ๏ธ ๐ โ โซ๏ธ ๐จ ๐จ. |
|||
|
|||
& ๐ฅ ๐จ โ ๐ ๐ฅ ๐ค. |
|||
|
|||
!!! danger |
|||
๐ฆ, ๐ฅ ๐ฅ โ โ ๐จ ๐ ๐ค. |
|||
|
|||
โ๏ธ ๐ ๐ธ, ๐โโ, ๐ ๐ โ ๐ญ ๐ ๐ด ๐ฎ โ ๐ ๐ฉโ๐ป ๐ค ๐ช โ๏ธ, โ๏ธ ๐ ๐ โ๏ธ ๐. |
|||
|
|||
```Python hl_lines="153" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
## ๐ฃ โ *โก ๐ ๏ธ* & ๐ |
|||
|
|||
๐ ๐ฅ ๐ฃ ๐ *โก ๐ ๏ธ* `/users/me/items/` ๐ โ `items`. |
|||
|
|||
๐, ๐ฅ ๐ & โ๏ธ `Security` โช๏ธโก๏ธ `fastapi`. |
|||
|
|||
๐ ๐ช โ๏ธ `Security` ๐ฃ ๐ (๐ `Depends`), โ๏ธ `Security` ๐จ ๐ข `scopes` โฎ๏ธ ๐ โ (๐ป). |
|||
|
|||
๐ ๐ผ, ๐ฅ ๐ถโโ๏ธ ๐ ๐ข `get_current_active_user` `Security` (๐ ๐ ๐ฅ ๐ โฎ๏ธ `Depends`). |
|||
|
|||
โ๏ธ ๐ฅ ๐ถโโ๏ธ `list` โ, ๐ ๐ผ โฎ๏ธ 1๏ธโฃ โ: `items` (โซ๏ธ ๐ช โ๏ธ ๐
). |
|||
|
|||
& ๐ ๐ข `get_current_active_user` ๐ช ๐ฃ ๐ง-๐, ๐ซ ๐ด โฎ๏ธ `Depends` โ๏ธ โฎ๏ธ `Security`. ๐ฃ ๐ฎ ๐ ๐ง-๐ ๐ข (`get_current_user`), & ๐ โ ๐. |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ โ `me` (โซ๏ธ ๐ช ๐ ๐
๐ 1๏ธโฃ โ). |
|||
|
|||
!!! note |
|||
๐ ๐ซ ๐ฏ ๐ช ๐ฎ ๐ โ ๐ ๐ฅ. |
|||
|
|||
๐ฅ ๐จ โซ๏ธ ๐ฅ ๐ฆ โ **FastAPI** ๐ต โ ๐ฃ ๐ ๐. |
|||
|
|||
```Python hl_lines="4 139 166" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
!!! info "๐ก โน" |
|||
`Security` ๐ค ๐ฟ `Depends`, & โซ๏ธ โ๏ธ 1๏ธโฃ โ ๐ข ๐ ๐ฅ ๐ ๐ โช. |
|||
|
|||
โ๏ธ โ๏ธ `Security` โฉ๏ธ `Depends`, **FastAPI** ๐ ๐ญ ๐ โซ๏ธ ๐ช ๐ฃ ๐โโ โ, โ๏ธ ๐ซ ๐, & ๐ ๐ ๏ธ โฎ๏ธ ๐. |
|||
|
|||
โ๏ธ ๐โ ๐ ๐ `Query`, `Path`, `Depends`, `Security` & ๐ โช๏ธโก๏ธ `fastapi`, ๐ ๐ค ๐ข ๐ ๐จ ๐ ๐. |
|||
|
|||
## โ๏ธ `SecurityScopes` |
|||
|
|||
๐ โน ๐ `get_current_user`. |
|||
|
|||
๐ 1๏ธโฃ โ๏ธ ๐ ๐. |
|||
|
|||
๐ฅ ๐ฅ โ๏ธ ๐ Oauth2๏ธโฃ โ ๐ฅ โ โญ, ๐ฃ โซ๏ธ ๐: `oauth2_scheme`. |
|||
|
|||
โฉ๏ธ ๐ ๐ ๐ข ๐ซ โ๏ธ ๐ โ ๐ โซ๏ธ, ๐ฅ ๐ช โ๏ธ `Depends` โฎ๏ธ `oauth2_scheme`, ๐ฅ ๐ซ โ๏ธ โ๏ธ `Security` ๐โ ๐ฅ ๐ซ ๐ช โ ๐โโ โ. |
|||
|
|||
๐ฅ ๐ฃ ๐ ๐ข ๐ `SecurityScopes`, ๐ โช๏ธโก๏ธ `fastapi.security`. |
|||
|
|||
๐ `SecurityScopes` ๐ ๐ `Request` (`Request` โ๏ธ ๐ค ๐จ ๐ ๐). |
|||
|
|||
```Python hl_lines="8 105" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
## โ๏ธ `scopes` |
|||
|
|||
๐ข `security_scopes` ๐ ๐ `SecurityScopes`. |
|||
|
|||
โซ๏ธ ๐ โ๏ธ ๐ `scopes` โฎ๏ธ ๐ โ ๐ โ โ โซ๏ธ & ๐ ๐ ๐ โ๏ธ ๐ ๐ง-๐. ๐ โ, ๐ "โ๏ธ"... ๐ ๐ช ๐ ๐จ, โซ๏ธ ๐ฌ ๐ โช ๐. |
|||
|
|||
`security_scopes` ๐ (๐ `SecurityScopes`) ๐ `scope_str` ๐ข โฎ๏ธ ๐ ๐ป, ๐ ๐ โ ๐ฝ ๐ (๐ฅ ๐ โ๏ธ โซ๏ธ). |
|||
|
|||
๐ฅ โ `HTTPException` ๐ ๐ฅ ๐ช ๐ค-โ๏ธ (`raise`) โช ๐ โ. |
|||
|
|||
๐ โ , ๐ฅ ๐ โ ๐ (๐ฅ ๐) ๐ป ๐ฝ ๐ (โ๏ธ `scope_str`). ๐ฅ ๐ฎ ๐ ๐ป โ โ `WWW-Authenticate` ๐ (๐ ๐ ๐). |
|||
|
|||
```Python hl_lines="105 107-115" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
## โ `username` & ๐ฝ ๐ |
|||
|
|||
๐ฅ โ ๐ ๐ฅ ๐ค `username`, & โ โ. |
|||
|
|||
& โคด๏ธ ๐ฅ โ ๐ ๐ โฎ๏ธ Pydantic ๐ท (โ `ValidationError` โ ), & ๐ฅ ๐ฅ ๐ค โ ๐ ๐ฅ ๐ค โ๏ธ โ ๐ โฎ๏ธ Pydantic, ๐ฅ ๐ค `HTTPException` ๐ฅ โ โญ. |
|||
|
|||
๐, ๐ฅ โน Pydantic ๐ท `TokenData` โฎ๏ธ ๐ ๐ `scopes`. |
|||
|
|||
โ ๐ โฎ๏ธ Pydantic ๐ฅ ๐ช โ ๐ญ ๐ ๐ฅ โ๏ธ, ๐ผ, โซ๏ธโ `list` `str` โฎ๏ธ โ & `str` โฎ๏ธ `username`. |
|||
|
|||
โฉ๏ธ, ๐ผ, `dict`, โ๏ธ ๐ณ ๐, โซ๏ธ ๐ช ๐ ๐ธ โ โช, โ โซ๏ธ ๐โโ โ . |
|||
|
|||
๐ฅ โ ๐ ๐ฅ โ๏ธ ๐ฉโ๐ป โฎ๏ธ ๐ ๐, & ๐ฅ ๐ซ, ๐ฅ ๐ค ๐ ๐ โ ๐ฅ โ โญ. |
|||
|
|||
```Python hl_lines="46 116-127" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
## โ `scopes` |
|||
|
|||
๐ฅ ๐ โ ๐ ๐ โ โ, ๐ ๐ & ๐ โ๏ธ (๐ *โก ๐ ๏ธ*), ๐ โ ๐ ๐ค ๐จ, โช ๐ค `HTTPException`. |
|||
|
|||
๐, ๐ฅ โ๏ธ `security_scopes.scopes`, ๐ ๐ `list` โฎ๏ธ ๐ ๐ซ โ `str`. |
|||
|
|||
```Python hl_lines="128-134" |
|||
{!../../../docs_src/security/tutorial005.py!} |
|||
``` |
|||
|
|||
## ๐ ๐ฒ & โ |
|||
|
|||
โก๏ธ ๐ ๐ ๐ ๐ ๐ฒ & โ. |
|||
|
|||
`get_current_active_user` ๐ โ๏ธ ๐ง-๐ ๐ `get_current_user`, โ `"me"` ๐ฃ `get_current_active_user` ๐ ๐ ๐ โ โ `security_scopes.scopes` ๐ถโโ๏ธ `get_current_user`. |
|||
|
|||
*โก ๐ ๏ธ* โซ๏ธ ๐ฃ โ, `"items"`, ๐ ๐ ๐ `security_scopes.scopes` ๐ถโโ๏ธ `get_current_user`. |
|||
|
|||
๐ฅ โ ๐ ๐ & โ ๐ ๐: |
|||
|
|||
* *โก ๐ ๏ธ* `read_own_items` โ๏ธ: |
|||
* โ โ `["items"]` โฎ๏ธ ๐: |
|||
* `get_current_active_user`: |
|||
* ๐ ๐ข `get_current_active_user` โ๏ธ: |
|||
* โ โ `["me"]` โฎ๏ธ ๐: |
|||
* `get_current_user`: |
|||
* ๐ ๐ข `get_current_user` โ๏ธ: |
|||
* ๐
โโ โ โ โซ๏ธ. |
|||
* ๐ โ๏ธ `oauth2_scheme`. |
|||
* `security_scopes` ๐ข ๐ `SecurityScopes`: |
|||
* ๐ `security_scopes` ๐ข โ๏ธ ๐ `scopes` โฎ๏ธ `list` โ ๐ ๐ซ โ ๐ฃ ๐,: |
|||
* `security_scopes.scopes` ๐ ๐ `["me", "items"]` *โก ๐ ๏ธ* `read_own_items`. |
|||
* `security_scopes.scopes` ๐ ๐ `["me"]` *โก ๐ ๏ธ* `read_users_me`, โฉ๏ธ โซ๏ธ ๐ฃ ๐ `get_current_active_user`. |
|||
* `security_scopes.scopes` ๐ ๐ `[]` (๐ณ) *โก ๐ ๏ธ* `read_system_status`, โฉ๏ธ โซ๏ธ ๐ซ ๐ฃ ๐ `Security` โฎ๏ธ `scopes`, & ๐ฎ ๐, `get_current_user`, ๐ซ ๐ฃ ๐ `scope` ๐ฏโโ๏ธ. |
|||
|
|||
!!! tip |
|||
โ & "๐ฑ" ๐ ๐ฅ ๐ `get_current_user` ๐ โ๏ธ ๐ ๐ `scopes` โ
๐ *โก ๐ ๏ธ*. |
|||
|
|||
๐ โ๏ธ ๐ `scopes` ๐ฃ ๐ *โก ๐ ๏ธ* & ๐ ๐ ๐ ๐ฒ ๐ ๐ฏ *โก ๐ ๏ธ*. |
|||
|
|||
## ๐ โน ๐ `SecurityScopes` |
|||
|
|||
๐ ๐ช โ๏ธ `SecurityScopes` ๐ โ, & ๐ ๐ฅ, โซ๏ธ ๐ซ โ๏ธ "๐ฑ" ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ง โ๏ธ ๐โโ โ ๐ฃ โฎ๏ธ `Security` ๐ & ๐ โ๏ธ **๐ ๐ฏ** *โก ๐ ๏ธ* & **๐ ๐ฏ** ๐ ๐ฒ. |
|||
|
|||
โฉ๏ธ `SecurityScopes` ๐ โ๏ธ ๐ โ ๐ฃ โ๏ธ, ๐ ๐ช โ๏ธ โซ๏ธ โ ๐ ๐ค โ๏ธ ๐ โ ๐จ๐ซ ๐ ๐ข, & โคด๏ธ ๐ฃ ๐ โ ๐ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
๐ซ ๐ โ
โก ๐ *โก ๐ ๏ธ*. |
|||
|
|||
## โ
โซ๏ธ |
|||
|
|||
๐ฅ ๐ ๐ ๐ ๏ธ ๐ฉบ, ๐ ๐ช ๐ & โ โ โ ๐ ๐ โ. |
|||
|
|||
<img src="/img/tutorial/security/image11.png"> |
|||
|
|||
๐ฅ ๐ ๐ซ ๐ ๐ โ, ๐ ๐ "๐", โ๏ธ ๐โ ๐ ๐ ๐ `/users/me/` โ๏ธ `/users/me/items/` ๐ ๐ ๐ค โ ๐ฌ ๐ ๐ ๐ซ โ๏ธ ๐ฅ โ. ๐ ๐ ๐ช ๐ `/status/`. |
|||
|
|||
& ๐ฅ ๐ ๐ โ `me` โ๏ธ ๐ซ โ `items`, ๐ ๐ ๐ช ๐ `/users/me/` โ๏ธ ๐ซ `/users/me/items/`. |
|||
|
|||
๐ โซ๏ธโ ๐ ๐จ ๐ฅ ๐ฅณ ๐ธ ๐ ๐ ๐ 1๏ธโฃ ๐ซ *โก ๐ ๏ธ* โฎ๏ธ ๐ค ๐ ๐ฉโ๐ป, โ๏ธ ๐ โ ๐ โ ๐ฉโ๐ป ๐ค ๐ธ. |
|||
|
|||
## ๐ ๐ฅ ๐ฅณ ๐ ๏ธ |
|||
|
|||
๐ ๐ผ ๐ฅ โ๏ธ Oauth2๏ธโฃ "๐" ๐ง. |
|||
|
|||
๐ โ ๐โ ๐ฅ ๐จ ๐ ๐ ๐ธ, ๐ฒ โฎ๏ธ ๐ ๐ ๐ธ. |
|||
|
|||
โฉ๏ธ ๐ฅ ๐ช ๐ โซ๏ธ ๐จ `username` & `password`, ๐ฅ ๐ โซ๏ธ. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ Oauth2๏ธโฃ ๐ธ ๐ ๐ ๐ ๐ (โก, ๐ฅ ๐ ๐ ๐ค ๐โ๐ฆบ ๐ ๐ฑ๐, ๐บ๐ธ๐, ๐, โ๏ธ.) ๐ ๐ โ๏ธ 1๏ธโฃ ๐ ๐ง. |
|||
|
|||
๐
โ ๐ ๐ง. |
|||
|
|||
๐ ๐ ๐ ๐ง, โ๏ธ ๐ ๐ ๐ ๏ธ โซ๏ธ ๐ ๐
๐ถ. โซ๏ธ ๐
๐, ๐ ๐โ๐ฆบ ๐ ๐ โ ๐ ๐ง. |
|||
|
|||
!!! note |
|||
โซ๏ธ โ ๐ ๐ ๐ค ๐โ๐ฆบ ๐ ๐ซ ๐ง ๐ ๐, โ โซ๏ธ ๐ ๐ซ ๐ท. |
|||
|
|||
โ๏ธ ๐, ๐ซ ๐ ๏ธ ๐ Oauth2๏ธโฃ ๐ฉ. |
|||
|
|||
**FastAPI** ๐ ๐ ๐ ๐ซ Oauth2๏ธโฃ ๐ค ๐ง `fastapi.security.oauth2`. |
|||
|
|||
## `Security` ๐จโ๐จ `dependencies` |
|||
|
|||
๐ ๐ ๐ ๐ช ๐ฌ `list` `Depends` ๐จโ๐จ `dependencies` ๐ข (๐ฌ [๐ โก ๐ ๏ธ ๐จโ๐จ](../../tutorial/dependencies/dependencies-in-path-operation-decorators.md){.internal-link target=_blank}), ๐ ๐ช โ๏ธ `Security` โฎ๏ธ `scopes` ๐ค. |
@ -0,0 +1,382 @@ |
|||
# โ & ๐ ๐ข |
|||
|
|||
๐ ๐ผ ๐ ๐ธ ๐ช ๐ช ๐ข โ โ๏ธ ๐ณ, ๐ผ ใ ๐, ๐ฝ ๐, ๐ ๐ง ๐โ๐ฆบ, โ๏ธ. |
|||
|
|||
๐ ๐ซ โ ๐ข (๐ช ๐), ๐ ๐ฝ ๐. & ๐ ๐ช ๐ฟ, ๐ ใ. |
|||
|
|||
๐ ๐ค โซ๏ธ โ ๐ ๐ซ ๐ ๐ข ๐ โ ๐ธ. |
|||
|
|||
## ๐ ๐ข |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ โช ๐ญ โซ๏ธโ "๐ ๐ข" & โ โ๏ธ ๐ซ, ๐ญ ๐ ๐ถ โญ ๐ ๐. |
|||
|
|||
<a href="https://en.wikipedia.org/wiki/Environment_variable" class="external-link" target="_blank">๐ ๐ข</a> (๐ญ "๐จ๐ป {") ๐ข ๐ ๐ ๐ ๐ ๐, ๐โโ โ๏ธ, & ๐ช โ ๐ ๐ ๐ (โ๏ธ ๐ ๐ ๐). |
|||
|
|||
๐ ๐ช โ & โ๏ธ ๐ ๐ข ๐, ๐ต ๐โโ ๐: |
|||
|
|||
=== "๐พ, ๐ธ๐ป, ๐ช ๐" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// You could create an env var MY_NAME with |
|||
$ export MY_NAME="Wade Wilson" |
|||
|
|||
// Then you could use it with other programs, like |
|||
$ echo "Hello $MY_NAME" |
|||
|
|||
Hello Wade Wilson |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
=== "๐ช ๐" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// Create an env var MY_NAME |
|||
$ $Env:MY_NAME = "Wade Wilson" |
|||
|
|||
// Use it with other programs, like |
|||
$ echo "Hello $Env:MY_NAME" |
|||
|
|||
Hello Wade Wilson |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
### โ ๐จ๐ป {๐ |
|||
|
|||
๐ ๐ช โ ๐ ๐ข ๐ ๐, ๐ถ (โ๏ธ โฎ๏ธ ๐ ๐ ๐ฉโ๐ฌ), & โคด๏ธ โ ๐ซ ๐. |
|||
|
|||
๐ผ ๐ ๐ช โ๏ธ ๐ `main.py` โฎ๏ธ: |
|||
|
|||
```Python hl_lines="3" |
|||
import os |
|||
|
|||
name = os.getenv("MY_NAME", "World") |
|||
print(f"Hello {name} from Python") |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ โ <a href="https://docs.python.org/3.8/library/os.html#os.getenv" class="external-link" target="_blank">`os.getenv()`</a> ๐ข ๐ฒ ๐จ. |
|||
|
|||
๐ฅ ๐ซ ๐, โซ๏ธ `None` ๐ข, ๐ฅ ๐ฅ ๐ `"World"` ๐ข ๐ฒ โ๏ธ. |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ค ๐ ๐ ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// Here we don't set the env var yet |
|||
$ python main.py |
|||
|
|||
// As we didn't set the env var, we get the default value |
|||
|
|||
Hello World from Python |
|||
|
|||
// But if we create an environment variable first |
|||
$ export MY_NAME="Wade Wilson" |
|||
|
|||
// And then call the program again |
|||
$ python main.py |
|||
|
|||
// Now it can read the environment variable |
|||
|
|||
Hello Wade Wilson from Python |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ข ๐ช โ ๐ ๐, โ๏ธ ๐ช โ ๐, & ๐ซ โ๏ธ ๐ช (๐ `git`) โฎ๏ธ ๐ ๐, โซ๏ธ โ โ๏ธ ๐ซ ๐ณ โ๏ธ โ. |
|||
|
|||
๐ ๐ช โ ๐ ๐ข ๐ด ๐ฏ ๐ ๐ผ, ๐ ๐ด ๐ช ๐ ๐, & ๐ด ๐ฎ ๐. |
|||
|
|||
๐, โ โซ๏ธ โถ๏ธ๏ธ โญ ๐ โซ๏ธ, ๐ ๐ โธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// Create an env var MY_NAME in line for this program call |
|||
$ MY_NAME="Wade Wilson" python main.py |
|||
|
|||
// Now it can read the environment variable |
|||
|
|||
Hello Wade Wilson from Python |
|||
|
|||
// The env var no longer exists afterwards |
|||
$ python main.py |
|||
|
|||
Hello World from Python |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
!!! tip |
|||
๐ ๐ช โ ๐
๐ โซ๏ธ <a href="https://12factor.net/config" class="external-link" target="_blank">1๏ธโฃ2๏ธโฃ-โ ๐ฑ: ๐</a>. |
|||
|
|||
### ๐ & ๐ฌ |
|||
|
|||
๐ซ ๐ ๐ข ๐ช ๐ด ๐ต โ ๐ป, ๐ซ ๐ข ๐ & โ๏ธ ๐ โฎ๏ธ ๐ ๐ & ๐ โ๏ธ (& โฎ๏ธ ๐ ๐โโ โ๏ธ, ๐พ, ๐ช, ๐ธ๐ป). |
|||
|
|||
๐ โ ๐ ๐ ๐ฒ โ ๐ โช๏ธโก๏ธ ๐ ๐ข ๐ `str`, & ๐ ๐ ๏ธ ๐ ๐ โ๏ธ ๐ฌ โ๏ธ ๐จ ๐. |
|||
|
|||
## Pydantic `Settings` |
|||
|
|||
๐, Pydantic ๐ ๐ ๐ ๐ต ๐ซ โ ๐ โช๏ธโก๏ธ ๐ ๐ข โฎ๏ธ <a href="https://pydantic-docs.helpmanual.io/usage/settings/" class="external-link" target="_blank">Pydantic: โ ๐งพ</a>. |
|||
|
|||
### โ `Settings` ๐ |
|||
|
|||
๐ `BaseSettings` โช๏ธโก๏ธ Pydantic & โ ๐ง-๐, ๐ถ ๐
๐ โฎ๏ธ Pydantic ๐ท. |
|||
|
|||
๐ ๐ โฎ๏ธ Pydantic ๐ท, ๐ ๐ฃ ๐ ๐ข โฎ๏ธ ๐ โ, & ๐ฒ ๐ข ๐ฒ. |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ ๐ฌ โ & ๐งฐ ๐ โ๏ธ Pydantic ๐ท, ๐ ๐ ๐ ๐ & ๐ ๐ฌ โฎ๏ธ `Field()`. |
|||
|
|||
```Python hl_lines="2 5-8 11" |
|||
{!../../../docs_src/settings/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ ๐ณ โฉ ๐ & ๐, ๐ซ โ๏ธ ๐ ๐ผ, โ๏ธ ๐ 1๏ธโฃ ๐. |
|||
|
|||
โคด๏ธ, ๐โ ๐ โ ๐ ๐ `Settings` ๐ (๐ ๐ผ, `settings` ๐), Pydantic ๐ โ ๐ ๐ข ๐ผ-๐ ๐,, โ-๐ผ ๐ข `APP_NAME` ๐ โ ๐ข `app_name`. |
|||
|
|||
โญ โซ๏ธ ๐ ๐ & โ ๐ฝ. , ๐โ ๐ โ๏ธ ๐ `settings` ๐, ๐ ๐ โ๏ธ ๐ ๐ ๐ ๐ฃ (โ
`items_per_user` ๐ `int`). |
|||
|
|||
### โ๏ธ `settings` |
|||
|
|||
โคด๏ธ ๐ ๐ช โ๏ธ ๐ `settings` ๐ ๐ ๐ธ: |
|||
|
|||
```Python hl_lines="18-20" |
|||
{!../../../docs_src/settings/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ฝ |
|||
|
|||
โญ, ๐ ๐ ๐ ๐ฝ ๐ถโโ๏ธ ๐ณ ๐ ๐ข, ๐ผ ๐ ๐ช โ `ADMIN_EMAIL` & `APP_NAME` โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ ADMIN_EMAIL="[email protected]" APP_NAME="ChimichangApp" uvicorn main:app |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
!!! tip |
|||
โ ๐ ๐จ๐ป {๐ ๐ ๐ ๐ซ โฎ๏ธ ๐, & ๐ฎ ๐ซ ๐ โญ ๐. |
|||
|
|||
& โคด๏ธ `admin_email` โ ๐ โ `"[email protected]"`. |
|||
|
|||
`app_name` ๐ `"ChimichangApp"`. |
|||
|
|||
& `items_per_user` ๐ ๐ง ๐ฎ ๐ข ๐ฒ `50`. |
|||
|
|||
## โ โ1๏ธโฃ ๐น |
|||
|
|||
๐ ๐ช ๐ฎ ๐ โ โ1๏ธโฃ ๐น ๐ ๐ ๐ [๐ฆ ๐ธ - ๐ ๐](../tutorial/bigger-applications.md){.internal-link target=_blank}. |
|||
|
|||
๐ผ, ๐ ๐ช โ๏ธ ๐ `config.py` โฎ๏ธ: |
|||
|
|||
```Python |
|||
{!../../../docs_src/settings/app01/config.py!} |
|||
``` |
|||
|
|||
& โคด๏ธ โ๏ธ โซ๏ธ ๐ `main.py`: |
|||
|
|||
```Python hl_lines="3 11-13" |
|||
{!../../../docs_src/settings/app01/main.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ช ๐ `__init__.py` ๐ ๐ ๐ [๐ฆ ๐ธ - ๐ ๐](../tutorial/bigger-applications.md){.internal-link target=_blank}. |
|||
|
|||
## โ ๐ |
|||
|
|||
๐พ โซ๏ธ 5๏ธโฃ๐ โ ๐ โ โช๏ธโก๏ธ ๐, โฉ๏ธ โ๏ธ ๐ ๐ โฎ๏ธ `settings` ๐ โ๏ธ ๐. |
|||
|
|||
๐ ๐ช โด๏ธ โ โฎ๏ธ ๐ฌ, โซ๏ธ ๐ถ โฉ ๐ ๐ โฎ๏ธ ๐ ๐ ๐ โ. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ โช๏ธโก๏ธ โฎ๏ธ ๐ผ, ๐ `config.py` ๐ ๐ช ๐ ๐: |
|||
|
|||
```Python hl_lines="10" |
|||
{!../../../docs_src/settings/app02/config.py!} |
|||
``` |
|||
|
|||
๐ ๐ ๐ ๐ฅ ๐ซ โ ๐ข ๐ `settings = Settings()`. |
|||
|
|||
### ๐ ๐ฑ ๐ |
|||
|
|||
๐ ๐ฅ โ ๐ ๐ ๐จ ๐ `config.Settings()`. |
|||
|
|||
```Python hl_lines="5 11-12" |
|||
{!../../../docs_src/settings/app02/main.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ฌ `@lru_cache()` ๐. |
|||
|
|||
๐ ๐ ๐ช ๐ค `get_settings()` ๐ ๐ข. |
|||
|
|||
& โคด๏ธ ๐ฅ ๐ช ๐ โซ๏ธ โช๏ธโก๏ธ *โก ๐ ๏ธ ๐ข* ๐ & โ๏ธ โซ๏ธ ๐ ๐ฅ ๐ช โซ๏ธ. |
|||
|
|||
```Python hl_lines="16 18-20" |
|||
{!../../../docs_src/settings/app02/main.py!} |
|||
``` |
|||
|
|||
### โ & ๐ฌ |
|||
|
|||
โคด๏ธ โซ๏ธ ๐ ๐ถ โฉ ๐ ๐ โ ๐ โฎ๏ธ ๐ฌ ๐ ๐ ๐ `get_settings`: |
|||
|
|||
```Python hl_lines="9-10 13 21" |
|||
{!../../../docs_src/settings/app02/test_main.py!} |
|||
``` |
|||
|
|||
๐ ๐ ๐ฅ โ ๐ ๐ฒ `admin_email` ๐โ ๐ ๐ `Settings` ๐, & โคด๏ธ ๐ฅ ๐จ ๐ ๐ ๐. |
|||
|
|||
โคด๏ธ ๐ฅ ๐ช ๐ฏ ๐ โซ๏ธ โ๏ธ. |
|||
|
|||
## ๐ `.env` ๐ |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ โ ๐ ๐ฒ ๐ ๐, ๐ฒ ๐ ๐, โซ๏ธ 5๏ธโฃ๐ โ ๐ฎ ๐ซ ๐ ๐ & โคด๏ธ โ ๐ซ โช๏ธโก๏ธ โซ๏ธ ๐ฅ ๐ซ ๐ ๐ข. |
|||
|
|||
๐ ๐ก โ ๐ฅ ๐ โซ๏ธ โ๏ธ ๐, ๐ซ ๐ ๐ข ๐ ๐ฅ ๐ `.env`, & ๐ ๐ค "๐จ๐ป". |
|||
|
|||
!!! tip |
|||
๐ โถ๏ธ โฎ๏ธ โฃ (`.`) ๐ตโโ ๐ ๐ฅ-๐ โ๏ธ, ๐ ๐พ & ๐ธ๐ป. |
|||
|
|||
โ๏ธ ๐จ๐ป ๐ ๐ซ ๐ค โ๏ธ โ๏ธ ๐ โ ๐. |
|||
|
|||
Pydantic โ๏ธ ๐โ๐ฆบ ๐ โช๏ธโก๏ธ ๐ ๐ ๐ โ๏ธ ๐ข ๐. ๐ ๐ช โ ๐ <a href="https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support" class="external-link" target="_blank">Pydantic โ: ๐จ๐ป (.๐จ๐ป) ๐โ๐ฆบ</a>. |
|||
|
|||
!!! tip |
|||
๐ ๐ท, ๐ ๐ช `pip install python-dotenv`. |
|||
|
|||
### `.env` ๐ |
|||
|
|||
๐ ๐ช โ๏ธ `.env` ๐ โฎ๏ธ: |
|||
|
|||
```bash |
|||
ADMIN_EMAIL="[email protected]" |
|||
APP_NAME="ChimichangApp" |
|||
``` |
|||
|
|||
### โ โ โช๏ธโก๏ธ `.env` |
|||
|
|||
& โคด๏ธ โน ๐ `config.py` โฎ๏ธ: |
|||
|
|||
```Python hl_lines="9-10" |
|||
{!../../../docs_src/settings/app03/config.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ฅ โ ๐ `Config` ๐ ๐ Pydantic `Settings` ๐, & โ `env_file` ๐ โฎ๏ธ ๐จ๐ป ๐ ๐ฅ ๐ โ๏ธ. |
|||
|
|||
!!! tip |
|||
`Config` ๐ โ๏ธ Pydantic ๐ณ. ๐ ๐ช โ ๐ <a href="https://pydantic-docs.helpmanual.io/usage/model_config/" class="external-link" target="_blank">Pydantic ๐ท ๐</a> |
|||
|
|||
### ๐ `Settings` ๐ด ๐ โฎ๏ธ `lru_cache` |
|||
|
|||
๐ ๐ โช๏ธโก๏ธ ๐พ ๐ โ (๐) ๐ ๏ธ, ๐ ๐ฒ ๐ โซ๏ธ ๐ด ๐ & โคด๏ธ ๐ค-โ๏ธ ๐ โ ๐, โฉ๏ธ ๐ โซ๏ธ ๐ ๐จ. |
|||
|
|||
โ๏ธ ๐ ๐ฐ ๐ฅ: |
|||
|
|||
```Python |
|||
Settings() |
|||
``` |
|||
|
|||
๐ `Settings` ๐ ๐ โ, & ๐ โซ๏ธ ๐ โ `.env` ๐ ๐. |
|||
|
|||
๐ฅ ๐ ๐ข ๐: |
|||
|
|||
```Python |
|||
def get_settings(): |
|||
return Settings() |
|||
``` |
|||
|
|||
๐ฅ ๐ โ ๐ ๐ ๐ ๐จ, & ๐ฅ ๐ ๐ `.env` ๐ ๐ ๐จ. ๐ถ ๐ถ |
|||
|
|||
โ๏ธ ๐ฅ โ๏ธ `@lru_cache()` ๐จโ๐จ ๐ ๐, `Settings` ๐ ๐ โ ๐ด ๐, ๐ฅ ๐ฐ โซ๏ธ ๐ค. ๐ถ ๐ถ |
|||
|
|||
```Python hl_lines="1 10" |
|||
{!../../../docs_src/settings/app03/main.py!} |
|||
``` |
|||
|
|||
โคด๏ธ ๐ ๐ ๐ค `get_settings()` ๐ โญ ๐จ, โฉ๏ธ ๐ ๏ธ ๐ ๐ `get_settings()` & ๐ ๐ `Settings` ๐, โซ๏ธ ๐ ๐จ ๐ ๐ ๐ ๐จ ๐ ๐ฅ ๐ค, ๐ & ๐. |
|||
|
|||
#### `lru_cache` ๐ก โน |
|||
|
|||
`@lru_cache()` ๐ ๐ข โซ๏ธ ๐ ๐จ ๐ ๐ฒ ๐ ๐จ ๐ฅ ๐ฐ, โฉ๏ธ ๐ป โซ๏ธ ๐, ๐ ๏ธ ๐ ๐ข ๐ ๐ฐ. |
|||
|
|||
, ๐ข ๐ โซ๏ธ ๐ ๐ ๏ธ ๐ ๐ ๐ โ. & โคด๏ธ ๐ฒ ๐จ ๐ ๐ ๐ โ ๐ โ๏ธ ๐ & ๐ ๐โ ๐ข ๐ค โฎ๏ธ โซ๏ธโ ๐ ๐ โ. |
|||
|
|||
๐ผ, ๐ฅ ๐ โ๏ธ ๐ข: |
|||
|
|||
```Python |
|||
@lru_cache() |
|||
def say_hi(name: str, salutation: str = "Ms."): |
|||
return f"Hello {salutation} {name}" |
|||
``` |
|||
|
|||
๐ ๐ ๐ช ๐ ๏ธ ๐ ๐: |
|||
|
|||
```mermaid |
|||
sequenceDiagram |
|||
|
|||
participant code as Code |
|||
participant function as say_hi() |
|||
participant execute as Execute function |
|||
|
|||
rect rgba(0, 255, 0, .1) |
|||
code ->> function: say_hi(name="Camila") |
|||
function ->> execute: execute function code |
|||
execute ->> code: return the result |
|||
end |
|||
|
|||
rect rgba(0, 255, 255, .1) |
|||
code ->> function: say_hi(name="Camila") |
|||
function ->> code: return stored result |
|||
end |
|||
|
|||
rect rgba(0, 255, 0, .1) |
|||
code ->> function: say_hi(name="Rick") |
|||
function ->> execute: execute function code |
|||
execute ->> code: return the result |
|||
end |
|||
|
|||
rect rgba(0, 255, 0, .1) |
|||
code ->> function: say_hi(name="Rick", salutation="Mr.") |
|||
function ->> execute: execute function code |
|||
execute ->> code: return the result |
|||
end |
|||
|
|||
rect rgba(0, 255, 255, .1) |
|||
code ->> function: say_hi(name="Rick") |
|||
function ->> code: return stored result |
|||
end |
|||
|
|||
rect rgba(0, 255, 255, .1) |
|||
code ->> function: say_hi(name="Camila") |
|||
function ->> code: return stored result |
|||
end |
|||
``` |
|||
|
|||
๐ผ ๐ ๐ `get_settings()`, ๐ข ๐ซ โ ๐ โ, โซ๏ธ ๐ง ๐จ ๐ ๐ฒ. |
|||
|
|||
๐ ๐, โซ๏ธ ๐ญ ๐ ๐ฅ โซ๏ธ ๐ ๐ข. โ๏ธ โซ๏ธ โ๏ธ ๐ ๐ข, โคด๏ธ ๐ฅ ๐ช ๐ โซ๏ธ ๐ช ๐ฌ. |
|||
|
|||
`@lru_cache()` ๐ `functools` โ ๐ ๐ ๐ฉ ๐, ๐ ๐ช โ ๐
๐ โซ๏ธ <a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">๐ ๐ฉบ `@lru_cache()`</a>. |
|||
|
|||
## ๐ |
|||
|
|||
๐ ๐ช โ๏ธ Pydantic โ ๐ต โ โ๏ธ ๐ณ ๐ ๐ธ, โฎ๏ธ ๐ ๐๏ธ Pydantic ๐ท. |
|||
|
|||
* โ๏ธ ๐ ๐ ๐ช ๐ ๐ฌ. |
|||
* ๐ ๐ช โ๏ธ `.env` ๐ โฎ๏ธ โซ๏ธ. |
|||
* โ๏ธ `@lru_cache()` โก๏ธ ๐ โ ๐ ๐จ๐ป ๐ ๐ & ๐ ๐ ๐จ, โช ๐ค ๐ ๐ โซ๏ธ โฎ๏ธ ๐ฌ. |
@ -0,0 +1,73 @@ |
|||
# ๐ง ๐ธ - ๐ป |
|||
|
|||
๐ฅ ๐ ๐ช โ๏ธ 2๏ธโฃ ๐ฌ FastAPI ๐ธ, โฎ๏ธ ๐ซ ๐ ๐ฌ ๐ & ๐ซ ๐ ๐ฉบ โ, ๐ ๐ช โ๏ธ ๐ ๐ฑ & "๐ป" 1๏ธโฃ (โ๏ธ ๐
) ๐ง-๐ธ(โ). |
|||
|
|||
## ๐ **FastAPI** ๐ธ |
|||
|
|||
"๐" โ โ ๐ "๐ฌ" ๐ธ ๐ฏ โก, ๐ โคด๏ธ โ ๐
๐ ๐ ๐ฝ ๐ โก, โฎ๏ธ _โก ๐ ๏ธ_ ๐ฃ ๐ ๐ง-๐ธ. |
|||
|
|||
### ๐-๐ ๐ธ |
|||
|
|||
๐ฅ, โ ๐, ๐-๐, **FastAPI** ๐ธ, & ๐ฎ *โก ๐ ๏ธ*: |
|||
|
|||
```Python hl_lines="3 6-8" |
|||
{!../../../docs_src/sub_applications/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐ง-๐ธ |
|||
|
|||
โคด๏ธ, โ ๐ ๐ง-๐ธ, & ๐ฎ *โก ๐ ๏ธ*. |
|||
|
|||
๐ ๐ง-๐ธ โ1๏ธโฃ ๐ฉ FastAPI ๐ธ, โ๏ธ ๐ 1๏ธโฃ ๐ ๐ "๐ป": |
|||
|
|||
```Python hl_lines="11 14-16" |
|||
{!../../../docs_src/sub_applications/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐ป ๐ง-๐ธ |
|||
|
|||
๐ ๐-๐ ๐ธ, `app`, ๐ป ๐ง-๐ธ, `subapi`. |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ ๐ โก `/subapi`: |
|||
|
|||
```Python hl_lines="11 19" |
|||
{!../../../docs_src/sub_applications/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ
๐ง ๐ ๏ธ ๐ฉบ |
|||
|
|||
๐, ๐ `uvicorn` โฎ๏ธ ๐ ๐ฑ, ๐ฅ ๐ ๐ `main.py`, โซ๏ธ ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --reload |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
& ๐ ๐ฉบ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
๐ ๐ ๐ ๐ง ๐ ๏ธ ๐ฉบ ๐ ๐ฑ, ๐ ๐ด ๐ฎ ๐ _โก ๐ ๏ธ_: |
|||
|
|||
<img src="/img/tutorial/sub-applications/image01.png"> |
|||
|
|||
& โคด๏ธ, ๐ ๐ฉบ ๐ง-๐ธ, <a href="http://127.0.0.1:8000/subapi/docs" class="external-link" target="_blank">http://127.0.0.1:8000/subapi/docs</a>. |
|||
|
|||
๐ ๐ ๐ ๐ง ๐ ๏ธ ๐ฉบ ๐ง-๐ธ, โ
๐ด ๐ฎ ๐ _โก ๐ ๏ธ_, ๐ ๐ฝ โ ๐ง-โก ๐ก `/subapi`: |
|||
|
|||
<img src="/img/tutorial/sub-applications/image02.png"> |
|||
|
|||
๐ฅ ๐ ๐ ๐ โฎ๏ธ ๐ 2๏ธโฃ ๐ฉโ๐ป ๐ข, ๐ซ ๐ ๐ท โ, โฉ๏ธ ๐ฅ ๐ ๐ช ๐ฌ ๐ ๐ฏ ๐ฑ โ๏ธ ๐ง-๐ฑ. |
|||
|
|||
### ๐ก โน: `root_path` |
|||
|
|||
๐โ ๐ ๐ป ๐ง-๐ธ ๐ฌ ๐, FastAPI ๐ โ ๐
๐ ๐ป โก ๐ง-๐ธ โ๏ธ ๐ ๏ธ โช๏ธโก๏ธ ๐ซ ๐ง ๐ค `root_path`. |
|||
|
|||
๐ ๐, ๐ง-๐ธ ๐ ๐ญ โ๏ธ ๐ โก ๐ก ๐ฉบ ๐. |
|||
|
|||
& ๐ง-๐ธ ๐ช โ๏ธ ๐ฎ ๐ ๐ ๐ง-๐ธ & ๐ ๐ ๐ท โ, โฉ๏ธ FastAPI ๐ต ๐ ๐ `root_path`โ ๐. |
|||
|
|||
๐ ๐ ๐ก ๐
๐ `root_path` & โ โ๏ธ โซ๏ธ ๐ฏ ๐ ๐ [โ
๐ณ](./behind-a-proxy.md){.internal-link target=_blank}. |
@ -0,0 +1,77 @@ |
|||
# ๐ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ ๐ ๐ ๐ โฎ๏ธ **FastAPI**. |
|||
|
|||
โ โ Jinja2๏ธโฃ, ๐ 1๏ธโฃ โ๏ธ ๐บ & ๐ ๐งฐ. |
|||
|
|||
๐ค ๐ ๐ โซ๏ธ ๐ช ๐ ๐ ๐ช โ๏ธ ๐ ๐ **FastAPI** ๐ธ (๐ ๐). |
|||
|
|||
## โ ๐ |
|||
|
|||
โ `jinja2`: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install jinja2 |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
## โ๏ธ `Jinja2Templates` |
|||
|
|||
* ๐ `Jinja2Templates`. |
|||
* โ `templates` ๐ ๐ ๐ ๐ช ๐ค-โ๏ธ โช. |
|||
* ๐ฃ `Request` ๐ข *โก ๐ ๏ธ* ๐ ๐ ๐จ ๐. |
|||
* โ๏ธ `templates` ๐ โ โ & ๐จ `TemplateResponse`, ๐ถโโ๏ธ `request` 1๏ธโฃ ๐-๐ฒ ๐ซ Jinja2๏ธโฃ "๐". |
|||
|
|||
```Python hl_lines="4 11 15-16" |
|||
{!../../../docs_src/templates/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! note |
|||
๐ ๐ ๐ โ๏ธ ๐ถโโ๏ธ `request` ๐ ๐-๐ฒ ๐ซ ๐ Jinja2๏ธโฃ. , ๐ โ๏ธ ๐ฃ โซ๏ธ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
!!! tip |
|||
๐ฃ `response_class=HTMLResponse` ๐ฉบ ๐ ๐ ๐ช ๐ญ ๐ ๐จ ๐ ๐ธ. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.templating import Jinja2Templates`. |
|||
|
|||
**FastAPI** ๐ ๐ `starlette.templating` `fastapi.templating` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐. ๐ โฎ๏ธ `Request` & `StaticFiles`. |
|||
|
|||
## โ ๐ |
|||
|
|||
โคด๏ธ ๐ ๐ช โ ๐ `templates/item.html` โฎ๏ธ: |
|||
|
|||
```jinja hl_lines="7" |
|||
{!../../../docs_src/templates/templates/item.html!} |
|||
``` |
|||
|
|||
โซ๏ธ ๐ ๐ฆ `id` โ โช๏ธโก๏ธ "๐" `dict` ๐ ๐ถโโ๏ธ: |
|||
|
|||
```Python |
|||
{"request": request, "id": id} |
|||
``` |
|||
|
|||
## ๐ & ๐ป ๐ |
|||
|
|||
& ๐ ๐ช โ๏ธ `url_for()` ๐ ๐, & โ๏ธ โซ๏ธ, ๐ผ, โฎ๏ธ `StaticFiles` ๐ ๐. |
|||
|
|||
```jinja hl_lines="4" |
|||
{!../../../docs_src/templates/templates/item.html!} |
|||
``` |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ ๐ ๐ ๐ `static/styles.css` โฎ๏ธ: |
|||
|
|||
```CSS hl_lines="4" |
|||
{!../../../docs_src/templates/static/styles.css!} |
|||
``` |
|||
|
|||
& โฉ๏ธ ๐ โ๏ธ `StaticFiles`, ๐ ๐ ๐ ๐ ๐ฆ ๐ ๐ **FastAPI** ๐ธ ๐ `/static/styles.css`. |
|||
|
|||
## ๐
โน |
|||
|
|||
๐
โน, ๐ โ ๐ฏ ๐, โ
<a href="https://www.starlette.io/templates/" class="external-link" target="_blank">๐ ๐ฉบ ๐ ๐</a>. |
@ -0,0 +1,95 @@ |
|||
# ๐ฌ ๐ฝ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ ๐ โช๏ธโก๏ธ [๐ฌ ๐ โฎ๏ธ ๐](testing-dependencies.md){.internal-link target=_blank} ๐ ๐ฝ ๐ฌ. |
|||
|
|||
๐ ๐ช ๐ โ ๐ ๐ ๐ฝ ๐ฌ, ๐พ ๐ฝ โฎ๏ธ ๐ฏ, ๐ค-๐ฅง โซ๏ธ โฎ๏ธ ๐ฌ ๐ฝ, โ๏ธ. |
|||
|
|||
๐ ๐ญ โซ๏ธโ ๐ ๐ ๐ ๐ โฎ๏ธ ๐. |
|||
|
|||
## ๐ฎ ๐ฏ ๐ ๐ฑ |
|||
|
|||
โก๏ธ โน ๐ผ โช๏ธโก๏ธ [๐ (๐) ๐ฝ](../tutorial/sql-databases.md){.internal-link target=_blank} โ๏ธ ๐ฌ ๐ฝ. |
|||
|
|||
๐ ๐ฑ ๐ ๐, ๐ ๐ช ๐ถ ๐ ๐ ๐ โ
โ โซ๏ธ. |
|||
|
|||
๐ด ๐ ๐ฅ ๐ ๐ฌ ๐. |
|||
|
|||
๐ ๐ ๐ `get_db()` ๐ ๐จ ๐ฝ ๐. |
|||
|
|||
๐ฏ, ๐ ๐ช โ๏ธ ๐ ๐ ๐จ ๐ *๐* ๐ฝ ๐ โฉ๏ธ 1๏ธโฃ ๐ ๐ โ๏ธ ๐. |
|||
|
|||
๐ ๐ผ ๐ฅ ๐ โ ๐ ๐ฝ ๐ด ๐ฏ. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ฅ โ ๐ ๐ `sql_app/tests/test_sql_app.py`. |
|||
|
|||
๐ ๐ ๐ ๐ ๐: |
|||
|
|||
``` hl_lines="9-11" |
|||
. |
|||
โโโ sql_app |
|||
โโโ __init__.py |
|||
โโโ crud.py |
|||
โโโ database.py |
|||
โโโ main.py |
|||
โโโ models.py |
|||
โโโ schemas.py |
|||
โโโ tests |
|||
โโโ __init__.py |
|||
โโโ test_sql_app.py |
|||
``` |
|||
|
|||
## โ ๐ ๐ฝ ๐ |
|||
|
|||
๐ฅ, ๐ฅ โ ๐ ๐ฝ ๐ โฎ๏ธ ๐ ๐ฝ. |
|||
|
|||
๐ฏ ๐ฅ ๐ โ๏ธ ๐ `test.db` โฉ๏ธ `sql_app.db`. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ ๐
โ๏ธ ๐ ๐, ๐ฅ ๐ โซ๏ธ. |
|||
|
|||
```Python hl_lines="8-13" |
|||
{!../../../docs_src/sql_databases/sql_app/tests/test_sql_app.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ช ๐ โ ๐ ๐ ๐ฎ โซ๏ธ ๐ข & โ๏ธ โซ๏ธ โช๏ธโก๏ธ ๐ฏโโ๏ธ `database.py` & `tests/test_sql_app.py`. |
|||
|
|||
๐ฆ & ๐ฏ ๐ ๐ฏ ๐ฌ ๐, ๐ฅ ๐จ โซ๏ธ. |
|||
|
|||
## โ ๐ฝ |
|||
|
|||
โฉ๏ธ ๐ ๐ฅ ๐ โ๏ธ ๐ ๐ฝ ๐ ๐, ๐ฅ ๐ช โ ๐ญ ๐ฅ โ ๐ฝ โฎ๏ธ: |
|||
|
|||
```Python |
|||
Base.metadata.create_all(bind=engine) |
|||
``` |
|||
|
|||
๐ ๐ ๐ค `main.py`, โ๏ธ โธ `main.py` โ๏ธ ๐ฝ ๐ `sql_app.db`, & ๐ฅ ๐ช โ ๐ญ ๐ฅ โ `test.db` ๐ฏ. |
|||
|
|||
๐ฅ ๐ฎ ๐ โธ ๐ฅ, โฎ๏ธ ๐ ๐. |
|||
|
|||
```Python hl_lines="16" |
|||
{!../../../docs_src/sql_databases/sql_app/tests/test_sql_app.py!} |
|||
``` |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ ๐ฅ โ ๐ ๐ & ๐ฎ โซ๏ธ ๐ ๐ ๐ฑ. |
|||
|
|||
```Python hl_lines="19-24 27" |
|||
{!../../../docs_src/sql_databases/sql_app/tests/test_sql_app.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ `override_get_db()` ๐ โซ๏ธโ ๐ `get_db()`, โ๏ธ `override_get_db()` ๐ฅ โ๏ธ `TestingSessionLocal` ๐ฌ ๐ฝ โฉ๏ธ. |
|||
|
|||
## ๐ฏ ๐ฑ |
|||
|
|||
โคด๏ธ ๐ฅ ๐ช ๐ฏ ๐ฑ ๐. |
|||
|
|||
```Python hl_lines="32-47" |
|||
{!../../../docs_src/sql_databases/sql_app/tests/test_sql_app.py!} |
|||
``` |
|||
|
|||
& ๐ ๐ ๏ธ ๐ฅ โ ๐ฝ โฎ๏ธ ๐ฏ ๐ `test.db` ๐ฝ โฉ๏ธ ๐ `sql_app.db`. |
@ -0,0 +1,49 @@ |
|||
# ๐ฌ ๐ โฎ๏ธ ๐ |
|||
|
|||
## ๐ ๐ โฎ๏ธ ๐ฌ |
|||
|
|||
๐ค ๐ ๐โ ๐ ๐ช ๐ ๐ ๐ โฎ๏ธ ๐ฌ. |
|||
|
|||
๐ ๐ซ ๐ โฎ๏ธ ๐ ๐ (๐ซ ๐ ๐ง-๐ โซ๏ธ ๐ช โ๏ธ). |
|||
|
|||
โฉ๏ธ, ๐ ๐ ๐ ๐ ๐ ๐ ๐ โ๏ธ ๐ด โฎ๏ธ ๐ฏ (๐ฒ ๐ด ๐ฏ ๐ฏ), & ๐ ๐ ๐ฒ ๐ ๐ช โ๏ธ ๐โ ๐ฒ โฎ๏ธ ๐ โ๏ธ. |
|||
|
|||
### โ๏ธ ๐ผ: ๐ข ๐โ๐ฆบ |
|||
|
|||
๐ผ ๐ช ๐ ๐ โ๏ธ ๐ข ๐ค ๐โ๐ฆบ ๐ ๐ ๐ช ๐ค. |
|||
|
|||
๐ ๐จ โซ๏ธ ๐ค & โซ๏ธ ๐จ ๐ ๐ฉโ๐ป. |
|||
|
|||
๐ ๐โ๐ฆบ 5๏ธโฃ๐ ๐ ๐ ๐ ๐จ, & ๐ค โซ๏ธ ๐ช โ โ ๐ฐ ๐ ๐ฅ ๐ โ๏ธ ๐ง ๐ ๐ฉโ๐ป ๐ฏ. |
|||
|
|||
๐ ๐ฒ ๐ ๐ฏ ๐ข ๐โ๐ฆบ ๐, โ๏ธ ๐ซ ๐ฏ ๐ค โซ๏ธ ๐ ๐ฏ ๐ ๐. |
|||
|
|||
๐ ๐ผ, ๐ ๐ช ๐ ๐ ๐ ๐ค ๐ ๐โ๐ฆบ, & โ๏ธ ๐ ๐ ๐ ๐จ ๐ ๐ฉโ๐ป, ๐ด ๐ ๐ฏ. |
|||
|
|||
### โ๏ธ `app.dependency_overrides` ๐ข |
|||
|
|||
๐ซ ๐ผ, ๐ **FastAPI** ๐ธ โ๏ธ ๐ข `app.dependency_overrides`, โซ๏ธ ๐
`dict`. |
|||
|
|||
๐ ๐ ๐ฌ, ๐ ๐ฎ ๐ โฎ๏ธ ๐ (๐ข), & ๐ฒ, ๐ ๐ ๐ (โ1๏ธโฃ ๐ข). |
|||
|
|||
& โคด๏ธ **FastAPI** ๐ ๐ค ๐ ๐ โฉ๏ธ โฎ๏ธ ๐. |
|||
|
|||
```Python hl_lines="28-29 32" |
|||
{!../../../docs_src/dependency_testing/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ช โ ๐ ๐ ๐ โ๏ธ ๐ ๐ **FastAPI** ๐ธ. |
|||
|
|||
โฎ๏ธ ๐ ๐ช โ๏ธ *โก ๐ ๏ธ ๐ข*, *โก ๐ ๏ธ ๐จโ๐จ* (๐โ ๐ ๐ซ โ๏ธ ๐จ ๐ฒ), `.include_router()` ๐ค, โ๏ธ. |
|||
|
|||
FastAPI ๐ ๐ช ๐ โซ๏ธ. |
|||
|
|||
โคด๏ธ ๐ ๐ช โฒ ๐ ๐ (โ ๐ซ) โ `app.dependency_overrides` ๐ `dict`: |
|||
|
|||
```Python |
|||
app.dependency_overrides = {} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ ๐ ๐ ๐ด โฎ๏ธ ๐ฏ, ๐ ๐ช โ ๐ โถ๏ธ ๐ฏ (๐ ๐ฏ ๐ข) & โฒ โซ๏ธ ๐ (๐ ๐ฏ ๐ข). |
@ -0,0 +1,7 @@ |
|||
# ๐ฌ ๐: ๐ด - ๐คซ |
|||
|
|||
๐โ ๐ ๐ช ๐ ๐ ๐โ๐ฆบ (`startup` & `shutdown`) ๐ ๐ ๐ฏ, ๐ ๐ช โ๏ธ `TestClient` โฎ๏ธ `with` ๐: |
|||
|
|||
```Python hl_lines="9-12 20-24" |
|||
{!../../../docs_src/app_testing/tutorial003.py!} |
|||
``` |
@ -0,0 +1,12 @@ |
|||
# ๐ฌ *๏ธโฃ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ `TestClient` ๐ฏ*๏ธโฃ. |
|||
|
|||
๐, ๐ โ๏ธ `TestClient` `with` ๐, ๐*๏ธโฃ: |
|||
|
|||
```Python hl_lines="27-31" |
|||
{!../../../docs_src/app_testing/tutorial002.py!} |
|||
``` |
|||
|
|||
!!! note |
|||
๐
โน, โ
๐ ๐งพ <a href="https://www.starlette.io/testclient/#testing-websocket-sessions" class="external-link" target="_blank">๐ฌ *๏ธโฃ </a>. |
@ -0,0 +1,52 @@ |
|||
# โ๏ธ ๐จ ๐ |
|||
|
|||
๐ ๐, ๐ โ๏ธ ๐ฃ ๐ ๐จ ๐ ๐ ๐ช โฎ๏ธ ๐ซ ๐. |
|||
|
|||
โ ๐ โช๏ธโก๏ธ: |
|||
|
|||
* โก ๐ข. |
|||
* ๐. |
|||
* ๐ช. |
|||
* โ๏ธ. |
|||
|
|||
& ๐จ, **FastAPI** โ ๐ ๐ฝ, ๐ญ โซ๏ธ & ๐ญ ๐งพ ๐ ๐ ๏ธ ๐. |
|||
|
|||
โ๏ธ ๐ค โ ๐โ ๐ ๐ช ๐ช ๐ `Request` ๐ ๐. |
|||
|
|||
## โน ๐ `Request` ๐ |
|||
|
|||
**FastAPI** ๐ค **๐** ๐, โฎ๏ธ ๐งฝ ๐ ๐งฐ ๐ ๐, ๐ ๐ช โ๏ธ ๐ <a href="https://www.starlette.io/requests/" class="external-link" target="_blank">`Request`</a> ๐ ๐ ๐โ ๐ ๐ช. |
|||
|
|||
โซ๏ธ ๐ โ ๐ ๐ฅ ๐ ๐ค ๐ โช๏ธโก๏ธ `Request` ๐ ๐ (๐ผ, โ ๐ช) โซ๏ธ ๐ ๐ซ โ, ๐ โ๏ธ ๐ (โฎ๏ธ ๐, ๐ง ๐ ๏ธ ๐ฉโ๐ป ๐ข) FastAPI. |
|||
|
|||
๐ ๐ ๐ ๐ข ๐ฃ ๐ (๐ผ, ๐ช โฎ๏ธ Pydantic ๐ท) ๐ โ, ๐, โ, โ๏ธ. |
|||
|
|||
โ๏ธ ๐ค ๐ฏ ๐ผ ๐โ โซ๏ธ โ ๐ค `Request` ๐. |
|||
|
|||
## โ๏ธ `Request` ๐ ๐ |
|||
|
|||
โก๏ธ ๐ ๐ ๐ ๐ค ๐ฉโ๐ป ๐ข ๐ข/๐ฆ ๐ ๐ *โก ๐ ๏ธ ๐ข*. |
|||
|
|||
๐ ๐ ๐ช ๐ ๐จ ๐. |
|||
|
|||
```Python hl_lines="1 7-8" |
|||
{!../../../docs_src/using_request_directly/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ฃ *โก ๐ ๏ธ ๐ข* ๐ข โฎ๏ธ ๐ โ `Request` **FastAPI** ๐ ๐ญ ๐ถโโ๏ธ `Request` ๐ ๐ข. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ ๐ผ, ๐ฅ ๐ฃ โก ๐ข โคด๏ธ ๐จ ๐ข. |
|||
|
|||
, โก ๐ข ๐ โ, โ, ๐ โ ๐ & โ โฎ๏ธ ๐. |
|||
|
|||
๐ ๐, ๐ ๐ช ๐ฃ ๐ ๐ ๐ข ๐, & โก, ๐ค `Request` ๐โโ๏ธ. |
|||
|
|||
## `Request` ๐งพ |
|||
|
|||
๐ ๐ช โ ๐
โน ๐ <a href="https://www.starlette.io/requests/" class="external-link" target="_blank">`Request` ๐ ๐ ๐ ๐งพ ๐ธ</a>. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.requests import Request`. |
|||
|
|||
**FastAPI** ๐ โซ๏ธ ๐ ๐ช ๐, ๐ฉโ๐ป. โ๏ธ โซ๏ธ ๐ ๐ โช๏ธโก๏ธ ๐. |
@ -0,0 +1,184 @@ |
|||
# *๏ธโฃ |
|||
|
|||
๐ ๐ช โ๏ธ <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API" class="external-link" target="_blank"> *๏ธโฃ </a> โฎ๏ธ **FastAPI**. |
|||
|
|||
## โ `WebSockets` |
|||
|
|||
๐ฅ ๐ ๐ช โ `WebSockets`: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install websockets |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
## *๏ธโฃ ๐ฉโ๐ป |
|||
|
|||
### ๐ญ |
|||
|
|||
๐ ๐ญ โ๏ธ, ๐ ๐ฒ โ๏ธ ๐ธ โ โฎ๏ธ ๐ ๐ ๏ธ ๐ ๐ฅ, Vue.js โ๏ธ ๐. |
|||
|
|||
& ๐ โ๏ธ *๏ธโฃ โฎ๏ธ ๐ ๐ฉโ๐ป ๐ ๐ ๐ฒ โ๏ธ ๐ ๐ธ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ช โ๏ธ ๐ฆ๐ธ ๐ฑ ๐ธ ๐ ๐ โฎ๏ธ ๐ *๏ธโฃ ๐ฉโ๐ป ๐, ๐ฆ๐ธ ๐. |
|||
|
|||
โ๏ธ ๐ 5๏ธโฃ๐ โ๏ธ ๐ ๐ ๐ ๐ โฎ๏ธ *๏ธโฃ ๐. |
|||
|
|||
--- |
|||
|
|||
โ๏ธ ๐ ๐ผ, ๐ฅ ๐ โ๏ธ ๐ถ ๐
๐ธ ๐ โฎ๏ธ ๐ธ, ๐ ๐ ๐ ๐ป. |
|||
|
|||
๐, โ๏ธ, ๐ซ โ & ๐ ๐ซ๐ โ๏ธ โซ๏ธ ๐ญ. |
|||
|
|||
๐ญ ๐ ๐ โ๏ธ 1๏ธโฃ ๐ ๐. |
|||
|
|||
โ๏ธ โซ๏ธ ๐
๐ ๐ฏ ๐ ๐ฝ-๐ *๏ธโฃ & โ๏ธ ๐ท ๐ผ: |
|||
|
|||
```Python hl_lines="2 6-38 41-43" |
|||
{!../../../docs_src/websockets/tutorial001.py!} |
|||
``` |
|||
|
|||
## โ `websocket` |
|||
|
|||
๐ **FastAPI** ๐ธ, โ `websocket`: |
|||
|
|||
```Python hl_lines="1 46-47" |
|||
{!../../../docs_src/websockets/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.websockets import WebSocket`. |
|||
|
|||
**FastAPI** ๐ ๐ `WebSocket` ๐ ๐ช ๐, ๐ฉโ๐ป. โ๏ธ โซ๏ธ ๐ ๐ โช๏ธโก๏ธ ๐. |
|||
|
|||
## โ ๐ง & ๐จ ๐ง |
|||
|
|||
๐ *๏ธโฃ ๐ฃ ๐ ๐ช `await` ๐ง & ๐จ ๐ง. |
|||
|
|||
```Python hl_lines="48-52" |
|||
{!../../../docs_src/websockets/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ ๐ช ๐จ & ๐จ ๐ฑ, โ, & ๐ป ๐ฝ. |
|||
|
|||
## ๐ โซ๏ธ |
|||
|
|||
๐ฅ ๐ ๐ ๐ `main.py`, ๐ ๐ ๐ธ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --reload |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ฅ <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>. |
|||
|
|||
๐ ๐ ๐ ๐
๐ ๐: |
|||
|
|||
<img src="/img/tutorial/websockets/image01.png"> |
|||
|
|||
๐ ๐ช ๐ ๐ง ๐ข ๐ฆ, & ๐จ ๐ซ: |
|||
|
|||
<img src="/img/tutorial/websockets/image02.png"> |
|||
|
|||
& ๐ **FastAPI** ๐ธ โฎ๏ธ *๏ธโฃ ๐ ๐จ ๐: |
|||
|
|||
<img src="/img/tutorial/websockets/image03.png"> |
|||
|
|||
๐ ๐ช ๐จ (& ๐จ) ๐ ๐ง: |
|||
|
|||
<img src="/img/tutorial/websockets/image04.png"> |
|||
|
|||
& ๐ ๐ซ ๐ โ๏ธ ๐ *๏ธโฃ ๐. |
|||
|
|||
## โ๏ธ `Depends` & ๐ |
|||
|
|||
*๏ธโฃ ๐ ๐ ๐ช ๐ โช๏ธโก๏ธ `fastapi` & โ๏ธ: |
|||
|
|||
* `Depends` |
|||
* `Security` |
|||
* `Cookie` |
|||
* `Header` |
|||
* `Path` |
|||
* `Query` |
|||
|
|||
๐ซ ๐ท ๐ ๐ ๐ FastAPI ๐/*โก ๐ ๏ธ*: |
|||
|
|||
```Python hl_lines="66-77 76-91" |
|||
{!../../../docs_src/websockets/tutorial002.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
๐ *๏ธโฃ โซ๏ธ ๐ซ ๐ค โ ๐ ๐ค `HTTPException`, โฉ๏ธ ๐ฅ ๐ค `WebSocketException`. |
|||
|
|||
๐ ๐ช โ๏ธ ๐ช ๐ โช๏ธโก๏ธ <a href="https://tools.ietf.org/html/rfc6455#section-7.4.1" class="external-link" target="_blank">โ ๐ ๐ฌ ๐ง</a>. |
|||
|
|||
### ๐ *๏ธโฃ โฎ๏ธ ๐ |
|||
|
|||
๐ฅ ๐ ๐ ๐ `main.py`, ๐ ๐ ๐ธ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --reload |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ฅ <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>. |
|||
|
|||
๐ค ๐ ๐ช โ: |
|||
|
|||
* "๐ฌ ๐", โ๏ธ โก. |
|||
* "๐ค" โ๏ธ ๐ข ๐ข. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ข `token` ๐ ๐ต ๐. |
|||
|
|||
โฎ๏ธ ๐ ๐ ๐ช ๐ *๏ธโฃ & โคด๏ธ ๐จ & ๐จ ๐ง: |
|||
|
|||
<img src="/img/tutorial/websockets/image05.png"> |
|||
|
|||
## ๐ ๐ & ๐ ๐ฉโ๐ป |
|||
|
|||
๐โ *๏ธโฃ ๐ ๐ช, `await websocket.receive_text()` ๐ ๐ค `WebSocketDisconnect` โ , โ ๐ ๐ช โคด๏ธ โ & ๐ต ๐ ๐ ๐ผ. |
|||
|
|||
```Python hl_lines="81-83" |
|||
{!../../../docs_src/websockets/tutorial003.py!} |
|||
``` |
|||
|
|||
๐ โซ๏ธ ๐
: |
|||
|
|||
* ๐ ๐ฑ โฎ๏ธ ๐ ๐ฅ ๐. |
|||
* โ ๐ง โช๏ธโก๏ธ ๐ซ. |
|||
* โคด๏ธ ๐ 1๏ธโฃ ๐. |
|||
|
|||
๐ ๐ ๐ค `WebSocketDisconnect` โ , & ๐ ๐ ๐ฉโ๐ป ๐ ๐จ ๐ง ๐: |
|||
|
|||
``` |
|||
Client #1596980209979 left the chat |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฑ ๐ โญ & ๐
๐ผ ๐ฆ โ ๐ต & ๐ป ๐ง ๐ *๏ธโฃ ๐. |
|||
|
|||
โ๏ธ โ๏ธ ๐คฏ ๐, ๐ ๐ต ๐พ, ๐ ๐, โซ๏ธ ๐ ๐ด ๐ท โช ๐ ๏ธ ๐, & ๐ ๐ด ๐ท โฎ๏ธ ๐ ๐ ๏ธ. |
|||
|
|||
๐ฅ ๐ ๐ช ๐ณ โฉ ๐ ๏ธ โฎ๏ธ FastAPI โ๏ธ ๐ ๐ ๐๏ธ, ๐โ๐ฆบ โณ, โณ โ๏ธ ๐, โ
<a href="https://github.com/encode/broadcaster" class="external-link" target="_blank">๐/๐ป</a>. |
|||
|
|||
## ๐
โน |
|||
|
|||
๐ก ๐
๐ ๐, โ
๐ ๐งพ: |
|||
|
|||
* <a href="https://www.starlette.io/websockets/" class="external-link" target="_blank"> `WebSocket` ๐</a>. |
|||
* <a href="https://www.starlette.io/endpoints/#websocketendpoint" class="external-link" target="_blank">๐-โ๏ธ *๏ธโฃ ๐</a>. |
@ -0,0 +1,37 @@ |
|||
# โ
๐จ๐ป - ๐บ, โณ, ๐ |
|||
|
|||
๐ ๐ช ๐ป ๐จ๐ป ๐ธ ๐ ๐ โฎ๏ธ [๐ง ๐ธ - ๐ป](./sub-applications.md){.internal-link target=_blank}, [โ
๐ณ](./behind-a-proxy.md){.internal-link target=_blank}. |
|||
|
|||
๐, ๐ ๐ช โ๏ธ `WSGIMiddleware` & โ๏ธ โซ๏ธ ๐ ๐ ๐จ๐ป ๐ธ, ๐ผ, ๐บ, โณ, โ๏ธ. |
|||
|
|||
## โ๏ธ `WSGIMiddleware` |
|||
|
|||
๐ ๐ช ๐ `WSGIMiddleware`. |
|||
|
|||
โคด๏ธ ๐ ๐จ๐ป (โ
๐บ) ๐ฑ โฎ๏ธ ๐ ๏ธ. |
|||
|
|||
& โคด๏ธ ๐ป ๐ ๐ฝ โก. |
|||
|
|||
```Python hl_lines="2-3 22" |
|||
{!../../../docs_src/wsgi/tutorial001.py!} |
|||
``` |
|||
|
|||
## โ
โซ๏ธ |
|||
|
|||
๐, ๐ ๐จ ๐ฝ โก `/v1/` ๐ ๐ต ๐บ ๐ธ. |
|||
|
|||
& ๐ ๐ ๐ต **FastAPI**. |
|||
|
|||
๐ฅ ๐ ๐ โซ๏ธ โฎ๏ธ Uvicorn & ๐ถ <a href="http://localhost:8000/v1/" class="external-link" target="_blank">http://localhost:8000/v1/</a> ๐ ๐ ๐ ๐จ โช๏ธโก๏ธ ๐บ: |
|||
|
|||
```txt |
|||
Hello, World from Flask! |
|||
``` |
|||
|
|||
& ๐ฅ ๐ ๐ถ <a href="http://localhost:8000/v2" class="external-link" target="_blank">http://localhost:8000/v2</a> ๐ ๐ ๐ ๐จ โช๏ธโก๏ธ FastAPI: |
|||
|
|||
```JSON |
|||
{ |
|||
"message": "Hello World" |
|||
} |
|||
``` |
@ -0,0 +1,414 @@ |
|||
# ๐, ๐ & ๐บ |
|||
|
|||
โซ๏ธโ ๐ฎ **FastAPI**, โ โซ๏ธ ๐ฌ ๐ ๐ & โซ๏ธโ โซ๏ธ ๐ญ๐ฒ โช๏ธโก๏ธ ๐ซ. |
|||
|
|||
## ๐ถ |
|||
|
|||
**FastAPI** ๐ซ๐ ๐ ๐ฅ ๐ซ โฎ๏ธ ๐ท ๐. |
|||
|
|||
๐ค โ๏ธ ๐ ๐งฐ โ โญ ๐ โ๏ธ โน ๐ฎ ๐ฎ ๐. |
|||
|
|||
๐ค โ๏ธ โ ๐ ๐ ๐ ๏ธ ๐ 1๏ธโฃ2๏ธโฃ๐๏ธ. ๐ฅ ๐ค ๐ โ ๐ โ ๐ **FastAPI** โ๏ธ ๐ ๐ ๐ ๏ธ, ๐-๐, & ๐งฐ. |
|||
|
|||
โ๏ธ โ, ๐ค ๐
โโ ๐ ๐ ๐ ๐ ๐ณ ๐ ๐ ๐ ๐ซ โ, โ ๐ ๐ญ โช๏ธโก๏ธ โฎ๏ธ ๐งฐ, & ๐ ๐ซ ๐ ๐ ๐ช, โ๏ธ ๐ช๐ธ โ ๐ โ๐ซ ๐ช โญ (๐ 3๏ธโฃ.6๏ธโฃ โ ๐ ๐). |
|||
|
|||
## โฎ๏ธ ๐งฐ |
|||
|
|||
### <a href="https://www.djangoproject.com/" class="external-link" target="_blank">โณ</a> |
|||
|
|||
โซ๏ธ ๐
๐ ๐ ๐ ๏ธ & ๐ ๐ด. โซ๏ธ โ๏ธ ๐ โ๏ธ ๐ ๐ฑ๐. |
|||
|
|||
โซ๏ธ ๐ถ ๐ ๐ โฎ๏ธ ๐ ๐ฝ (๐ โณ โ๏ธ โณ),, โ๏ธ โ ๐ฝ (๐ ๐, โณ, ๐ธ, โ๏ธ) ๐ ๐ช ๐ ๐ซ ๐ถ โฉ. |
|||
|
|||
โซ๏ธ โ ๐ ๐ธ ๐ฉโ๐ป, ๐ซ โ ๐ โ๏ธ ๐ ๐ธ (๐ ๐ฅ, Vue.js & ๐) โ๏ธ ๐ โ๏ธ (๐ <abbr title="Internet of Things">โ</abbr> ๐ณ) ๐ โฎ๏ธ โซ๏ธ. |
|||
|
|||
### <a href="https://www.django-rest-framework.org/" class="external-link" target="_blank">โณ ๐ ๐ ๏ธ</a> |
|||
|
|||
โณ ๐ ๐ ๏ธ โ ๐ ๐งฐ ๐ ๐ธ ๐ โ๏ธ โณ ๐, ๐ ๐ฎ ๐ ๏ธ ๐ ๏ธ. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ ๐ข โ
๐ฆ, ๐ฅ ๐ & ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ฅ ๐ผ **๐ง ๐ ๏ธ ๐งพ**, & ๐ ๐ฏ ๐ ๐ฅ ๐ญ ๐ ๐ฎ "๐" **FastAPI**. |
|||
|
|||
!!! note |
|||
โณ ๐ ๐ ๏ธ โ โก ๐บ๐ธ๐. ๐ ๐ผ ๐ & Uvicorn, ๐ โ **FastAPI** โ๏ธ. |
|||
|
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
โ๏ธ ๐ง ๐ ๏ธ ๐งพ ๐ธ ๐ฉโ๐ป ๐ข. |
|||
|
|||
### <a href="https://flask.palletsprojects.com" class="external-link" target="_blank">๐บ</a> |
|||
|
|||
๐บ "๐ธ", โซ๏ธ ๐ซ ๐ ๐ฝ ๐ ๏ธ ๐ซ ๐ ๐ ๐ ๐ ๐ข โณ. |
|||
|
|||
๐ ๐ฆ & ๐ช โ ๐จ ๐ ๐ โ๏ธ โ ๐ฝ ๐ ๐ฝ ๐พ โ๏ธ. |
|||
|
|||
โซ๏ธ ๐ถ ๐
, โซ๏ธ ๐ถ ๐๏ธ ๐ก, ๐ ๐งพ ๐ค ๐ ๐ก โ. |
|||
|
|||
โซ๏ธ ๐ โ๏ธ ๐ ๐ธ ๐ ๐ซ ๐ฏ ๐ช ๐ฝ, ๐ฉโ๐ป ๐งพ, โ๏ธ ๐ ๐ โ ๐ ๐ ๐ค-๐ โณ. ๐ ๐ ๐ซ โ ๐ช ๐ฎ โฎ๏ธ ๐-๐. |
|||
|
|||
๐ โ ๐, & โ "๐ธ" ๐ ๐ช โ ๐ โซ๏ธโ โซ๏ธโ ๐ช ๐ โ ๐ ๐ค ๐ ๐ง. |
|||
|
|||
๐ ๐ฆ ๐บ, โซ๏ธ ๐ ๐ ๐ ๐ ๐ ๐. โญ ๐ ๐ "โณ ๐ ๐ ๏ธ" ๐บ. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
โพ-๐ ๏ธ. โ โซ๏ธ โฉ ๐ & ๐ ๐งฐ & ๐ ๐ช. |
|||
|
|||
โ๏ธ ๐
& โฉ โ๏ธ ๐น โ๏ธ. |
|||
|
|||
|
|||
### <a href="https://requests.readthedocs.io" class="external-link" target="_blank">๐จ</a> |
|||
|
|||
**FastAPI** ๐ซ ๐ค ๐ **๐จ**. ๐ซ โ ๐ถ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ค โ โ๏ธ ๐จ *๐* FastAPI ๐ธ. |
|||
|
|||
โ๏ธ, FastAPI ๐ค ๐ โช๏ธโก๏ธ ๐จ. |
|||
|
|||
**๐จ** ๐ *๐* โฎ๏ธ ๐ (๐ฉโ๐ป), โช **FastAPI** ๐ *๐* ๐ (๐ฝ). |
|||
|
|||
๐ซ, ๐ โ๏ธ ๐, ๐ ๐, ๐ ๐ ๐. |
|||
|
|||
๐จ โ๏ธ ๐ถ ๐
& ๐๏ธ ๐ง, โซ๏ธ ๐ถ โฉ โ๏ธ, โฎ๏ธ ๐ค ๐ข. โ๏ธ ๐ ๐ฐ, โซ๏ธ ๐ถ ๐๏ธ & ๐. |
|||
|
|||
๐ โซ๏ธโ, ๐ฌ ๐ ๐ธ: |
|||
|
|||
> ๐จ 1๏ธโฃ ๐ โฌ ๐ ๐ฆ ๐ ๐ฐ |
|||
|
|||
๐ ๐ โ๏ธ โซ๏ธ ๐ถ ๐
. ๐ผ, `GET` ๐จ, ๐ ๐ โ: |
|||
|
|||
```Python |
|||
response = requests.get("http://example.com/some/url") |
|||
``` |
|||
|
|||
FastAPI ๐ ๐ ๏ธ *โก ๐ ๏ธ* ๐ช ๐ ๐: |
|||
|
|||
```Python hl_lines="1" |
|||
@app.get("/some/url") |
|||
def read_url(): |
|||
return {"message": "Hello World"} |
|||
``` |
|||
|
|||
๐ ๐ `requests.get(...)` & `@app.get(...)`. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
* โ๏ธ ๐
& ๐๏ธ ๐ ๏ธ. |
|||
* โ๏ธ ๐บ๐ธ๐ ๐ฉโ๐ฌ ๐ (๐ ๏ธ) ๐, ๐ฏ & ๐๏ธ ๐. |
|||
* โ๏ธ ๐ค ๐ข, โ๏ธ ๐๏ธ ๐. |
|||
|
|||
|
|||
### <a href="https://swagger.io/" class="external-link" target="_blank">๐ฆ</a> / <a href="https://github.com/OAI/OpenAPI-Specification/" class="external-link" target="_blank">๐</a> |
|||
|
|||
๐ โ ๐ค ๐ โช๏ธโก๏ธ โณ ๐ ๐ ๏ธ ๐ง ๐ ๏ธ ๐งพ. |
|||
|
|||
โคด๏ธ ๐ค ๐ ๐ ๐ค ๐ฉ ๐ ๐, โ๏ธ ๐ป (โ๏ธ ๐, โ ๐ป) ๐ค ๐ฆ. |
|||
|
|||
& ๐ค ๐ธ ๐ฉโ๐ป ๐ข ๐ฆ ๐ ๏ธ โช โ. , ๐โโ ๐ช ๐ ๐ฆ ๐งพ ๐ ๏ธ ๐ โ โ๏ธ ๐ ๐ธ ๐ฉโ๐ป ๐ข ๐. |
|||
|
|||
โ, ๐ฆ ๐ ๐พ ๐, ๐ ๐. |
|||
|
|||
๐ โซ๏ธโ ๐โ ๐ฌ ๐ โฌ 2๏ธโฃ.0๏ธโฃ โซ๏ธ โ ๐ฌ "๐ฆ", & โฌ 3๏ธโฃ โ "๐". |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
๐ ๏ธ & โ๏ธ ๐ ๐ฉ ๐ ๏ธ ๐ง, โฉ๏ธ ๐ ๐. |
|||
|
|||
& ๐ ๏ธ ๐ฉ-โ๏ธ ๐ฉโ๐ป ๐ข ๐งฐ: |
|||
|
|||
* <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">๐ฆ ๐</a> |
|||
* <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">๐</a> |
|||
|
|||
๐ซ 2๏ธโฃ ๐ โ ๐ถ ๐ & โ, โ๏ธ ๐จ โฉ ๐, ๐ ๐ช ๐ ๐ฏ ๐ ๐ ๐ฉโ๐ป ๐ข ๐ (๐ ๐ ๐ช โ๏ธ โฎ๏ธ **FastAPI**). |
|||
|
|||
### ๐บ ๐ ๐ ๏ธ |
|||
|
|||
๐ค ๐ ๐บ ๐ ๐ ๏ธ, โ๏ธ โฎ๏ธ ๐ฐ ๐ฐ & ๐ท ๐ ๐ฌ ๐ซ, ๐ค ๐ ๐ ๐ ๐ โ๏ธ ๐ซ, โฎ๏ธ ๐ ๐ง โ ๐ โ ๐ซ ๐. |
|||
|
|||
### <a href="https://marshmallow.readthedocs.io/en/stable/" class="external-link" target="_blank">๐ญ</a> |
|||
|
|||
1๏ธโฃ ๐ โ ๐ช ๐ ๏ธ โ๏ธ ๐ "<abbr title="also called marshalling, conversion">๐ ๏ธ</abbr>" โ โ ๐ โช๏ธโก๏ธ ๐ (๐) & ๐ญ โซ๏ธ ๐ ๐ณ ๐ ๐ช ๐จ ๐ ๐ธ. ๐ผ, ๐ญ ๐ โ ๐ โช๏ธโก๏ธ ๐ฝ ๐ ๐ป ๐. ๐ญ `datetime` ๐ ๐ ๐ป, โ๏ธ. |
|||
|
|||
โ1๏ธโฃ ๐ฆ โ ๐ ๐ ๐ฝ ๐ฌ, โ ๐ญ ๐ ๐ฝ โ, ๐ค ๐ฏ ๐ข. ๐ผ, ๐ ๐ `int`, & ๐ซ ๐ฒ ๐ป. ๐ โด๏ธ โ ๐จ ๐ฝ. |
|||
|
|||
๐ต ๐ฝ ๐ฌ โ๏ธ, ๐ ๐ โ๏ธ ๐ โ
โ, ๐. |
|||
|
|||
๐ซ โ โซ๏ธโ ๐ญ ๐ ๐. โซ๏ธ ๐ ๐, & ๐ค โ๏ธ โ๏ธ โซ๏ธ ๐ โญ. |
|||
|
|||
โ๏ธ โซ๏ธ โ โญ ๐ค ๐ ๐ ๐ ๐. , ๐ฌ ๐ <abbr title="the definition of how data should be formed">๐</abbr> ๐ ๐ช โ๏ธ ๐ฏ ๐จ๐ป & ๐ ๐ ๐ญ. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
โ๏ธ ๐ ๐ฌ "๐" ๐ ๐ ๐ฝ ๐ & ๐ฌ, ๐. |
|||
|
|||
### <a href="https://webargs.readthedocs.io/en/latest/" class="external-link" target="_blank">Webarg</a> |
|||
|
|||
โ1๏ธโฃ ๐ฆ โ โ ๐ <abbr title="reading and converting to Python data">โ</abbr> ๐ โช๏ธโก๏ธ ๐จ ๐จ. |
|||
|
|||
Webarg ๐งฐ ๐ โ ๐ ๐ ๐ ๐ ๐ ๐ ๏ธ, ๐ ๐บ. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ญ ๐ ๐ฝ ๐ฌ. & โซ๏ธ โ ๐ ๐ฉโ๐ป. |
|||
|
|||
โซ๏ธ ๐ ๐งฐ & ๐ค โ๏ธ โ๏ธ โซ๏ธ ๐ ๐โโ๏ธ, โญ โ๏ธ **FastAPI**. |
|||
|
|||
!!! info |
|||
Webarg โ ๐ ๐ญ ๐ฉโ๐ป. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
โ๏ธ ๐ง ๐ฌ ๐จ ๐จ ๐ฝ. |
|||
|
|||
### <a href="https://apispec.readthedocs.io/en/stable/" class="external-link" target="_blank">APISpec</a> |
|||
|
|||
๐ญ & Webarg ๐ ๐ฌ, โ & ๐ ๏ธ ๐-๐. |
|||
|
|||
โ๏ธ ๐งพ โ. โคด๏ธ APISpec โ. |
|||
|
|||
โซ๏ธ ๐-๐ ๐ ๏ธ (& ๐ค ๐-๐ ๐โโ๏ธ). |
|||
|
|||
๐ โซ๏ธ ๐ท ๐ ๐ โ ๐ ๐ โ๏ธ ๐ ๐ ๐ #๏ธโฃ ๐ ๐ข ๐ ๐ฃ. |
|||
|
|||
& โซ๏ธ ๐ ๐ ๐. |
|||
|
|||
๐ โ โซ๏ธ ๐ท ๐บ, ๐, ๐, โ๏ธ. |
|||
|
|||
โ๏ธ โคด๏ธ, ๐ฅ โ๏ธ ๐ โ โ๏ธ โพ-โ, ๐ ๐ ๐ป (๐ฆ ๐). |
|||
|
|||
๐จโ๐จ ๐ช ๐ซ โน ๐
โฎ๏ธ ๐. & ๐ฅ ๐ฅ ๐ ๐ข โ๏ธ ๐ญ ๐ & ๐ญ ๐ ๐ ๐#๏ธโฃ, ๐ ๐ ๐ โ. |
|||
|
|||
!!! info |
|||
APISpec โ ๐ ๐ญ ๐ฉโ๐ป. |
|||
|
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
๐โ๐ฆบ ๐ ๐ฉ ๐ ๏ธ, ๐. |
|||
|
|||
### <a href="https://flask-apispec.readthedocs.io/en/latest/" class="external-link" target="_blank">๐บ-Apispec</a> |
|||
|
|||
โซ๏ธ ๐บ ๐ -, ๐ ๐ ๐ฏโโ๏ธ Webarg, ๐ญ & APISpec. |
|||
|
|||
โซ๏ธ โ๏ธ โน โช๏ธโก๏ธ Webarg & ๐ญ ๐ ๐ ๐ ๐, โ๏ธ APISpec. |
|||
|
|||
โซ๏ธ ๐ ๐งฐ, ๐ถ ๐ฝ-๐. โซ๏ธ ๐ ๐ ๐ ๐ ๐ ๐ ๐บ ๐-๐ ๐
๐ค. โซ๏ธ ๐ช โฉ๏ธ ๐ฎ ๐งพ โ ๐โโ๏ธ ๐ฉฒ & ๐. |
|||
|
|||
๐ โ โ๏ธ โ ๐ (โ1๏ธโฃ โ) ๐ ๐ โ. |
|||
|
|||
๐ ๐ ๐บ, ๐บ-Apispec โฎ๏ธ ๐ญ & Webarg ๐ ๐ ๐ฉโ๐ป ๐ โญ ๐ **FastAPI**. |
|||
|
|||
โ๏ธ โซ๏ธ โ๏ธ ๐ ๐ ๐บ ๐-๐ ๐. ๐ซ ๐ ๐ ๐ค (& ๐ ๐ข ๐) โ๏ธ โ๏ธ ๐ ๐: |
|||
|
|||
* <a href="https://github.com/tiangolo/full-stack" class="external-link" target="_blank">https://github.com/tiangolo/full-stack</a> |
|||
* <a href="https://github.com/tiangolo/full-stack-flask-couchbase" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-flask-couchbase</a> |
|||
* <a href="https://github.com/tiangolo/full-stack-flask-couchdb" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-flask-couchdb</a> |
|||
|
|||
& ๐ซ ๐ ๐-๐ ๐ ๐งข [**FastAPI** ๐ ๐](project-generation.md){.internal-link target=_blank}. |
|||
|
|||
!!! info |
|||
๐บ-Apispec โ ๐ ๐ญ ๐ฉโ๐ป. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
๐ ๐ ๐ ๐, โช๏ธโก๏ธ ๐ ๐ ๐ ๐ฌ ๐ ๏ธ & ๐ฌ. |
|||
|
|||
### <a href="https://nestjs.com/" class="external-link" target="_blank">NestJS</a> (& <a href="https://angular.io/" class="external-link" target="_blank">๐</a>) |
|||
|
|||
๐ โ๐ซ ๐ซ ๐, NestJS ๐ธ (๐) โณ ๐ ๏ธ ๐ฎ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ณ ๐ ๐ โซ๏ธโ ๐ช ๐จ โฎ๏ธ ๐บ-Apispec. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ ๏ธ ๐ ๐ โ๏ธ, ๐ฎ ๐ 2๏ธโฃ. โซ๏ธ ๐ ๐ค-ยฎ "๐" (๐ ๐ ๐ ๐ ๐ โ๏ธ ๐ค ๐ญ),, โซ๏ธ ๐ฎ ๐ญ & ๐ ๐. |
|||
|
|||
๐ข ๐ฌ โฎ๏ธ ๐ ๐ (๐ ๐ ๐ ๐), ๐จโ๐จ ๐โ๐ฆบ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ซ ๐ก โฎ๏ธ ๐น ๐ธ, โซ๏ธ ๐ซ๐ โ๏ธ ๐ ๐ ๐ฌ ๐ฌ, ๐ ๏ธ & ๐งพ ๐ ๐ฐ. โฉ๏ธ ๐ & ๐ง ๐ซ, ๐ค ๐ฌ, ๐ ๏ธ & ๐ง ๐ โก, โซ๏ธ ๐ช ๐ฎ ๐จโ๐จ ๐ ๐ฅ. , โซ๏ธ โถ๏ธ๏ธ ๐. |
|||
|
|||
โซ๏ธ ๐ช ๐ซ ๐ต ๐ ๐ท ๐ถ ๐. , ๐ฅ ๐ป ๐ช ๐จ ๐ป ๐ ๐ โ๏ธ ๐ ๐ ๐ ๐ ๐ฆ ๐ป ๐, โซ๏ธ ๐ซ๐ โ ๐ & โ. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
โ๏ธ ๐ ๐ โ๏ธ ๐ ๐จโ๐จ ๐โ๐ฆบ. |
|||
|
|||
โ๏ธ ๐๏ธ ๐ ๐ โ๏ธ. ๐ ๐ ๐ ๐ ๐. |
|||
|
|||
### <a href="https://sanic.readthedocs.io/en/latest/" class="external-link" target="_blank">๐คฃ</a> |
|||
|
|||
โซ๏ธ ๐ ๐ฅ ๐ถ โฉ ๐ ๐ ๏ธ โ๏ธ ๐ `asyncio`. โซ๏ธ โ ๐ถ ๐ ๐บ. |
|||
|
|||
!!! note "๐ก โน" |
|||
โซ๏ธ โ๏ธ <a href="https://github.com/MagicStack/uvloop" class="external-link" target="_blank">`uvloop`</a> โฉ๏ธ ๐ข ๐ `asyncio` โฐ. ๐ โซ๏ธโ โ โซ๏ธ โฉ. |
|||
|
|||
โซ๏ธ ๐ฏ ๐ฎ Uvicorn & ๐, ๐ โณ โฉ ๐ ๐คฃ ๐ ๐. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
๐ ๐ โ๏ธ ๐ ๐ญ. |
|||
|
|||
๐ โซ๏ธโ **FastAPI** โ๏ธ ๐ ๐, โซ๏ธ โฉ ๐ ๏ธ ๐ช (๐ฏ ๐ฅ-๐ฅณ ๐). |
|||
|
|||
### <a href="https://falconframework.org/" class="external-link" target="_blank">๐ฆ
</a> |
|||
|
|||
๐ฆ
โ1๏ธโฃ โ ๐ญ ๐ ๐ ๏ธ, โซ๏ธ ๐ง โญ, & ๐ท ๐ ๐ ๐ ๏ธ ๐ ๐ค. |
|||
|
|||
โซ๏ธ ๐ โ๏ธ ๐ข ๐ ๐จ 2๏ธโฃ ๐ข, 1๏ธโฃ "๐จ" & 1๏ธโฃ "๐จ". โคด๏ธ ๐ "โ" ๐ โช๏ธโก๏ธ ๐จ, & "โ" ๐ ๐จ. โฉ๏ธ ๐ ๐ง, โซ๏ธ ๐ซ ๐ช ๐ฃ ๐จ ๐ข & ๐ช โฎ๏ธ ๐ฉ ๐ ๐ ๐ ๐ข ๐ข. |
|||
|
|||
, ๐ฝ ๐ฌ, ๐ ๏ธ, & ๐งพ, โ๏ธ โ ๐, ๐ซ ๐. โ๏ธ ๐ซ โ๏ธ ๐ ๏ธ ๐ ๏ธ ๐ ๐ ๐ฆ
, ๐ ๐ค. ๐ ๐ ๐บ ๐จ ๐ ๐ ๏ธ ๐ ๐ฎ ๐ฆ
๐ง, โ๏ธ 1๏ธโฃ ๐จ ๐ & 1๏ธโฃ ๐จ ๐ ๐ข. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
๐ ๐ ๐ค ๐ ๐ญ. |
|||
|
|||
โคด๏ธ โฎ๏ธ ๐ค (๐ค โ๏ธ ๐ ๐ฆ
) ๐ฎ **FastAPI** ๐ฃ `response` ๐ข ๐ข. |
|||
|
|||
๐ FastAPI โซ๏ธ ๐ฆ, & โ๏ธ โด๏ธ โ ๐, ๐ช, & ๐ ๐ ๐. |
|||
|
|||
### <a href="https://moltenframework.com/" class="external-link" target="_blank">โจ</a> |
|||
|
|||
๐ค ๐ โจ ๐ฅ โถ๏ธ ๐ **FastAPI**. & โซ๏ธ โ๏ธ ๐ ๐ญ: |
|||
|
|||
* โ๏ธ ๐ ๐ ๐ ๐. |
|||
* ๐ฌ & ๐งพ โช๏ธโก๏ธ ๐ซ ๐. |
|||
* ๐ ๐ โ๏ธ. |
|||
|
|||
โซ๏ธ ๐ซ โ๏ธ ๐ฝ ๐ฌ, ๐ ๏ธ & ๐งพ ๐ฅ-๐ฅณ ๐ ๐ Pydantic, โซ๏ธ โ๏ธ ๐ฎ ๐. , ๐ซ ๐ฝ ๐ ๐ ๐ ๐ซ โป ๐ช. |
|||
|
|||
โซ๏ธ ๐ ๐ฅ ๐ ๐
๐ ๐ณ. & โซ๏ธ โ๏ธ ๐ ๐จ๐ป (โฉ๏ธ ๐ซ), โซ๏ธ ๐ซ ๐ง โ ๐ โ-๐ญ ๐ ๐งฐ ๐ Uvicorn, ๐ & ๐คฃ. |
|||
|
|||
๐ ๐ โ๏ธ ๐ ๐ค-ยฎ ๐ & ๐ โ ๐งข ๐ ๐ฃ ๐. , โซ๏ธ ๐ซ ๐ช ๐ฃ ๐
๐ 1๏ธโฃ "๐ฆฒ" ๐ ๐ ๐ฏ ๐. |
|||
|
|||
๐ฃ ๐ฃ ๐ ๐ฅ, โ๏ธ ๐ข ๐ฃ ๐ ๐ฅ (โฉ๏ธ โ๏ธ ๐จโ๐จ ๐ ๐ช ๐ฅ โถ๏ธ๏ธ ๐ ๐ ๐ข ๐ ๐ต ๐). ๐ ๐ โ โณ ๐จ โซ๏ธ ๐ โ ๐บ (& ๐) ๐จ โซ๏ธ. โซ๏ธ ๐ ๐ ๐ ๐ ๐ถ ๐ ๐. |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
๐ฌ โ ๐ฌ ๐ฝ ๐ โ๏ธ "๐ข" ๐ฒ ๐ท ๐ข. ๐ ๐ ๐จโ๐จ ๐โ๐ฆบ, & โซ๏ธ ๐ซ ๐ช Pydantic โญ. |
|||
|
|||
๐ ๐ค ๐ฎ ๐ ๏ธ ๐ Pydantic, ๐โ๐ฆบ ๐ ๐ฌ ๐ ๐ (๐ ๐ ๐ ๏ธ ๐ โช ๐ช Pydantic). |
|||
|
|||
### <a href="https://www.hug.rest/" class="external-link" target="_blank">๐ค</a> |
|||
|
|||
๐ค ๐ ๐ฅ ๐ ๏ธ ๐ ๏ธ ๐ ๐ ๏ธ ๐ข ๐ โ๏ธ ๐ ๐ ๐. ๐ ๐ ๐ญ ๐ ๐ฎ ๐ ๐งฐ ๐. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ ๐ ๐ฎ ๐ โฉ๏ธ ๐ฉ ๐ ๐, โ๏ธ โซ๏ธ ๐ฆ ๐ โฉ. |
|||
|
|||
โซ๏ธ ๐ ๐ฅ ๐ ๏ธ ๐ ๐ ๐ ๐ฃ ๐ ๐ ๏ธ ๐ป. |
|||
|
|||
โซ๏ธ ๐ซ โ๏ธ ๐ ๐ฉ ๐ ๐ & ๐ป ๐. โซ๏ธ ๐ซ๐ ๐ฏ ๐ ๏ธ โซ๏ธ โฎ๏ธ ๐ ๐งฐ, ๐ ๐ฆ ๐. โ๏ธ ๐, โซ๏ธ ๐ถ ๐ก ๐ญ. |
|||
|
|||
โซ๏ธ โ๏ธ ๐, โญ โ: โ๏ธ ๐ ๐ ๏ธ, โซ๏ธ ๐ช โ ๐ & ๐ณ๐จ. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ โฎ๏ธ ๐ฉ ๐ ๐ ๐ธ ๐ ๏ธ (๐จ๐ป), โซ๏ธ ๐ช ๐ซ ๐ต *๏ธโฃ & ๐ ๐, ๐ โซ๏ธ โ๏ธ โ ๐ญ ๐โโ๏ธ. |
|||
|
|||
!!! info |
|||
๐ค โ โก ๐, ๐ ๐ผ <a href="https://github.com/timothycrosley/isort" class="external-link" target="_blank">`isort`</a>, ๐ ๐งฐ ๐ ๐ ๐ ๐ ๐. |
|||
|
|||
!!! check "๐ญ ๐ฎ **FastAPI**" |
|||
๐ค ๐ฎ ๐ APIStar, & 1๏ธโฃ ๐งฐ ๐ค ๐ ๐ ๐, ๐ APIStar. |
|||
|
|||
๐ค โน ๐ **FastAPI** โ๏ธ ๐ ๐ ๐ ๐ฃ ๐ข, & ๐ ๐ โ ๐ ๏ธ ๐. |
|||
|
|||
๐ค ๐ฎ **FastAPI** ๐ฃ `response` ๐ข ๐ข โ ๐ & ๐ช. |
|||
|
|||
### <a href="https://github.com/encode/apistar" class="external-link" target="_blank">APIStar</a> (<= 0๏ธโฃ.5๏ธโฃ) |
|||
|
|||
โถ๏ธ๏ธ โญ ๐ค ๐ **FastAPI** ๐ค ๐ **APIStar** ๐ฝ. โซ๏ธ โ๏ธ ๐ ๐ ๐ค ๐ & โ๏ธ ๐ ๐ง. |
|||
|
|||
โซ๏ธ ๐ ๐ฅ ๐ ๏ธ ๐ ๏ธ โ๏ธ ๐ ๐ ๐ ๐ฃ ๐ข & ๐จ ๐ ๐ค โฑ ๐ (โญ NestJS & โจ). ๐ค ๐ โซ๏ธ ๐
โ๏ธ ๐ ๐ ๐ฐ ๐ค. โ๏ธ APIStar โ๏ธ ๐ ๐ฉ. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ง ๐ฝ ๐ฌ, ๐ฝ ๐ ๏ธ & ๐ ๐ โก โ๏ธ ๐ ๐ ๐ ๐ ๐ ๐ฅ. |
|||
|
|||
๐ช ๐ ๐ ๐ซ โ๏ธ ๐ ๐ ๐ ๐ ๐ Pydantic, โซ๏ธ ๐ ๐
๐ ๐ญ,, ๐จโ๐จ ๐โ๐ฆบ ๐ซ๐ ๐, โ๏ธ, APIStar ๐ ๐ช ๐. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ ๐ญ ๐ ๐ฐ (๐ด ๐ฅ ๐). |
|||
|
|||
๐ฅ, โซ๏ธ ๐ซ โ๏ธ ๐ง ๐ ๏ธ ๐งพ ๐ธ ๐, โ๏ธ ๐ค ๐ญ ๐ค ๐ช ๐ฎ ๐ฆ ๐ โซ๏ธ. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ ๐ โ๏ธ. โซ๏ธ โ ๐ค-ยฎ ๐ฆฒ, ๐ ๐งฐ ๐ฌ ๐. โ๏ธ, โซ๏ธ ๐ โ. |
|||
|
|||
๐ค ๐
๐ช โ๏ธ โซ๏ธ ๐ ๐, โซ๏ธ ๐ซ โ๏ธ ๐โโ ๐ ๏ธ,, ๐ค ๐ซ ๐ซ โ ๐ โ ๐ค โ๏ธ โฎ๏ธ ๐-๐ ๐ โ๏ธ ๐ ๐บ-Apispec. ๐ค โ๏ธ ๐ ๐ ๐ โ ๐ฒ ๐จ โ ๐ ๐ ๏ธ. |
|||
|
|||
โ๏ธ โคด๏ธ, ๐ ๐ฏ ๐. |
|||
|
|||
โซ๏ธ ๐
โโ ๐ ๐ ๏ธ ๐ธ ๐ ๏ธ, ๐ผ ๐ช ๐ฏ ๐ ๐. |
|||
|
|||
๐ APIStar โ ๐งฐ โ ๐ ๐ง, ๐ซ ๐ธ ๐ ๏ธ. |
|||
|
|||
!!! info |
|||
APIStar โ โก ๐บ๐ธ๐. ๐ ๐จ ๐ โ: |
|||
|
|||
* โณ ๐ ๐ ๏ธ |
|||
* ๐ (โ **FastAPI** โ๏ธ) |
|||
* Uvicorn (โ๏ธ ๐ & **FastAPI**) |
|||
|
|||
!!! check "๐ฎ **FastAPI** " |
|||
๐. |
|||
|
|||
๐ญ ๐ฃ ๐ ๐ (๐ฝ ๐ฌ, ๐ ๏ธ & ๐งพ) โฎ๏ธ ๐ ๐ ๐, ๐ ๐ ๐ฐ ๐ ๐ ๐จโ๐จ ๐โ๐ฆบ, ๐ณ ๐ค ๐ค ๐ ๐ญ. |
|||
|
|||
& โฎ๏ธ ๐ ๐ ๐ฐ ๐ ๐ ๏ธ & ๐ฌ ๐ ๐ ๐, APIStar ๐ ๐ ๐ช. |
|||
|
|||
โคด๏ธ APIStar โ๏ธ ๐ ๐ฝ & ๐ โ, & ๐ ๐ป ๐ โ
โ๏ธ. ๐ ๐ ๐ ๐ **FastAPI**. |
|||
|
|||
๐ค ๐ค **FastAPI** "๐ ๐จโ๐ผ" APIStar, โช ๐ & ๐ โ, โจ โ๏ธ, & ๐ ๐, โ๏ธ ๐ ๐ซ โช๏ธโก๏ธ ๐ ๐ โฎ๏ธ ๐งฐ. |
|||
|
|||
## โ๏ธ **FastAPI** |
|||
|
|||
### <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> |
|||
|
|||
Pydantic ๐ ๐ฌ ๐ฝ ๐ฌ, ๐ ๏ธ & ๐งพ (โ๏ธ ๐ป ๐) โ๏ธ ๐ ๐ ๐ ๐. |
|||
|
|||
๐ โ โซ๏ธ ๐ถ ๐๏ธ. |
|||
|
|||
โซ๏ธ โญ ๐ญ. ๐ โซ๏ธ โฉ ๐ ๐ญ ๐. & โซ๏ธ โ๏ธ ๐ ๐ ๐ ๐ ๐, ๐จโ๐จ ๐โ๐ฆบ ๐. |
|||
|
|||
!!! check "**FastAPI** โ๏ธ โซ๏ธ" |
|||
๐ต ๐ ๐ฝ ๐ฌ, ๐ฝ ๐ ๏ธ & ๐ง ๐ท ๐งพ (โ๏ธ ๐ ๐ป ๐). |
|||
|
|||
**FastAPI** โคด๏ธ โ ๐ ๐ป ๐ ๐ฝ & ๐ฎ โซ๏ธ ๐, โ๏ธ โช๏ธโก๏ธ ๐ ๐ ๐ โซ๏ธ ๐จ. |
|||
|
|||
### <a href="https://www.starlette.io/" class="external-link" target="_blank">๐</a> |
|||
|
|||
๐ ๐ฟ <abbr title="The new standard for building asynchronous Python web">๐ซ</abbr> ๐ ๏ธ/๐งฐ, โ ๐ฏ ๐ โ-๐ญ โณ ๐โ๐ฆบ. |
|||
|
|||
โซ๏ธ ๐ถ ๐
& ๐๏ธ. โซ๏ธ ๐ง ๐ช ๐ง, & โ๏ธ ๐ง ๐ฆฒ. |
|||
|
|||
โซ๏ธ โ๏ธ: |
|||
|
|||
* ๐ค ๐ ๐ญ. |
|||
* *๏ธโฃ ๐โ๐ฆบ. |
|||
* -๐ ๏ธ ๐ฅ ๐. |
|||
* ๐ด & ๐คซ ๐. |
|||
* ๐ฏ ๐ฉโ๐ป ๐ ๐ ๐ธ๐ฒ. |
|||
* โ, ๐, ๐ป ๐, ๐ ๐จ. |
|||
* ๐ & ๐ช ๐โ๐ฆบ. |
|||
* 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ ๐ฏ ๐ฐ. |
|||
* 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ ๐ โ โ. |
|||
* ๐ฉโโคโ๐จ ๐๏ธ ๐. |
|||
|
|||
๐ โณ โฉ ๐ ๐ ๏ธ ๐ฏ. ๐ด ๐ฅ Uvicorn, โ ๐ซ ๐ ๏ธ, โ๏ธ ๐ฝ. |
|||
|
|||
๐ ๐ ๐ ๐ฐ ๐ธ ๐ธ ๐ ๏ธ. |
|||
|
|||
โ๏ธ โซ๏ธ ๐ซ ๐ ๐ง ๐ฝ ๐ฌ, ๐ ๏ธ โ๏ธ ๐งพ. |
|||
|
|||
๐ 1๏ธโฃ ๐ ๐ ๐ **FastAPI** ๐ฎ ๐ ๐, ๐ โ๏ธ ๐ ๐ ๐ ๐ (โ๏ธ Pydantic). ๐, โ ๐ ๐ โ๏ธ, ๐โโ ๐, ๐ ๐ โก, โ๏ธ. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ซ ๐ "๐ฉ" โ ๐ ๏ธ โณ ๐ ๐ ๐จโ๐. โซ๏ธ ๐ซ "๐ ๐ฉ" (๐ฉ๐ฌ), ๐ ๐ซ ๐ ๏ธ ๐จ ๐. |
|||
|
|||
๐, โซ๏ธ โช โ โ๏ธ "๐ฉ" ๐ ๐งฐ. ๐ ๐ ๐ ๐ ๏ธ, ๐ ๐ช ๐ Uvicorn ๐ ๐ ๐ซ ๐ฝ (๐ ๐ธ โ๏ธ Hypercorn), โ๏ธ ๐ ๐ช ๐ฎ ๐ซ ๐ ๐งฐ, ๐ `python-socketio`. |
|||
|
|||
!!! check "**FastAPI** โ๏ธ โซ๏ธ" |
|||
๐ต ๐ ๐ ๐ธ ๐. โ โ ๐ ๐. |
|||
|
|||
๐ `FastAPI` โซ๏ธ ๐ ๐ โช๏ธโก๏ธ ๐ `Starlette`. |
|||
|
|||
, ๐ณ ๐ ๐ ๐ช โฎ๏ธ ๐, ๐ ๐ช โซ๏ธ ๐ โฎ๏ธ **FastAPI**, โซ๏ธ ๐ ๐ ๐ ๐. |
|||
|
|||
### <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a> |
|||
|
|||
Uvicorn ๐ฉ-โฉ ๐ซ ๐ฝ, ๐ ๐ uvloop & httptool. |
|||
|
|||
โซ๏ธ ๐ซ ๐ธ ๐ ๏ธ, โ๏ธ ๐ฝ. ๐ผ, โซ๏ธ ๐ซ ๐ ๐งฐ ๐น โก. ๐ ๐ณ ๐ ๐ ๏ธ ๐ ๐ (โ๏ธ **FastAPI**) ๐ ๐ ๐ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ฝ ๐ & **FastAPI**. |
|||
|
|||
!!! check "**FastAPI** ๐ โซ๏ธ" |
|||
๐ ๐ธ ๐ฝ ๐ **FastAPI** ๐ธ. |
|||
|
|||
๐ ๐ช ๐ โซ๏ธ โฎ๏ธ ๐, โ๏ธ ๐ ๐-๐ ๏ธ ๐ฝ. |
|||
|
|||
โ
๐
โน [๐ ๏ธ](deployment/index.md){.internal-link target=_blank} ๐. |
|||
|
|||
## ๐ & ๐
|
|||
|
|||
๐ค, ๐ฌ, & ๐ ๐บ ๐ Uvicorn, ๐ & FastAPI, โ
๐ ๐ [๐](benchmarks.md){.internal-link target=_blank}. |
@ -0,0 +1,430 @@ |
|||
# ๐ ๏ธ & ๐ / โ |
|||
|
|||
โน ๐ `async def` โ *โก ๐ ๏ธ ๐ข* & ๐ฅ ๐ ๐ ๐, ๐ ๏ธ, & ๐. |
|||
|
|||
## ๐ โ |
|||
|
|||
<abbr title="too long; didn't read"><strong>๐;๐ฉโโ๏ธ:</strong></abbr> |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ฅ ๐ฅณ ๐ ๐ ๐ฌ ๐ ๐ค ๐ซ โฎ๏ธ `await`, ๐: |
|||
|
|||
```Python |
|||
results = await some_library() |
|||
``` |
|||
|
|||
โคด๏ธ, ๐ฃ ๐ *โก ๐ ๏ธ ๐ข* โฎ๏ธ `async def` ๐: |
|||
|
|||
```Python hl_lines="2" |
|||
@app.get('/') |
|||
async def read_results(): |
|||
results = await some_library() |
|||
return results |
|||
``` |
|||
|
|||
!!! note |
|||
๐ ๐ช ๐ด โ๏ธ `await` ๐ ๐ข โ โฎ๏ธ `async def`. |
|||
|
|||
--- |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ฅ ๐ฅณ ๐ ๐ ๐ โฎ๏ธ ๐ณ (๐ฝ, ๐ ๏ธ, ๐ โ๏ธ, โ๏ธ.) & ๐ซ โ๏ธ ๐โ๐ฆบ โ๏ธ `await`, (๐ โณ ๐ผ ๐
๐ฝ ๐), โคด๏ธ ๐ฃ ๐ *โก ๐ ๏ธ ๐ข* ๐, โฎ๏ธ `def`, ๐: |
|||
|
|||
```Python hl_lines="2" |
|||
@app.get('/') |
|||
def results(): |
|||
results = some_library() |
|||
return results |
|||
``` |
|||
|
|||
--- |
|||
|
|||
๐ฅ ๐ ๐ธ (๐ซ) ๐ซ โ๏ธ ๐ โฎ๏ธ ๐ณ ๐ & โ โซ๏ธ ๐จ, โ๏ธ `async def`. |
|||
|
|||
--- |
|||
|
|||
๐ฅ ๐ ๐ซ ๐ญ, โ๏ธ ๐ `def`. |
|||
|
|||
--- |
|||
|
|||
**๐**: ๐ ๐ช ๐ `def` & `async def` ๐ *โก ๐ ๏ธ ๐ข* ๐
๐ ๐ช & ๐ฌ ๐ 1๏ธโฃ โ๏ธ ๐ ๐ ๐. FastAPI ๐ โถ๏ธ๏ธ ๐ โฎ๏ธ ๐ซ. |
|||
|
|||
๐, ๐ ๐ผ ๐, FastAPI ๐ ๐ท ๐ & ๐ถ โฉ. |
|||
|
|||
โ๏ธ ๐ ๐ถ ๐, โซ๏ธ ๐ ๐ช ๐ญ ๐ ๏ธ. |
|||
|
|||
## ๐ก โน |
|||
|
|||
๐ โฌ ๐ โ๏ธ ๐โ๐ฆบ **"๐ ๐"** โ๏ธ ๐ณ ๐ค **"๐"**, โฎ๏ธ **`async` & `await`** โ. |
|||
|
|||
โก๏ธ ๐ ๐ ๐ค ๐ ๐ ๐: |
|||
|
|||
* **๐ ๐** |
|||
* **`async` & `await`** |
|||
* **๐** |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ ๐ โ ๐ ๐ช๐ธ ๐ถ โ๏ธ ๐ ๐ฌ ๐ป / ๐ ๐ถ ๐ โ ๐, โซ๏ธ ๐ถ ๐ โ๏ธ โ *๐ณ ๐* ๐ ๐ฑ ๐. โก๏ธ ๐ฌ ๐ *๐ณ ๐* ๐ค "๐-๐" ๐ถ. |
|||
|
|||
, โฎ๏ธ ๐ ๐ฐ, ๐ป ๐ช ๐ถ & ๐ ๐ท, โช "๐-๐" ๐ถ ๐. |
|||
|
|||
โคด๏ธ ๐ป / ๐ ๐ถ ๐ ๐ ๐ ๐ ๐ฐ โซ๏ธ โ๏ธ ๐ค โฉ๏ธ โซ๏ธ โ ๐, โ๏ธ ๐โ โซ๏ธ ๐ถ ๐ ๐ ๐ท โซ๏ธ โ๏ธ ๐ โ. & โซ๏ธ ๐ถ ๐ ๐ ๐ฅ ๐ ๐ โซ๏ธ โ โ๏ธ โช ๐, ๐คธ โซ๏ธโ โซ๏ธ โ๏ธ. |
|||
|
|||
โญ, โซ๏ธ ๐ถ โ ๐ฅ ๐ ๐ (โก๏ธ ๐ฌ, ๐ "๐-๐" ๐ถ) & ๐ฃ โซ๏ธโ โซ๏ธ โ๏ธ โฎ๏ธ โซ๏ธ. |
|||
|
|||
๐ "โ ๐ณ ๐" ๐ ๐ <abbr title="Input and Output">๐ค/๐
พ</abbr> ๐ ๏ธ ๐ ๐ถ "๐" (๐ฌ ๐
๐น & ๐พ ๐พ), ๐ โ: |
|||
|
|||
* ๐ โช๏ธโก๏ธ ๐ฉโ๐ป ๐จ ๐ ๐ธ |
|||
* ๐ ๐จ ๐ ๐ ๐จ ๐ฉโ๐ป ๐ ๐ธ |
|||
* ๐ ๐ ๐พ โ โ๏ธ & ๐ค ๐ ๐ |
|||
* ๐ ๐ ๐ ๐ค โ๏ธ โ ๐พ |
|||
* ๐ฐ ๐ ๏ธ ๐ ๏ธ |
|||
* ๐ฝ ๐ ๏ธ ๐ |
|||
* ๐ฝ ๐ข ๐จ ๐ |
|||
* โ๏ธ. |
|||
|
|||
๐ ๏ธ ๐ฐ ๐ด โด๏ธ โ <abbr title="Input and Output">๐ค/๐
พ</abbr> ๐ ๏ธ, ๐ซ ๐ค ๐ซ "๐ค/๐
พ ๐" ๐ ๏ธ. |
|||
|
|||
โซ๏ธ ๐ค "๐" โฉ๏ธ ๐ป / ๐ ๐ซ โ๏ธ "๐" โฎ๏ธ ๐ ๐, โ โ ๐ ๐ ๐ ๐, โช ๐จ ๐ณ, ๐ช โ ๐ ๐ & ๐ฃ ๐ท. |
|||
|
|||
โฉ๏ธ ๐, ๐โโ "๐" โ๏ธ, ๐ ๐, ๐ ๐ช โ โธ ๐ฅ ๐ (โฒ) ๐ป / ๐ ๐ โซ๏ธโ โซ๏ธ ๐ถ, & โคด๏ธ ๐ ๐ โ ๐ & ๐ฃ ๐ท โฎ๏ธ ๐ซ. |
|||
|
|||
"๐" (๐ฝ "๐") ๐ซ ๐ โ๏ธ โ "๐", โฉ๏ธ ๐ป / ๐ โฉ ๐ ๐ถ ๐ โญ ๐ ๐ ๐, ๐ฅ ๐ ๐ ๐ โ. |
|||
|
|||
### ๐ ๏ธ & ๐ |
|||
|
|||
๐ ๐ญ **๐** ๐ ๐ฌ ๐ ๐ฃ ๐ค **"๐ ๏ธ"**. โซ๏ธ ๐ โช๏ธโก๏ธ **"๐"**. |
|||
|
|||
**๐ ๏ธ** & **๐** ๐ฏโโ๏ธ ๐ "๐ ๐ ๐ฅ ๐
โ๏ธ ๐ ๐ ๐ฐ". |
|||
|
|||
โ๏ธ โน ๐ *๐ ๏ธ* & *๐* ๐. |
|||
|
|||
๐ ๐บ, ๐ ๐ ๐ ๐ ๐: |
|||
|
|||
### ๐ ๏ธ ๐ |
|||
|
|||
๐ ๐ถ โฎ๏ธ ๐ ๐ฅฐ ๐ค โฉ ๐ฅ, ๐ ๐ง โธ โช ๐ง โ โ โช๏ธโก๏ธ ๐ซ๐ซ ๐ช ๐. ๐ถ |
|||
|
|||
<img src="/img/async/concurrent-burgers/concurrent-burgers-01.png" class="illustration"> |
|||
|
|||
โคด๏ธ โซ๏ธ ๐ ๐, ๐ ๐ฅ ๐ โ 2๏ธโฃ ๐ถ ๐ ๐ ๐ ๐ฅฐ & ๐. ๐ถ ๐ถ |
|||
|
|||
<img src="/img/async/concurrent-burgers/concurrent-burgers-02.png" class="illustration"> |
|||
|
|||
๐ง ๐ฌ ๐ณ ๐ณ ๐จโ๐ณ ๐ซ ๐ญ ๐ซ โ๏ธ ๐ ๐ ๐ (โ๏ธ ๐ซ โณ ๐ ๐ โฎ๏ธ ๐ฉโ๐ป). |
|||
|
|||
<img src="/img/async/concurrent-burgers/concurrent-burgers-03.png" class="illustration"> |
|||
|
|||
๐ ๐ธ. ๐ถ |
|||
|
|||
๐ง ๐ค ๐ ๐ข ๐ ๐. |
|||
|
|||
<img src="/img/async/concurrent-burgers/concurrent-burgers-04.png" class="illustration"> |
|||
|
|||
โช ๐ โ, ๐ ๐ถ โฎ๏ธ ๐ ๐ฅฐ & โ ๐, ๐ ๐ง & ๐ฌ โฎ๏ธ ๐ ๐ฅฐ ๐ ๐ฐ (๐ ๐ ๐ถ ๐ & โ ๐ฐ ๐). |
|||
|
|||
๐ ๐ ๐ โฎ๏ธ ๐ ๐ฅฐ, โช ๐ โ ๐, ๐ ๐ช ๐ธ ๐ ๐ฐ ๐ฎ โ ๐, ๐จ & ๐ ๐ ๐ฅฐ ๐ถ ๐ถ ๐ถ. |
|||
|
|||
<img src="/img/async/concurrent-burgers/concurrent-burgers-05.png" class="illustration"> |
|||
|
|||
โช โ & ๐ฌ ๐ ๐ฅฐ, โช๏ธโก๏ธ ๐ฐ ๐ฐ, ๐ โ
๐ข ๐ฅ ๐ โฒ ๐ ๐ฅ โซ๏ธ ๐ ๐ โช. |
|||
|
|||
โคด๏ธ โ, โซ๏ธ ๐ ๐ ๐. ๐ ๐ถ โฒ, ๐ค ๐ ๐ & ๐ ๐ ๐. |
|||
|
|||
<img src="/img/async/concurrent-burgers/concurrent-burgers-06.png" class="illustration"> |
|||
|
|||
๐ & ๐ ๐ฅฐ ๐ด ๐ & โ๏ธ ๐ ๐ฐ. ๐ถ |
|||
|
|||
<img src="/img/async/concurrent-burgers/concurrent-burgers-07.png" class="illustration"> |
|||
|
|||
!!! info |
|||
๐น ๐ผ <a href="https://www.instagram.com/ketrinadrawsalot" class="external-link" target="_blank">๐ฏ ๐</a>. ๐ถ |
|||
|
|||
--- |
|||
|
|||
๐ ๐ ๐ป / ๐ ๐ถ ๐ ๐. |
|||
|
|||
โช ๐ โธ, ๐ โฝ ๐ถ, โ ๐ ๐, ๐ซ ๐จ ๐ณ ๐ถ "๐". โ๏ธ โธ โฉ โฉ๏ธ ๐ง ๐ด โ โ (๐ซ ๐ ๐ซ), ๐ ๐. |
|||
|
|||
โคด๏ธ, ๐โ โซ๏ธ ๐ ๐, ๐ โ "๐" ๐ท, ๐ ๐ ๏ธ ๐ฃ, ๐ญ โซ๏ธโ ๐ ๐, ๐ค ๐ ๐ฅฐ โ, ๐ธ, โ
๐ ๐ ๐ค โ ๐ต โ๏ธ ๐ณ, โ
๐ ๐ ๐ โ, โ
๐ โ โ๏ธ โ ๐ฌ, โ๏ธ. |
|||
|
|||
โ๏ธ โคด๏ธ, โ๏ธ ๐ ๐ซ โ๏ธ ๐ ๐, ๐ ๐ท โฎ๏ธ ๐ง "๐ โธ" โธ, โฉ๏ธ ๐ โ๏ธ โ ๐ถ ๐ ๐ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ถ โ๏ธ โช๏ธโก๏ธ โฒ & ๐ง ๐ โฎ๏ธ ๐ข ๐ ๐, ๐ ๐ช ๐ ๐ถ ๐ ๐ ๐ ๐ฅฐ, & "๐ท" ๐ถ ๐ถ ๐ ๐. โคด๏ธ ๐ ๐ ๐จ ๐ณ ๐ถ "๐" ๐ โฎ๏ธ ๐ ๐ฅฐ ๐ถ. |
|||
|
|||
โคด๏ธ ๐ง ๐ถ ๐ฌ "๐ค ๐ โฎ๏ธ ๐จ ๐" ๐ฎ ๐ ๐ข ๐ โฒ ๐ฅ, โ๏ธ ๐ ๐ซ ๐ฆ ๐ ๐ โช ๐โ ๐ฅ ๐ข ๐ ๐ ๐ ๐ข. ๐ ๐ญ ๐
โโ 1๏ธโฃ ๐ ๐ ๐ ๐ โฉ๏ธ ๐ โ๏ธ ๐ข ๐ ๐, & ๐ซ โ๏ธ ๐ซ. |
|||
|
|||
๐ โ ๐ ๐ฅฐ ๐ ๐ (๐ โฎ๏ธ ๐ท ๐ถ / ๐ โ ๐ ๏ธ ๐ถ), ๐ ๐ & ๐ฌ ๐ ๐ ๐ ๐ โธ. |
|||
|
|||
โคด๏ธ ๐ ๐ถ โฒ ๐ถ, โถ๏ธ ๐ ๐ ๐ ๐ ๐ถ, โ ๐, ๐ฌ ๐ & โ ๐ซ ๐. ๐ ๐ ๐ ๐ / ๐ ๐ โฎ๏ธ โฒ โน. ๐ ๐, โ ๐ ๐, "๐ด ๐" ๐ถ ๐ถ, โ๏ธ โฎ๏ธ 1๏ธโฃ "๐ค ๐" ๐ โน. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ โก๏ธ ๐ ๐ซ โ๐ซ ๐ซ "๐ ๏ธ ๐", โ๏ธ "๐ ๐". |
|||
|
|||
๐ ๐ถ โฎ๏ธ ๐ ๐ฅฐ ๐ค ๐ โฉ ๐ฅ. |
|||
|
|||
๐ ๐ง โธ โช ๐ (โก๏ธ ๐ฌ 8๏ธโฃ) ๐ง ๐ ๐ ๐ฐ ๐ณ โ โ โช๏ธโก๏ธ ๐ซ๐ซ ๐ช ๐. |
|||
|
|||
๐ฑ โญ ๐ โ ๐ซ ๐ ๐ โญ ๐ โฒ โฉ๏ธ ๐ 8๏ธโฃ ๐ง ๐ถ & ๐ ๐ โถ๏ธ๏ธ โ๏ธ โญ ๐โโ โญ โ. |
|||
|
|||
<img src="/img/async/parallel-burgers/parallel-burgers-01.png" class="illustration"> |
|||
|
|||
โคด๏ธ โซ๏ธ ๐ ๐ ๐, ๐ ๐ฅ ๐ โ 2๏ธโฃ ๐ถ ๐ ๐ ๐ ๐ฅฐ & ๐. |
|||
|
|||
๐ ๐ธ ๐ถ. |
|||
|
|||
<img src="/img/async/parallel-burgers/parallel-burgers-02.png" class="illustration"> |
|||
|
|||
๐ง ๐ถ ๐จโ๐ณ. |
|||
|
|||
๐ โ, ๐ง ๐ช โฒ ๐ถ, ๐ ๐
โโ 1๏ธโฃ ๐ โ ๐ ๐ โญ ๐, ๐ค ๐
โโ ๐ข ๐. |
|||
|
|||
<img src="/img/async/parallel-burgers/parallel-burgers-03.png" class="illustration"> |
|||
|
|||
๐ & ๐ ๐ฅฐ ๐ฉ ๐ซ โก๏ธ ๐ ๐ค ๐ช ๐ & โ ๐ ๐ ๐โ ๐ซ ๐ฌ, ๐ ๐ซ๐ ๐ธ ๐ ๐ ๐ฅฐ. ๐ถ |
|||
|
|||
๐ "๐" ๐ท, ๐ "๐" โฎ๏ธ ๐ง/๐ณ ๐ถ ๐ถ. ๐ โ๏ธ โ ๐ถ & ๐ค โ ๐ ๐ ๐ง/๐ณ ๐ถ ๐ถ ๐ ๐ & ๐ค ๐ซ ๐, โ๏ธ โช, ๐ฑ ๐ ๐ช โ ๐ซ. |
|||
|
|||
<img src="/img/async/parallel-burgers/parallel-burgers-04.png" class="illustration"> |
|||
|
|||
โคด๏ธ ๐ ๐ง/๐ณ ๐ถ ๐ถ ๐ ๐ ๐ โฎ๏ธ ๐ ๐, โฎ๏ธ ๐ ๐ฐ โ ๐ถ ๐ค ๐ช โฒ. |
|||
|
|||
<img src="/img/async/parallel-burgers/parallel-burgers-05.png" class="illustration"> |
|||
|
|||
๐ โ ๐ ๐ & ๐ถ ๐ โฎ๏ธ ๐ ๐ฅฐ. |
|||
|
|||
๐ ๐ด ๐ซ, & ๐ ๐จ. โน |
|||
|
|||
<img src="/img/async/parallel-burgers/parallel-burgers-06.png" class="illustration"> |
|||
|
|||
๐ค ๐ซ ๐
๐ฌ โ๏ธ ๐ ๐
๐ฐ ๐ธ โ ๐ถ ๐ช โฒ. ๐ถ |
|||
|
|||
!!! info |
|||
๐น ๐ผ <a href="https://www.instagram.com/ketrinadrawsalot" class="external-link" target="_blank">๐ฏ ๐</a>. ๐ถ |
|||
|
|||
--- |
|||
|
|||
๐ ๐ ๐ ๐, ๐ ๐ป / ๐ ๐ถ โฎ๏ธ 2๏ธโฃ ๐น (๐ & ๐ ๐ฅฐ), ๐ฏโโ๏ธ โ ๐ถ & ๐ก ๐ซ ๐ ๐ถ "โ ๐ โฒ" ๐ถ ๐ ๐ฐ. |
|||
|
|||
โฉ ๐ฅ ๐ช โ๏ธ 8๏ธโฃ ๐น (๐ง/๐ณ). โช ๐ ๏ธ ๐ ๐ช ๐ช โ๏ธ โ๏ธ ๐ด 2๏ธโฃ (1๏ธโฃ ๐ง & 1๏ธโฃ ๐ณ). |
|||
|
|||
โ๏ธ, ๐ ๐ก ๐ซ ๐. ๐ถ |
|||
|
|||
--- |
|||
|
|||
๐ ๐ ๐ ๐ ๐ ๐. ๐ถ |
|||
|
|||
๐
"๐ฐ ๐จโโคโ๐จ" ๐ผ ๐, ๐ ๐ฆ. |
|||
|
|||
๐ โณ, ๐ ๐ฆ โ๏ธ ๐ ๐ง ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ & ๐ฆ โธ ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ ๐ถ. |
|||
|
|||
๐ ๐ง ๐จ ๐ ๐ท โฎ๏ธ 1๏ธโฃ ๐ฉโ๐ป โฎ๏ธ ๐ ๐ถ ๐ถ ๐ถ. |
|||
|
|||
& ๐ โ๏ธ โ ๐ถ โธ ๐ ๐ฐ โ๏ธ ๐ ๐ธ ๐ ๐. |
|||
|
|||
๐ ๐ฒ ๐ซ๐ ๐ โ ๐ ๐ฅฐ ๐ถ โฎ๏ธ ๐ ๐ท ๐ฆ ๐ถ. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ ๐ "โฉ ๐ฅ ๐ โฎ๏ธ ๐ ๐ฅฐ", ๐ค ๐ โ ๐ถ, โซ๏ธ โ ๐ ๐
๐ โ๏ธ ๐ ๏ธ โ๏ธ โธ ๐ถ ๐ถ. |
|||
|
|||
๐ ๐ผ ๐
๐ธ ๐ธ. |
|||
|
|||
๐, ๐ ๐ฉโ๐ป, โ๏ธ ๐ ๐ฝ โ ๐ถ ๐ซ ๐ซ--๐ ๐ ๐จ ๐ซ ๐จ. |
|||
|
|||
& โคด๏ธ โ ๐ถ ๐ ๐จ ๐ ๐. |
|||
|
|||
๐ "โ" ๐ถ โ โฒ, โ๏ธ, โ โซ๏ธ ๐, โซ๏ธ ๐ โ ๐. |
|||
|
|||
๐ โซ๏ธโ โซ๏ธ โ ๐ ๐ โ๏ธ ๐ โธ ๐ถ ๐ถ ๐ ๐ธ ๐. |
|||
|
|||
๐ ๐ ๐ โซ๏ธโ โ โณ ๐ (โ๏ธ โณ ๐ซ ๐) & ๐ ๐ช ๐ถ ๐ ๏ธ ๐ช๐ธ. |
|||
|
|||
& ๐ ๐ ๐ ๐ญ ๐ ๐ค โฎ๏ธ **FastAPI**. |
|||
|
|||
& ๐ ๐ช โ๏ธ ๐ & ๐ ๐ ๐ฐ, ๐ ๐ค โ ๐ญ ๐ ๐
๐ฏ โณ ๐ ๏ธ & ๐ ๐ท๐ช โฎ๏ธ ๐ถ, โ โ ๐ช๐ธ ๐ ๐
ฑ <a href="https://www.techempower.com/benchmarks/#section=data-r17&hw=ph&test=query&l=zijmkf-1" class="external-link" target="_blank">(๐ ๐ ๐)</a>. |
|||
|
|||
### ๐ ๏ธ ๐ ๐ ๐ โ |
|||
|
|||
๐ โ ๐ ๐ซ ๐ ๐. |
|||
|
|||
๐ ๏ธ ๐ ๐ ๐. & โซ๏ธ ๐ป ๐ **๐ฏ** ๐ ๐ ๐ ๐ โ. โฉ๏ธ ๐, โซ๏ธ ๐ ๐ ๐ ๐ ๐ ๐ธ ๐ธ ๐ ๏ธ. โ๏ธ ๐ซ ๐. |
|||
|
|||
, โ ๐ ๐
, ๐ ๐ ๐ ๐: |
|||
|
|||
> ๐ โ๏ธ ๐งน ๐ฆ, ๐ฉ ๐ . |
|||
|
|||
*๐, ๐ ๐ ๐*. |
|||
|
|||
--- |
|||
|
|||
๐ค ๐
โโ โ ๐ถ ๐, ๐ ๐ท ๐จ, ๐ ๐ ๐ฅ ๐ . |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ ๐ผ, ๐ฅ ๐ ๐งโโ, โคด๏ธ ๐จโ๐ณ, โ๏ธ ๐ ๐ซ โ ๐ถ ๐ณ, ๐งน & ๐งน, ๐ ๐ซ๐ ๐ ๐ณ. |
|||
|
|||
โซ๏ธ ๐ โ ๐ ๐ธ ๐ฐ ๐ โฎ๏ธ โ๏ธ ๐ต ๐ (๐ ๏ธ) & ๐ ๐ โ๏ธ โ ๐ ๐ธ ๐ท. |
|||
|
|||
โ๏ธ ๐ ๐ผ, ๐ฅ ๐ ๐ช โ๏ธ 8๏ธโฃ ๐ฐ-๐ง/๐ณ/๐-๐งน, & ๐ 1๏ธโฃ ๐ซ (โ ๐) ๐ช โ ๐ ๐ ๐งน โซ๏ธ, ๐ ๐ช ๐ ๐ท **๐**, โฎ๏ธ โ โน, & ๐ ๐
๐. |
|||
|
|||
๐ ๐, ๐ 1๏ธโฃ ๐งน (๐ ๐) ๐ ๐น, ๐คธ ๐ซ ๐ ๐จโ๐ญ. |
|||
|
|||
& ๐ ๐ ๏ธ ๐ฐ โ โ ๐ท (โฉ๏ธ โ), & ๐ท ๐ป โ <abbr title="Central Processing Unit">๐ฝ</abbr>, ๐ซ ๐ค ๐ซ โ "๐ฝ ๐". |
|||
|
|||
--- |
|||
|
|||
โ ๐ผ ๐ฝ ๐ ๐ ๏ธ ๐ ๐ ๐ ๐ ๐งช ๐ญ. |
|||
|
|||
๐ผ: |
|||
|
|||
* **๐ง** โ๏ธ **๐ผ ๐ญ**. |
|||
* **๐ป ๐**: ๐ผ โ ๐ฏ ๐
, ๐ ๐
โ๏ธ 3๏ธโฃ ๐ฒ / ๐จ, ๐ญ ๐ ๐ ๐ ๐ป ๐ณ ๐ ๐ ๐
, ๐ ๐ ๐ฐ. |
|||
* **๐ฐ ๐ซ**: โซ๏ธ ๐ ๐ ๐ "โ" & "๐ผ" โ. ๐ญ ๐ฆ ๐ โฎ๏ธ ๐ข & โ ๐ ๐ซ ๐ฏโโ๏ธ ๐ ๐ฐ. |
|||
* **โฌ ๐ซ**: ๐ ๐ง-๐ ๐ฐ ๐ซ,, ๐ โ. โซ๏ธ ๐ ๐ค ๐ซ ๐ ๐ ๐ข โ, โ๏ธ ๐ฆ โ ๐ซ, & ๐ ๐ผ, ๐ โ๏ธ ๐ ๐น ๐ & / โ๏ธ โ๏ธ ๐ ๐ท. |
|||
|
|||
### ๐ ๏ธ โ ๐: ๐ธ โ ๐ฐ ๐ซ |
|||
|
|||
โฎ๏ธ **FastAPI** ๐ ๐ช โ ๐ ๐ ๏ธ ๐ ๐ถ โ ๐ธ ๐ ๏ธ (๐ ๐ ๐งฒ โณ). |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ ๐ฐ ๐ & ๐พ (โ๏ธ ๐ ๐ ๏ธ ๐โโ ๐) **๐ฝ ๐** โ ๐ ๐ ๐ฐ ๐ซ โ๏ธ. |
|||
|
|||
๐, โ ๐
๐ ๐ ๐ ๐ ๐ช๐ธ **๐ฝ ๐งช**, ๐ฐ ๐ซ & โด๏ธ โฌ ๐ซ, โ FastAPI ๐ถ ๐ ๐ ๐ฝ ๐งช / ๐ฐ ๐ซ ๐ธ ๐ & ๐ธ (๐ช ๐ ๐). |
|||
|
|||
๐ โ ๐ ๐ ๐ ๐ญ ๐ ๐ ๐ [๐ ๏ธ](deployment/index.md){.internal-link target=_blank}. |
|||
|
|||
## `async` & `await` |
|||
|
|||
๐ โฌ ๐ โ๏ธ ๐ถ ๐๏ธ ๐ ๐ฌ ๐ ๐. ๐ โ โซ๏ธ ๐ ๐ ๐ "๐" ๐ & "โ" ๐ โถ๏ธ๏ธ ๐. |
|||
|
|||
๐โ ๐ค ๐ ๏ธ ๐ ๐ ๐ โ โญ ๐ค ๐ & โ๏ธ ๐โ๐ฆบ ๐ ๐ ๐ โ, ๐ ๐ช ๐ โซ๏ธ ๐: |
|||
|
|||
```Python |
|||
burgers = await get_burgers(2) |
|||
``` |
|||
|
|||
๐ ๐ฅ `await`. โซ๏ธ ๐ฌ ๐ ๐ โซ๏ธ โ๏ธ โ โธ `get_burgers(2)` ๐ ๐จ ๐ฎ ๐ ๐ถ โญ โป ๐ `burgers`. โฎ๏ธ ๐, ๐ ๐ ๐ญ ๐ โซ๏ธ ๐ช ๐ถ & ๐ณ ๐ ๐ถ ๐ถ ๐ (๐ ๐จ โ1๏ธโฃ ๐จ). |
|||
|
|||
`await` ๐ท, โซ๏ธ โ๏ธ ๐ ๐ข ๐ ๐โ๐ฆบ ๐ ๐. ๐, ๐ ๐ฃ โซ๏ธ โฎ๏ธ `async def`: |
|||
|
|||
```Python hl_lines="1" |
|||
async def get_burgers(number: int): |
|||
# Do some asynchronous stuff to create the burgers |
|||
return burgers |
|||
``` |
|||
|
|||
...โฉ๏ธ `def`: |
|||
|
|||
```Python hl_lines="2" |
|||
# This is not asynchronous |
|||
def get_sequential_burgers(number: int): |
|||
# Do some sequential stuff to create the burgers |
|||
return burgers |
|||
``` |
|||
|
|||
โฎ๏ธ `async def`, ๐ ๐ญ ๐, ๐ ๐ ๐ข, โซ๏ธ โ๏ธ ๐ค `await` ๐งฌ, & ๐ โซ๏ธ ๐ช "โธ" โธ ๐ ๏ธ ๐ ๐ข & ๐ถ ๐ณ ๐ ๐ถ โญ ๐ ๐. |
|||
|
|||
๐โ ๐ ๐ ๐ค `async def` ๐ข, ๐ โ๏ธ "โ" โซ๏ธ. , ๐ ๐ ๐ซ ๐ท: |
|||
|
|||
```Python |
|||
# This won't work, because get_burgers was defined with: async def |
|||
burgers = get_burgers(2) |
|||
``` |
|||
|
|||
--- |
|||
|
|||
, ๐ฅ ๐ โ๏ธ ๐ ๐ ๐ฌ ๐ ๐ ๐ ๐ช ๐ค โซ๏ธ โฎ๏ธ `await`, ๐ ๐ช โ *โก ๐ ๏ธ ๐ข* ๐ โ๏ธ โซ๏ธ โฎ๏ธ `async def`, ๐: |
|||
|
|||
```Python hl_lines="2-3" |
|||
@app.get('/burgers') |
|||
async def read_burgers(): |
|||
burgers = await get_burgers(2) |
|||
return burgers |
|||
``` |
|||
|
|||
### ๐
๐ก โน |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ `await` ๐ช ๐ด โ๏ธ ๐ ๐ข ๐ฌ โฎ๏ธ `async def`. |
|||
|
|||
โ๏ธ ๐ ๐ฐ, ๐ข ๐ฌ โฎ๏ธ `async def` โ๏ธ "โ". , ๐ข โฎ๏ธ `async def` ๐ช ๐ด ๐ค ๐ ๐ข ๐ฌ โฎ๏ธ `async def` ๐โโ๏ธ. |
|||
|
|||
, ๐ ๐ฅ & ๐, โ ๐ ๐ค ๐ฅ `async` ๐ข โ |
|||
|
|||
๐ฅ ๐ ๐ท โฎ๏ธ **FastAPI** ๐ ๐ซ โ๏ธ ๐ ๐ ๐, โฉ๏ธ ๐ "๐ฅ" ๐ข ๐ ๐ *โก ๐ ๏ธ ๐ข*, & FastAPI ๐ ๐ญ โ โถ๏ธ๏ธ ๐. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ โ๏ธ `async` / `await` ๐ต FastAPI, ๐ ๐ช โซ๏ธ ๐. |
|||
|
|||
### โ ๐ ๐ ๐ ๐ |
|||
|
|||
๐ (& **FastAPI**) โ๏ธ ๐ <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a>, โ โ โซ๏ธ ๐ โฎ๏ธ ๐ฏโโ๏ธ ๐ ๐ฉ ๐ <a href="https://docs.python.org/3/library/asyncio-task.html" class="external-link" target="_blank">โณ</a> & <a href="https://trio.readthedocs.io/en/stable/" class="external-link" target="_blank">๐ป</a>. |
|||
|
|||
๐ฏ, ๐ ๐ช ๐ โ๏ธ <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a> ๐ ๐ง ๐ ๏ธ โ๏ธ ๐ผ ๐ ๐ ๐
๐ง โ ๐ ๐ ๐. |
|||
|
|||
& ๐ฅ ๐ ๐ซ โ๏ธ FastAPI, ๐ ๐ช โ ๐ ๐ ๐ ๐ธ โฎ๏ธ <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a> ๐ ๐ & ๐ค ๐ฎ ๐ฐ (โ
*๐ ๐ ๏ธ*). |
|||
|
|||
### ๐ ๐จ ๐ ๐ |
|||
|
|||
๐ ๐ โ๏ธ `async` & `await` ๐ถ ๐ ๐ช๐ธ. |
|||
|
|||
โ๏ธ โซ๏ธ โ ๐ท โฎ๏ธ ๐ ๐ ๐ โฉ. |
|||
|
|||
๐ ๐ โ (โ๏ธ ๐ ๐) ๐ โณ ๐ โฌ ๐ธ (๐ฅ & โณ). |
|||
|
|||
โ๏ธ โญ ๐, ๐ ๐ ๐ ๐ ๐ & โ . |
|||
|
|||
โฎ๏ธ โฌ ๐, ๐ ๐ช โ๏ธ โ๏ธ ๐งต โ๏ธ <a href="https://www.gevent.org/" class="external-link" target="_blank">๐</a>. โ๏ธ ๐ ๐ ๐ ๐ ๐ค, โน, & ๐ญ ๐. |
|||
|
|||
โฎ๏ธ โฌ โณ / ๐ฅ ๐ธ, ๐ ๐ โ๏ธ โ๏ธ "โฒ". โ โ๏ธ <a href="http://callbackhell.com/" class="external-link" target="_blank">โฒ ๐ฅ๐</a>. |
|||
|
|||
## ๐ |
|||
|
|||
**๐** ๐ถ ๐ โ ๐ ๐จ `async def` ๐ข. ๐ ๐ญ ๐ โซ๏ธ ๐ณ ๐ ๐ข ๐ โซ๏ธ ๐ช โถ๏ธ & ๐ โซ๏ธ ๐ ๐ โ, โ๏ธ ๐ โซ๏ธ 5๏ธโฃ๐ โธ โธ ๐ ๐โโ๏ธ, ๐โ ๐ค `await` ๐ โซ๏ธ. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ ๏ธ โ๏ธ ๐ ๐ โฎ๏ธ `async` & `await` ๐ ๐ฐ ๐ฌ โ๏ธ "๐". โซ๏ธ โญ ๐ ๐ โ ๐ถ, "๐". |
|||
|
|||
## ๐ |
|||
|
|||
โก๏ธ ๐ ๐ ๐ค โช๏ธโก๏ธ ๐: |
|||
|
|||
> ๐ โฌ ๐ โ๏ธ ๐โ๐ฆบ **"๐ ๐"** โ๏ธ ๐ณ ๐ค **"๐"**, โฎ๏ธ **`async` & `await`** โ. |
|||
|
|||
๐ ๐ โ ๐
๐ ๐. ๐ถ |
|||
|
|||
๐ ๐ โซ๏ธโ ๐๏ธ FastAPI (๐ ๐) & โซ๏ธโ โ โซ๏ธ โ๏ธ โ
๐ ๐ญ. |
|||
|
|||
## ๐ถ ๐ก โน |
|||
|
|||
!!! warning |
|||
๐ ๐ช ๐ฒ ๐ถ ๐. |
|||
|
|||
๐ ๐ถ ๐ก โน โ **FastAPI** ๐ท ๐. |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ก ๐ก (๐ถ-๐, ๐งต, ๐ซ, โ๏ธ.) & ๐ ๐ โ FastAPI ๐ต `async def` ๐ ๐ `def`, ๐ถ โคด๏ธ. |
|||
|
|||
### โก ๐ ๏ธ ๐ข |
|||
|
|||
๐โ ๐ ๐ฃ *โก ๐ ๏ธ ๐ข* โฎ๏ธ ๐ `def` โฉ๏ธ `async def`, โซ๏ธ ๐ ๐ข ๐งต ๐ โคด๏ธ โ, โฉ๏ธ โ ๐ค ๐ (โซ๏ธ ๐ ๐ซ ๐ฝ). |
|||
|
|||
๐ฅ ๐ ๐ โช๏ธโก๏ธ โ1๏ธโฃ ๐ ๐ ๏ธ ๐ ๐จ ๐ซ ๐ท ๐ ๐ฌ ๐ & ๐ โ๏ธ โ ๐ ๐-๐ด *โก ๐ ๏ธ ๐ข* โฎ๏ธ โ
`def` ๐คช ๐ญ ๐ (๐ 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐), ๐ ๐ ๐ **FastAPI** โญ ๐ ๐. ๐ซ ๐ผ, โซ๏ธ ๐ป โ๏ธ `async def` ๐ฅ ๐ *โก ๐ ๏ธ ๐ข* โ๏ธ ๐ ๐ ๐ญ ๐ง <abbr title="Input/Output: disk reading or writing, network communications.">๐ค/๐
พ</abbr>. |
|||
|
|||
, ๐ฏโโ๏ธ โ , ๐ค ๐ **FastAPI** ๐ [โฉ](/#performance){.internal-link target=_blank} ๐ (โ๏ธ ๐ โญ) ๐ โฎ๏ธ ๐ ๏ธ. |
|||
|
|||
### ๐ |
|||
|
|||
๐ โ [๐](/tutorial/dependencies/index.md){.internal-link target=_blank}. ๐ฅ ๐ ๐ฉ `def` ๐ข โฉ๏ธ `async def`, โซ๏ธ ๐ ๐ข ๐งต. |
|||
|
|||
### ๐ง-๐ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ & [๐ง-๐](/tutorial/dependencies/sub-dependencies.md){.internal-link target=_blank} ๐ซ ๐ ๐ (๐ข ๐ข ๐), ๐ซ ๐ช โ โฎ๏ธ `async def` & โฎ๏ธ ๐ `def`. โซ๏ธ ๐ ๐ท, & ๐ โ โฎ๏ธ ๐ `def` ๐ ๐ค ๐ ๐ข ๐งต (โช๏ธโก๏ธ ๐งต) โฉ๏ธ โ "โ". |
|||
|
|||
### ๐ ๐ ๐ข |
|||
|
|||
๐ ๐ ๐ ๐ข ๐ ๐ ๐ค ๐ ๐ช โ โฎ๏ธ ๐ `def` โ๏ธ `async def` & FastAPI ๐ ๐ซ ๐ ๐ ๐ ๐ค โซ๏ธ. |
|||
|
|||
๐ ๐
๐ข ๐ FastAPI ๐ค ๐: *โก ๐ ๏ธ ๐ข* & ๐. |
|||
|
|||
๐ฅ ๐ ๐ ๐ข ๐ ๐ข โฎ๏ธ `def`, โซ๏ธ ๐ ๐ค ๐ (๐ โ โซ๏ธ ๐ ๐), ๐ซ ๐งต, ๐ฅ ๐ข โ โฎ๏ธ `async def` โคด๏ธ ๐ ๐ `await` ๐ ๐ข ๐โ ๐ ๐ค โซ๏ธ ๐ ๐. |
|||
|
|||
--- |
|||
|
|||
๐, ๐ ๐ถ ๐ก โน ๐ ๐ ๐ฒ โ ๐ฅ ๐ ๐ ๐ ๐ซ. |
|||
|
|||
โช, ๐ ๐ ๐ โฎ๏ธ ๐ โช๏ธโก๏ธ ๐ ๐: <a href="#in-a-hurry">๐ โ</a>. |
@ -0,0 +1,34 @@ |
|||
# ๐ |
|||
|
|||
๐ฌ ๐ธ๐ฒ ๐ ๐ฆ **FastAPI** ๐ธ ๐โโ ๐ฝ Uvicorn <a href="https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7" class="external-link" target="_blank">1๏ธโฃ โฉ ๐ ๐ ๏ธ ๐ช</a>, ๐ด ๐ ๐ & Uvicorn ๐ซ (โ๏ธ ๐ FastAPI). (*) |
|||
|
|||
โ๏ธ ๐โ โ
๐ & ๐บ ๐ ๐ โ๏ธ ๐ ๐คฏ. |
|||
|
|||
## ๐ & ๐
|
|||
|
|||
๐โ ๐ โ
๐, โซ๏ธ โ ๐ ๐ ๐งฐ ๐ ๐ ๐ฌ ๐. |
|||
|
|||
๐ฏ, ๐ Uvicorn, ๐ & FastAPI ๐ฌ ๐ฏโโ๏ธ (๐ช ๐ ๐ ๐งฐ). |
|||
|
|||
๐
โ โ ๐งฐ, ๐ ๐ญ โซ๏ธ ๐ ๐ค. & ๐ ๐ ๐ซ ๐ฏ ๐ โ ๐ ๐งฐ. |
|||
|
|||
๐ ๐: |
|||
|
|||
* **Uvicorn**: ๐ซ ๐ฝ |
|||
* **๐**: (โ๏ธ Uvicorn) ๐ธ ๐ธ |
|||
* **FastAPI**: (โ๏ธ ๐) ๐ ๏ธ ๐ธ โฎ๏ธ ๐ ๐ โ ๐ ๐, โฎ๏ธ ๐ฝ ๐ฌ, โ๏ธ. |
|||
|
|||
* **Uvicorn**: |
|||
* ๐ โ๏ธ ๐ ๐ญ, โซ๏ธ ๐ซ โ๏ธ ๐
โ ๐ โ๏ธ โช๏ธโก๏ธ ๐ฝ โซ๏ธ. |
|||
* ๐ ๐ซ๐ โ ๐ธ Uvicorn ๐. ๐ ๐ โ ๐ ๐ ๐ ๐ โ๏ธ ๐ ๐ โ๏ธ ๐, ๐, ๐ ๐ ๐ ๐ (โ๏ธ **FastAPI**). & ๐ฅ ๐ ๐, ๐ ๐ ๐ธ ๐ โ๏ธ ๐ ๐ฅ โ๏ธ โ๏ธ ๐ ๏ธ & ๐ ๐ ๐ฑ ๐ & ๐. |
|||
* ๐ฅ ๐ โ Uvicorn, ๐ฌ โซ๏ธ ๐ก ๐ธ, Hypercorn, โณ, โ๏ธ. ๐ธ ๐ฝ. |
|||
* **๐**: |
|||
* ๐ โ๏ธ โญ ๐ ๐ญ, โฎ๏ธ Uvicorn. ๐, ๐ โ๏ธ Uvicorn ๐. , โซ๏ธ ๐ฒ ๐ช ๐ด ๐ค "๐" ๐ Uvicorn โ๏ธ ๐ ๏ธ ๐
๐. |
|||
* โ๏ธ โซ๏ธ ๐ ๐ ๐งฐ ๐ ๐
๐ธ ๐ธ, โฎ๏ธ ๐น โ๏ธ ๐ โก, โ๏ธ. |
|||
* ๐ฅ ๐ โ ๐, ๐ฌ โซ๏ธ ๐ก ๐คฃ, ๐บ, โณ, โ๏ธ. ๐ธ ๐ ๏ธ (โ๏ธ ๐ธ). |
|||
* **FastAPI**: |
|||
* ๐ ๐ ๐ ๐ โ๏ธ Uvicorn & ๐ซ๐ โฉ ๐ โซ๏ธ, **FastAPI** โ๏ธ ๐, โซ๏ธ ๐ซ๐ โฉ ๐ โซ๏ธ. |
|||
* FastAPI ๐ ๐
โ ๐ ๐ ๐. โ ๐ ๐ ๐ ๐ง ๐ช ๐โ ๐ ๐, ๐ ๐ฝ ๐ฌ & ๐ ๏ธ. & โ๏ธ โซ๏ธ, ๐ ๐ค ๐ง ๐งพ ๐ (๐ง ๐งพ ๐ซ ๐ฎ ๐ฅ ๐โโ ๐ธ, โซ๏ธ ๐ ๐ ๐ด). |
|||
* ๐ฅ ๐ ๐ซ โ๏ธ FastAPI & โ๏ธ ๐ ๐ (โ๏ธ โ1๏ธโฃ ๐งฐ, ๐ ๐คฃ, ๐บ, ๐, โ๏ธ) ๐ ๐ โ๏ธ ๐ ๏ธ ๐ ๐ฝ ๐ฌ & ๐ ๏ธ ๐. , ๐ ๐ ๐ธ ๐ โ๏ธ ๐ ๐ฅ ๐ฅ โซ๏ธ ๐ โ๏ธ FastAPI. & ๐ ๐ผ, ๐ ๐ฝ ๐ฌ & ๐ ๏ธ ๐ฆ ๐ธ ๐ โ ๐ธ. |
|||
* , โ๏ธ FastAPI ๐ โป ๐ ๏ธ ๐ฐ, ๐, โธ ๐, & ๐ ๐ ๐ฒ ๐ค ๐ ๐ญ (โ๏ธ ๐) ๐ ๐ ๐ฅ ๐ ๐ซ โ๏ธ โซ๏ธ (๐ ๐ โ๏ธ ๐ ๏ธ โซ๏ธ ๐ ๐ ๐). |
|||
* ๐ฅ ๐ โ FastAPI, ๐ฌ โซ๏ธ ๐ก ๐ธ ๐ธ ๐ ๏ธ (โ๏ธ โ ๐งฐ) ๐ ๐ ๐ฝ ๐ฌ, ๐ ๏ธ & ๐งพ, ๐ ๐บ-apispec, NestJS, โจ, โ๏ธ. ๐ ๏ธ โฎ๏ธ ๐ ๏ธ ๐ง ๐ฝ ๐ฌ, ๐ ๏ธ & ๐งพ. |
@ -0,0 +1,465 @@ |
|||
# ๐ ๏ธ - ๐ |
|||
|
|||
๐ฅ, ๐ ๐ช ๐ ๐ ๐ฐ ๐ [โน FastAPI & ๐ค โน](help-fastapi.md){.internal-link target=_blank}. |
|||
|
|||
## ๐ ๏ธ |
|||
|
|||
๐ฅ ๐ โช ๐ ๐ & ๐ ๐ญ ๐ ๐ ๐ช โฌ ๐คฟ ๐, ๐ฅ ๐ โ ๐ ๐ ๐. |
|||
|
|||
### ๐น ๐ โฎ๏ธ `venv` |
|||
|
|||
๐ ๐ช โ ๐น ๐ ๐ โ๏ธ ๐ `venv` ๐น: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ python -m venv env |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ โ ๐ `./env/` โฎ๏ธ ๐ ๐ฑ & โคด๏ธ ๐ ๐ ๐ช โ ๐ฆ ๐ โ ๐. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ ๐ ๐ โฎ๏ธ: |
|||
|
|||
=== "๐พ, ๐ธ๐ป" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ source ./env/bin/activate |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
=== "๐ช ๐" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ .\env\Scripts\Activate.ps1 |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
=== "๐ช ๐" |
|||
|
|||
โ๏ธ ๐ฅ ๐ โ๏ธ ๐ ๐ฅ (โ
<a href="https://gitforwindows.org/" class="external-link" target="_blank">๐ ๐</a>): |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ source ./env/Scripts/activate |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โ
โซ๏ธ ๐ท, โ๏ธ: |
|||
|
|||
=== "๐พ, ๐ธ๐ป, ๐ช ๐" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ which pip |
|||
|
|||
some/directory/fastapi/env/bin/pip |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
=== "๐ช ๐" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ Get-Command pip |
|||
|
|||
some/directory/fastapi/env/bin/pip |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ฅ โซ๏ธ ๐ฆ `pip` ๐ฑ `env/bin/pip` โคด๏ธ โซ๏ธ ๐ท. ๐ถ |
|||
|
|||
โ ๐ญ ๐ โ๏ธ ๐ฐ ๐ โฌ ๐ ๐ ๐น ๐ โ โ ๐ โญ ๐ถ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ python -m pip install --upgrade pip |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
!!! tip |
|||
๐ ๐ฐ ๐ โ ๐ ๐ฆ โฎ๏ธ `pip` ๐ฝ ๐ ๐, ๐ ๐ ๐. |
|||
|
|||
๐ โ ๐ญ ๐ ๐ฅ ๐ โ๏ธ ๐ถ ๐ โ ๐ ๐ฆ, ๐ โ๏ธ 1๏ธโฃ โช๏ธโก๏ธ ๐ ๐ง๐ฟ ๐ & ๐ซ ๐ ๐ ๐ ๐ช โ ๐. |
|||
|
|||
### ๐ |
|||
|
|||
โฎ๏ธ ๐ ๐ ๐ฌ ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install -r requirements.txt |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โซ๏ธ ๐ โ ๐ ๐ & ๐ ๐ง๐ฟ FastAPI ๐ ๐ง๐ฟ ๐. |
|||
|
|||
#### โ๏ธ ๐ ๐ง๐ฟ FastAPI |
|||
|
|||
๐ฅ ๐ โ ๐ ๐ ๐ ๐ & โ๏ธ FastAPI, & ๐ โซ๏ธ โฎ๏ธ ๐ โช๏ธโก๏ธ ๐ ๐ง๐ฟ ๐, โซ๏ธ ๐ โ๏ธ ๐ ๐ง๐ฟ FastAPI โน ๐. |
|||
|
|||
& ๐ฅ ๐ โน ๐ ๐ง๐ฟ FastAPI โน ๐, โซ๏ธ โ โฎ๏ธ `-e`, ๐โ ๐ ๐ ๐ ๐ ๐ ๐, โซ๏ธ ๐ โ๏ธ ๐ โฌ FastAPI ๐ โ. |
|||
|
|||
๐ ๐, ๐ ๐ซ โ๏ธ "โ" ๐ ๐ง๐ฟ โฌ ๐ช ๐ฏ ๐ ๐. |
|||
|
|||
### ๐ |
|||
|
|||
๐ค โ ๐ ๐ ๐ช ๐ ๐ ๐ ๐ & ๐งน ๐ ๐ ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ bash scripts/format.sh |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โซ๏ธ ๐ ๐-๐ ๐ ๐ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ซ โ, ๐ ๐ช โ๏ธ FastAPI โ ๐ ๐ ๐, โฎ๏ธ ๐ ๐ ๐ โ๏ธ `-e`. |
|||
|
|||
## ๐ฉบ |
|||
|
|||
๐ฅ, โ ๐ญ ๐ โ ๐ ๐ ๐ ๐ฌ ๐, ๐ ๐ โ ๐ ๐. |
|||
|
|||
๐งพ โ๏ธ <a href="https://www.mkdocs.org/" class="external-link" target="_blank">โฌ</a>. |
|||
|
|||
& ๐ค โ ๐งฐ/โ ๐ฅ ๐ต โ `./scripts/docs.py`. |
|||
|
|||
!!! tip |
|||
๐ ๐ซ ๐ช ๐ ๐ `./scripts/docs.py`, ๐ โ๏ธ โซ๏ธ ๐ โธ. |
|||
|
|||
๐ ๐งพ โ ๐ ๐ `./docs/en/`. |
|||
|
|||
๐ ๐ฐ โ๏ธ ๐ซ ๐. |
|||
|
|||
๐
๐ผ, ๐ซ ๐ซ ๐ โ ๐ ๐ธ ๐ ๐ช ๐. |
|||
|
|||
๐, ๐ ๐ซ ๐ ๐ซ โ ๐ โ, ๐ซ ๐ ๐ `./docs_src/` ๐. |
|||
|
|||
& ๐ ๐ ๐ ๐/๐ ๐งพ ๐โ ๐ญ ๐ธ. |
|||
|
|||
### ๐ฉบ ๐ฏ |
|||
|
|||
๐ ๐ฏ ๐ค ๐ ๐ก ๐ผ โน ๐ ๐งพ. |
|||
|
|||
๐ โน โ ๐ญ ๐: |
|||
|
|||
* ๐งพ ๐ ๐
. |
|||
* ๐งพ ๐ผ ๐ช ๐. |
|||
* ๐
โ ๐ ๐งพ, ๐ ๐ฏ ๐ฐ. |
|||
|
|||
โฎ๏ธ ๐ง๐ฟ ๐ ๏ธ, ๐ค โ ๐ ๐ ๐ธ & โ
๐ ๐, ๐-๐ซ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ python ./scripts/docs.py live |
|||
|
|||
<span style="color: green;">[INFO]</span> Serving on http://127.0.0.1:8008 |
|||
<span style="color: green;">[INFO]</span> Start watching changes |
|||
<span style="color: green;">[INFO]</span> Start detecting changes |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โซ๏ธ ๐ ๐ฆ ๐งพ ๐ `http://127.0.0.1:8008`. |
|||
|
|||
๐ ๐, ๐ ๐ช โ ๐งพ/โน ๐ & ๐ ๐ ๐. |
|||
|
|||
#### ๐ โณ (๐ฆ) |
|||
|
|||
๐ฉโ๐พ ๐ฅ ๐ฆ ๐ โ โ๏ธ โ `./scripts/docs.py` โฎ๏ธ `python` ๐ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ช โ๏ธ <a href="https://typer.tiangolo.com/typer-cli/" class="external-link" target="_blank">๐ โณ</a>, & ๐ ๐ ๐ค โ ๐ ๐ถ ๐ โฎ๏ธ โ ๐ ๏ธ. |
|||
|
|||
๐ฅ ๐ โ ๐ โณ, ๐ ๐ช โ ๐ ๏ธ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ typer --install-completion |
|||
|
|||
zsh completion installed in /home/user/.bashrc. |
|||
Completion will take effect once you restart the terminal. |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
### ๐ฑ & ๐ฉบ ๐ ๐ฐ |
|||
|
|||
๐ฅ ๐ ๐ ๐ผ โฎ๏ธ, โ
: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn tutorial001:app --reload |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
Uvicorn ๐ข ๐ โ๏ธ โด `8000`, ๐งพ ๐ โด `8008` ๐ ๐ซ โ. |
|||
|
|||
### โ |
|||
|
|||
โน โฎ๏ธ โ ๐ถ ๐
๐ โ & โซ๏ธ ๐ช ๐ซ ๐จ ๐ต โน โช๏ธโก๏ธ ๐ช. ๐ถ ๐ถ |
|||
|
|||
๐ฅ ๐ถ โน โฎ๏ธ โ. |
|||
|
|||
#### ๐โโ & ๐ |
|||
|
|||
* โ
โณ <a href="https://github.com/tiangolo/fastapi/pulls" class="external-link" target="_blank">โป ๐ฒ ๐จ</a> ๐ ๐ช๐ธ & ๐ฎ ๐ โ ๐ โ๏ธ โ ๐ซ. |
|||
|
|||
!!! tip |
|||
๐ ๐ช <a href="https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request" class="external-link" target="_blank">๐ฎ ๐ค โฎ๏ธ ๐ ๐</a> โป ๐ฒ ๐จ. |
|||
|
|||
โ
๐ฉบ ๐ <a href="https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-request-reviews" class="external-link" target="_blank">โ ๐ฒ ๐จ ๐</a> โ โซ๏ธ โ๏ธ ๐จ ๐. |
|||
|
|||
* โ
<a href="https://github.com/tiangolo/fastapi/issues" class="external-link" target="_blank">โ</a> ๐ ๐ฅ ๐ค 1๏ธโฃ ๐ ๏ธ โ ๐ ๐ช๐ธ. |
|||
|
|||
* ๐ฎ ๐ ๐ฒ ๐จ ๐ ๐ ๐ฌ. ๐ ๐ โ โซ๏ธ ๐
โฉ ๐ ๐ โซ๏ธ. |
|||
|
|||
๐ช๐ธ ๐ค ๐ซ ๐ฌ, ๐ค ๐ โ ๐ ๐ ๐ โ โญ ๐. |
|||
|
|||
* ๐ ๐ช โ
๐ฅ ๐ค โ ๐ ๐ช๐ธ & ๐ฎ ๐ ๐ซ, ๐ ๐ โน ๐ค ๐ญ ๐ โ โ & ๐ค ๐ช ๐ โซ๏ธ. |
|||
|
|||
* โ๏ธ ๐ ๐ ๐ผ & ๐ด ๐ฌ โ ๐ฉบ. ๐ ๐ซ โ๏ธ ๐ ๐ณ ๐ ๐ท. |
|||
|
|||
* โ๏ธ ๐ ๐ผ, ๐ ๐, & ๐. ๐ ๐ซ โ๏ธ ๐ ๐ณ โซ๏ธ ๐ท. |
|||
|
|||
* โ
2๏ธโฃ-๐ค ๐ ๐ช๐ธ ๐ ๐ ๐ฌ ๐ ๐ช โ๏ธ ๐ <a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes" class="external-link" target="_blank">๐ ๐พ 6๏ธโฃ3๏ธโฃ9๏ธโฃ-1๏ธโฃ ๐</a>. |
|||
|
|||
#### โป ๐ช๐ธ |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ ๐ฌ ๐ ๐ช๐ธ ๐ โช โ๏ธ โ ๐, ๐ ๐ช๐ธ. |
|||
|
|||
๐ผ ๐ช๐ธ, 2๏ธโฃ-๐ค ๐ `es`. , ๐ ๐ช๐ธ โ ๐ `docs/es/`. |
|||
|
|||
!!! tip |
|||
๐ ("๐") ๐ช๐ธ ๐ช๐ธ, ๐ `docs/en/`. |
|||
|
|||
๐ ๐ ๐ ๐ฝ ๐ฉบ ๐ช๐ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// Use the command "live" and pass the language code as a CLI argument |
|||
$ python ./scripts/docs.py live es |
|||
|
|||
<span style="color: green;">[INFO]</span> Serving on http://127.0.0.1:8008 |
|||
<span style="color: green;">[INFO]</span> Start watching changes |
|||
<span style="color: green;">[INFO]</span> Start detecting changes |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ช ๐ถ <a href="http://127.0.0.1:8008" class="external-link" target="_blank">http://127.0.0.1:8008</a> & ๐ ๐ ๐ ๐. |
|||
|
|||
๐ฅ ๐ ๐ FastAPI ๐ฉบ ๐ธ, ๐ ๐ ๐ ๐ ๐ ๐ช๐ธ โ๏ธ ๐ ๐. โ๏ธ ๐ ๐ซ ๐ฌ & โ๏ธ ๐จ ๐ โ โ. |
|||
|
|||
โ๏ธ ๐โ ๐ ๐ โซ๏ธ ๐ ๐ ๐, ๐ ๐ ๐ด ๐ ๐ ๐ โช ๐ฌ. |
|||
|
|||
๐ โก๏ธ ๐ฌ ๐ ๐ ๐ ๐ฎ โ ๐ [โ](features.md){.internal-link target=_blank}. |
|||
|
|||
* ๐ ๐: |
|||
|
|||
``` |
|||
docs/en/docs/features.md |
|||
``` |
|||
|
|||
* ๐ โซ๏ธ โซ๏ธโ ๐ ๐บ โ๏ธ ๐ช๐ธ ๐ ๐ ๐ฌ, โ
: |
|||
|
|||
``` |
|||
docs/es/docs/features.md |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ด ๐ โก & ๐ ๐ ๐ช๐ธ ๐, โช๏ธโก๏ธ `en` `es`. |
|||
|
|||
* ๐ ๐ โฌ ๐ ๐ ๐ช๐ธ: |
|||
|
|||
``` |
|||
docs/en/mkdocs.yml |
|||
``` |
|||
|
|||
* ๐ ๐ฅ ๐โ ๐ `docs/features.md` ๐ ๐ ๐. ๐ฑ ๐: |
|||
|
|||
```YAML hl_lines="8" |
|||
site_name: FastAPI |
|||
# More stuff |
|||
nav: |
|||
- FastAPI: index.md |
|||
- Languages: |
|||
- en: / |
|||
- es: /es/ |
|||
- features.md |
|||
``` |
|||
|
|||
* ๐ โฌ ๐ ๐ ๐ช๐ธ ๐ โ, โ
: |
|||
|
|||
``` |
|||
docs/es/mkdocs.yml |
|||
``` |
|||
|
|||
* ๐ฎ โซ๏ธ ๐ค โ ๐ ๐บ โซ๏ธ ๐ช๐ธ, โ
: |
|||
|
|||
```YAML hl_lines="8" |
|||
site_name: FastAPI |
|||
# More stuff |
|||
nav: |
|||
- FastAPI: index.md |
|||
- Languages: |
|||
- en: / |
|||
- es: /es/ |
|||
- features.md |
|||
``` |
|||
|
|||
โ ๐ญ ๐ ๐ฅ ๐ค ๐ โ, ๐ โ โฎ๏ธ ๐ โ โซ๏ธโ ๐ โ ๐ช๐ธ โฌ. |
|||
|
|||
๐ฅ ๐ ๐ถ ๐ ๐ฅ ๐ ๐ ๐ ๐ ๐ ๐ฉบ ๐ฆ ๐ ๐ ๐. ๐ถ |
|||
|
|||
๐ ๐ ๐ช ๐ฌ โซ๏ธ ๐ & ๐ โ โซ๏ธ ๐ ๐ ๐ ๐. |
|||
|
|||
#### ๐ ๐ช๐ธ |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ ๐ ๐ฎ โ ๐ช๐ธ ๐ ๐ซ ๐ฌ, ๐ซ ๐. |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ ๐ฎ โ ๐ญ๐น, & โซ๏ธ ๐ซ ๐ค ๐ฉบ. |
|||
|
|||
โ
๐ โช๏ธโก๏ธ ๐, ๐ "๐ญ๐น" `ht`. |
|||
|
|||
โญ ๐ ๐ โ ๐ ๐ โ ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// Use the command new-lang, pass the language code as a CLI argument |
|||
$ python ./scripts/docs.py new-lang ht |
|||
|
|||
Successfully initialized: docs/ht |
|||
Updating ht |
|||
Updating en |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ช โ
๐ ๐ ๐จโ๐จ โณ โ ๐ `docs/ht/`. |
|||
|
|||
!!! tip |
|||
โ ๐ฅ ๐ฒ ๐จ โฎ๏ธ ๐, โ ๐ ๐ณ ๐ ๐ช๐ธ, โญ โ โ. |
|||
|
|||
๐ ๐ ๐ ๐ช โน โฎ๏ธ ๐ ๐ โช ๐ ๐ท ๐ ๐ฅ ๐. ๐ถ |
|||
|
|||
โถ๏ธ โ ๐ ๐, `docs/ht/index.md`. |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ฃ โฎ๏ธ โฎ๏ธ ๐ฉโ๐พ, "โป ๐ช๐ธ". |
|||
|
|||
##### ๐ ๐ช๐ธ ๐ซ ๐โ๐ฆบ |
|||
|
|||
๐ฅ ๐โ ๐โโ ๐ ๐ฝ โ ๐ ๐ค โ ๐ ๐ช๐ธ ๐ซ โ ๐โ๐ฆบ, ๐ณ ๐: |
|||
|
|||
``` |
|||
raise TemplateNotFound(template) |
|||
jinja2.exceptions.TemplateNotFound: partials/language/xx.html |
|||
``` |
|||
|
|||
๐ โ ๐ ๐ข ๐ซ ๐โ๐ฆบ ๐ ๐ช๐ธ (๐ ๐ผ, โฎ๏ธ โ 2๏ธโฃ-๐ค ๐ `xx`). |
|||
|
|||
โ๏ธ ๐ซ ๐, ๐ ๐ช โ ๐ข ๐ช๐ธ ๐ช๐ธ & โคด๏ธ ๐ฌ ๐ ๐ฉบ. |
|||
|
|||
๐ฅ ๐ ๐ช ๐, โ `mkdocs.yml` ๐ ๐ ๐ช๐ธ, โซ๏ธ ๐ โ๏ธ ๐ณ ๐: |
|||
|
|||
```YAML hl_lines="5" |
|||
site_name: FastAPI |
|||
# More stuff |
|||
theme: |
|||
# More stuff |
|||
language: xx |
|||
``` |
|||
|
|||
๐ ๐ ๐ช๐ธ โช๏ธโก๏ธ `xx` (โช๏ธโก๏ธ ๐ ๐ช๐ธ ๐) `en`. |
|||
|
|||
โคด๏ธ ๐ ๐ช โถ๏ธ ๐ ๐ฝ ๐. |
|||
|
|||
#### ๐ฎ ๐ |
|||
|
|||
๐โ ๐ โ๏ธ โ `./scripts/docs.py` โฎ๏ธ `live` ๐ โซ๏ธ ๐ด ๐ฆ ๐ & โ ๐ช โฎ๏ธ ๐ช๐ธ. |
|||
|
|||
โ๏ธ ๐ ๐ ๐จ, ๐ ๐ช ๐ฏ โซ๏ธ ๐ โซ๏ธ ๐ ๐ ๐ณ. |
|||
|
|||
๐, ๐ฅ ๐ ๐ ๐ฉบ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// Use the command "build-all", this will take a bit |
|||
$ python ./scripts/docs.py build-all |
|||
|
|||
Updating es |
|||
Updating en |
|||
Building docs for: en |
|||
Building docs for: es |
|||
Successfully built docs for: es |
|||
Copying en index.md to README.md |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ ๐ฉบ `./docs_build/` ๐ ๐ช๐ธ. ๐ ๐ โ ๐ ๐ โฎ๏ธ โ โ, โฎ๏ธ ๐ ๐ฌ ๐ "๐ ๐ ๐ซ โ๏ธ โ". โ๏ธ ๐ ๐ซ โ๏ธ ๐ณ โฎ๏ธ ๐ ๐. |
|||
|
|||
โคด๏ธ โซ๏ธ ๐ ๐ ๐ ๐ฌ โฌ ๐ธ ๐ ๐ช๐ธ, ๐ ๐ซ, & ๐ ๐ ๐ข `./site/`. |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ฆ ๐ โฎ๏ธ ๐ `serve`: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
// Use the command "serve" after running "build-all" |
|||
$ python ./scripts/docs.py serve |
|||
|
|||
Warning: this is a very simple server. For development, use mkdocs serve instead. |
|||
This is here only to preview a site with translations already built. |
|||
Make sure you run the build-all command first. |
|||
Serving at: http://127.0.0.1:8008 |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
## ๐ฏ |
|||
|
|||
๐ค โ ๐ ๐ ๐ช ๐ ๐ ๐ฏ ๐ ๐ & ๐ ๐ฐ ๐ ๐ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ bash scripts/test-cov-html.sh |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ ๐ `./htmlcov/`, ๐ฅ ๐ ๐ ๐ `./htmlcov/index.html` ๐ ๐ฅ, ๐ ๐ช ๐ฌ ๐ฅ ๐น๐ผ ๐ ๐ ๐ ๐ฏ, & ๐ ๐ฅ ๐ค ๐ ๐น๐ผ โ. |
@ -0,0 +1,311 @@ |
|||
# ๐ ๏ธ ๐ง |
|||
|
|||
๐โ ๐ ๏ธ **FastAPI** ๐ธ, โ๏ธ ๐ค, ๐ ๐ ๐ธ ๐ ๏ธ, ๐ค ๐ ๐ง ๐ ๐ ๐ฒ ๐
๐, & โ๏ธ ๐ซ ๐ ๐ช ๐ **๐ โ** ๐ **๐ ๏ธ ๐ ๐ธ**. |
|||
|
|||
โ ๐ง: |
|||
|
|||
* ๐โโ - ๐บ๐ธ๐ |
|||
* ๐โโ ๐ ๐ด |
|||
* โ |
|||
* ๐งฌ (๐ข ๐ ๏ธ ๐) |
|||
* ๐พ |
|||
* โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
๐ฅ ๐ ๐ โ ๐ซ ๐ ๐ **๐ ๏ธ**. |
|||
|
|||
๐, ๐ ๐ฏ ๐ช **๐ฆ ๐ ๐ ๏ธ ๐ฉโ๐ป** ๐ ๐ **๐**, **โ ๐**, & โ๏ธ **๐ โน** (๐ผ ๐ฐ ๐ฝ/๐น ๐ฐ) โป ๐ช. ๐ถ |
|||
|
|||
๐ค ๐ ๐ฌ ๐ ๐ ๐ ๐ ๐ซ **๐ง** ๐ฅ, & ๐ ๐ ๐ค ๐ค ๐ **๐ค** ๐ ๐ ๐ช ๐ญ โ ๐ ๏ธ ๐ ๐ ๏ธ ๐ถ ๐ ๐, ๐ฒ **๐ฎ** ๐ ๐ ๐ซ ๐. |
|||
|
|||
๐ค ๐ซ ๐ง, ๐ ๐ ๐ช **๐ฌ & ๐ง** ๐ ๐ ๐ ๏ธ **๐ ๐ ๐**. |
|||
|
|||
โญ ๐, ๐ค ๐ ๐ค ๐ ๐
**๐งฑ ๐ฎ** ๐ ๏ธ FastAPI ๐ธ. |
|||
|
|||
โ๏ธ ๐, โก๏ธ โ
๐ โ **โ ๐ญ**. ๐ซ ๐ง โ ๐ ๐ ๐ ๐ธ ๐ ๏ธ. ๐ถ |
|||
|
|||
## ๐โโ - ๐บ๐ธ๐ |
|||
|
|||
[โฎ๏ธ ๐ ๐ ๐บ๐ธ๐](./https.md){.internal-link target=_blank} ๐ฅ ๐ญ๐ฒ ๐ โ ๐บ๐ธ๐ ๐ ๐ ๐ ๐ ๏ธ. |
|||
|
|||
๐ฅ ๐ ๐ ๐บ๐ธ๐ ๐ ๐ ๐ฆฒ **๐ข** ๐ ๐ธ ๐ฝ, **๐ค โ ๐ณ**. |
|||
|
|||
& ๐ค โ๏ธ ๐ณ ๐ **โป ๐บ๐ธ๐ ๐**, โซ๏ธ ๐ช ๐ ๐ฆฒ โ๏ธ โซ๏ธ ๐ช ๐ณ ๐. |
|||
|
|||
### ๐ผ ๐งฐ ๐บ๐ธ๐ |
|||
|
|||
๐งฐ ๐ ๐ช โ๏ธ ๐ค โ ๐ณ: |
|||
|
|||
* Traefik |
|||
* ๐ ๐ต ๐ ๐ ๐ถ |
|||
* ๐ฅ |
|||
* ๐ ๐ต ๐ ๐ ๐ถ |
|||
* ๐ |
|||
* โฎ๏ธ ๐ข ๐ฆฒ ๐ Certbot ๐ ๐ |
|||
* โณ |
|||
* โฎ๏ธ ๐ข ๐ฆฒ ๐ Certbot ๐ ๐ |
|||
* Kubernete โฎ๏ธ ๐ง ๐น ๐ ๐ |
|||
* โฎ๏ธ ๐ข ๐ฆฒ ๐ ๐-๐จโ๐ผ ๐ ๐ |
|||
* ๐ต ๐ โ ๐โ๐ฆบ ๐ ๐ซ ๐โ๐ฆบ (โ ๐ ๐ถ) |
|||
|
|||
โ1๏ธโฃ ๐ ๐ ๐ ๐ช โ๏ธ **โ ๐โ๐ฆบ** ๐ ๐จ ๐ ๐ท โ
โ ๐ ๐บ๐ธ๐. โซ๏ธ ๐ช โ๏ธ ๐ซ โ๏ธ ๐ ๐ ๐
, โ๏ธ. โ๏ธ ๐ ๐ผ, ๐ ๐ซ๐ โ๏ธ โ ๐ ๐ค โ ๐ณ ๐. |
|||
|
|||
๐ค ๐ ๐ฆ ๐ ๐งฑ ๐ผ โญ ๐. |
|||
|
|||
--- |
|||
|
|||
โคด๏ธ โญ ๐ง ๐ค ๐ ๐ ๐ ๐ ๐ โ ๐ ๏ธ (โ
Uvicorn). |
|||
|
|||
## ๐ & ๐ ๏ธ |
|||
|
|||
๐ฅ ๐ ๐ฌ ๐ ๐ ๐ "**๐ ๏ธ**", โซ๏ธ โ โ๏ธ โฏ ๐ โซ๏ธโ โซ๏ธ โ, & โซ๏ธโ ๐บ โฎ๏ธ ๐ค "**๐**". |
|||
|
|||
### โซ๏ธโ ๐ |
|||
|
|||
๐ค **๐** ๐ โ๏ธ ๐ฌ ๐ ๐: |
|||
|
|||
* **๐** ๐ ๐ โ, **๐ ๐**. |
|||
* **๐** ๐ ๐ช **๐ ๏ธ** ๐โโ โ๏ธ, ๐ผ: `python`, `python.exe` โ๏ธ `uvicorn`. |
|||
* ๐ฏ ๐ โช โซ๏ธ **๐โโ** ๐ ๐ โ๏ธ, โ๏ธ ๐ฝ, & โป ๐ ๐ ๐พ. ๐ ๐ค **๐ ๏ธ**. |
|||
|
|||
### โซ๏ธโ ๐ ๏ธ |
|||
|
|||
๐ค **๐ ๏ธ** ๐ โ๏ธ ๐ ๐ฏ ๐, ๐ด ๐ ๐ ๐ ๐ ๐โโ โ๏ธ (๐ ๐ โ ๐): |
|||
|
|||
* ๐ฏ ๐ โช โซ๏ธ **๐โโ** ๐ ๐โโ โ๏ธ. |
|||
* ๐ ๐ซ ๐ ๐, ๐ซ ๐, โซ๏ธ ๐ **๐ฏ** ๐ ๐ โ **๐ ๏ธ** & ๐ ๐โโ โ๏ธ. |
|||
* ๐ ๐, ๐ ๐, **๐ช ๐ด ๐** ๐โ โซ๏ธ โ **๐ ๏ธ**. , ๐โ ๐ค **๐ ๏ธ ๐**. |
|||
* ๐ ๏ธ ๐ช **โ** (โ๏ธ "๐ฅ") ๐, โ๏ธ ๐โโ โ๏ธ. ๐ โ, โซ๏ธ โ๏ธ ๐/โ ๐ ๏ธ, & โซ๏ธ ๐ช **๐
โโ ๐ ๐**. |
|||
* ๐ ๐ธ ๐ ๐ โ๏ธ ๐ ๐ ๐ ๐ป โ๏ธ ๐ ๏ธ โ
โซ๏ธ, ๐ ๐โโ ๐, ๐ ๐ช, โ๏ธ. & ๐ค ๐ ๐ ๐ ๏ธ ๐ **๐ ๐ฐ** โช ๐ป ๐. |
|||
* ๐ค ๐ช **๐ ๐ ๏ธ** **๐ ๐** ๐ ๐ ๐ฐ. |
|||
|
|||
๐ฅ ๐ โ
๐
"๐ ๐จโ๐ผ" โ๏ธ "โ๏ธ ๐ฅ" (โ๏ธ ๐ ๐งฐ) ๐ ๐โโ โ๏ธ, ๐ ๐ ๐ช ๐ ๐ ๐ ๐ ๏ธ ๐โโ. |
|||
|
|||
& , ๐ผ, ๐ ๐ ๐ฒ ๐ ๐ ๐ค ๐ ๐ ๏ธ ๐ ๐ ๐ฅ ๐ (๐ฆ, ๐, ๐, โ๏ธ). ๐ซ ๐ ๐ 1๏ธโฃ ๐ ๏ธ ๐ ๐, โ ๐ โ ๐ ๏ธ. |
|||
|
|||
<img class="shadow" src="/img/deployment/concepts/image01.png"> |
|||
|
|||
--- |
|||
|
|||
๐ ๐ ๐ฅ ๐ญ ๐บ ๐ โ **๐ ๏ธ** & **๐**, โก๏ธ ๐ฃ ๐ฌ ๐ ๐ ๏ธ. |
|||
|
|||
## ๐โโ ๐ ๐ด |
|||
|
|||
๐
๐ผ, ๐โ ๐ โ ๐ธ ๐ ๏ธ, ๐ ๐ โซ๏ธ **๐ง ๐โโ**, โก, ๐ ๐ ๐ฉโ๐ป ๐ช ๐ง ๐ โซ๏ธ. ๐ โ๏ธ, ๐ฅ ๐ โ๏ธ ๐ฏ ๐ค โซ๏ธโ ๐ ๐ โซ๏ธ ๐ ๐ด ๐ฏ โ , โ๏ธ ๐
๐ฐ ๐ ๐ โซ๏ธ ๐ง ๐โโ & **๐ช**. |
|||
|
|||
### ๐ฐ ๐ฝ |
|||
|
|||
๐โ ๐ โ ๐ ๐ฐ ๐ฝ (โ ๐ฝ, ๐น ๐ฐ, โ๏ธ.) ๐
๐ ๐ ๐ช ๐ Uvicorn (โ๏ธ ๐) โ, ๐ ๐ ๐ ๐โ ๐ ๏ธ ๐. |
|||
|
|||
& โซ๏ธ ๐ ๐ท & ๐ โ **โฎ๏ธ ๐ ๏ธ**. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ ๐ฝ ๐ธ, **๐โโ ๐ ๏ธ** ๐ ๐ฒ โ ๏ธ. |
|||
|
|||
& ๐ฅ ๐ฝ โ (๐ผ โฎ๏ธ โน, โ๏ธ ๐ ๏ธ โช๏ธโก๏ธ โ ๐โ๐ฆบ) ๐ ๐ฒ **๐ ๐ซ ๐ โซ๏ธ**. & โฉ๏ธ ๐, ๐ ๐ ๐ซ ๐ญ ๐ ๐ โ๏ธ โ ๐ ๏ธ โ. , ๐ ๐ ๏ธ ๐ ๐ง โ ๏ธ. ๐ถ |
|||
|
|||
### ๐ ๐ ๐ ๐ด |
|||
|
|||
๐ข, ๐ ๐ ๐ฒ ๐ ๐ฝ ๐ (โ
Uvicorn) โถ๏ธ ๐ ๐ ๐ฝ ๐ด, & ๐ต ๐ช ๐ **๐ฟ ๐ฅ**, โ๏ธ ๐ ๏ธ ๐ง ๐ โฎ๏ธ ๐ ๐ ๏ธ (โ
Uvicorn ๐โโ ๐ FastAPI ๐ฑ). |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ ๐, ๐ ๐ ๐ โ๏ธ **๐ ๐** ๐ ๐ โ ๐ญ ๐ ๐ธ ๐ ๐ ๐ด. & ๐ ๐ผ, โซ๏ธ ๐ โ ๐ญ ๐ ๐ฆฒ โ๏ธ ๐ธ ๐, ๐ผ, ๐ฝ. |
|||
|
|||
### ๐ผ ๐งฐ ๐ ๐ด |
|||
|
|||
๐ผ ๐งฐ ๐ ๐ช ๐ ๐จโ๐ญ: |
|||
|
|||
* โ |
|||
* Kubernete |
|||
* โ โ |
|||
* โ ๐ ๐ณ |
|||
* โณ |
|||
* ๐จโ๐ป |
|||
* ๐ต ๐ โ ๐โ๐ฆบ ๐ ๐ซ ๐โ๐ฆบ |
|||
* ๐... |
|||
|
|||
๐ค ๐ ๐ค ๐ ๐
๐งฑ ๐ผ โญ ๐. |
|||
|
|||
## โ |
|||
|
|||
๐ โ ๐ญ ๐ ๐ธ ๐ ๐ ๐ด, ๐ ๐ฒ ๐ โ ๐ญ โซ๏ธ **โ** โฎ๏ธ โ. |
|||
|
|||
### ๐ฅ โ โ |
|||
|
|||
๐ฅ, ๐ฟ, โ **โ**, ๐ ๐ฐ. ๐ฅ ๐ *๐ง* โ๏ธ **๐** ๐ตโโ ๐ ๐ฅ. ๐ถ |
|||
|
|||
& ๐ฅ ๐ฉโ๐ป ๐ง ๐ ๐ ๐ฅ ๐ ๐ ๐ & ๐ฅ ๐ ๏ธ ๐ โ (๐ฒ โ ๐ ๐ ๐โโ๏ธ ๐ถ). |
|||
|
|||
### ๐คช โ ๐ ๐ต |
|||
|
|||
๐โ ๐ ๐ธ ๐ โฎ๏ธ FastAPI, ๐ฅ ๐ค โ ๐ ๐, FastAPI ๐ ๐ ๐ โซ๏ธ ๐ ๐จ ๐ โฒ โ. ๐ก |
|||
|
|||
๐ฉโ๐ป ๐ ๐ค **5๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ ๐ฝ โ** ๐ ๐จ, โ๏ธ ๐ธ ๐ ๐ฃ ๐ท โญ ๐จ โฉ๏ธ ๐ฅ ๐. |
|||
|
|||
### ๐ฆ โ - ๐ฅ |
|||
|
|||
๐, ๐ค 5๏ธโฃ๐ ๐ผ ๐โ ๐ฅ โ ๐ ๐ **๐ฅ ๐ ๐ธ** โ Uvicorn & ๐ ๐ฅ. ๐ถ |
|||
|
|||
& , ๐ ๐ ๐ฒ ๐ซ ๐ ๐ธ ๐ง โ ๏ธ โฉ๏ธ ๐ค โ 1๏ธโฃ ๐ฅ, ๐ ๐ฒ ๐ โซ๏ธ **๐ฃ ๐** ๐ *โก ๐ ๏ธ* ๐ ๐ซ ๐. |
|||
|
|||
### โ โฎ๏ธ ๐ฅ |
|||
|
|||
โ๏ธ ๐ ๐ผ โฎ๏ธ ๐ค ๐ โ ๐ ๐ฅ ๐โโ **๐ ๏ธ**, ๐ ๐ ๐ ๐ข ๐ฆฒ ๐ ๐ **๐** ๐ ๏ธ, ๐ ๐ฉโโคโ๐จ ๐ฐ... |
|||
|
|||
!!! tip |
|||
...๐ ๐ฅ ๐ ๐ธ **๐ฅ โช** โซ๏ธ ๐ฒ ๐ซ โ ๐ ๐ง ๐ โซ๏ธ โพ. โ๏ธ ๐ ๐ผ, ๐ ๐ ๐ฒ ๐ โซ๏ธ โฎ๏ธ ๐ ๏ธ, โ๏ธ ๐ โถ๏ธ๏ธ โฎ๏ธ ๐ ๏ธ. |
|||
|
|||
โก๏ธ ๐ฏ ๐ ๐ ๐ผ, ๐โ โซ๏ธ ๐ช ๐ฅ ๐ ๐ฏ ๐ผ **๐ฎ**, & โซ๏ธ โ ๐ โ โซ๏ธ. |
|||
|
|||
๐ ๐ ๐ฒ ๐ โ๏ธ ๐ ๐ ๐ ๐ ๐ธ **๐ข ๐ฆฒ**, โฉ๏ธ ๐ โ, ๐ ๐ธ โฎ๏ธ Uvicorn & ๐ โช ๐ฅ, ๐ค ๐ณ ๐ ๐ ๐ ๐ฑ ๐ ๐ช ๐ณ ๐ โซ๏ธ. |
|||
|
|||
### ๐ผ ๐งฐ โ ๐ |
|||
|
|||
๐ ๐ผ, ๐ ๐งฐ ๐ โ๏ธ **๐ ๐ ๐ ๐ด** โ๏ธ ๐ต ๐ง **โ**. |
|||
|
|||
๐ผ, ๐ ๐ช ๐ต: |
|||
|
|||
* โ |
|||
* Kubernete |
|||
* โ โ |
|||
* โ ๐ ๐ณ |
|||
* โณ |
|||
* ๐จโ๐ป |
|||
* ๐ต ๐ โ ๐โ๐ฆบ ๐ ๐ซ ๐โ๐ฆบ |
|||
* ๐... |
|||
|
|||
## ๐งฌ - ๐ ๏ธ & ๐พ |
|||
|
|||
โฎ๏ธ FastAPI ๐ธ, โ๏ธ ๐ฝ ๐ ๐ Uvicorn, ๐โโ โซ๏ธ ๐ **1๏ธโฃ ๐ ๏ธ** ๐ช ๐ฆ ๐ ๐ฉโ๐ป ๐. |
|||
|
|||
โ๏ธ ๐ ๐ผ, ๐ ๐ ๐ ๐ ๐ ๐จโ๐ญ ๐ ๏ธ ๐ ๐ฐ. |
|||
|
|||
### ๐ ๐ ๏ธ - ๐จโ๐ญ |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐
๐ฉโ๐ป ๐ โซ๏ธโ ๐ ๐ ๏ธ ๐ช ๐ต (๐ผ ๐ฅ ๐น ๐ฐ ๐ซ ๐โโ๏ธ ๐ฆ) & ๐ โ๏ธ **๐ ๐** ๐ฝ ๐ฝ, โคด๏ธ ๐ ๐ช โ๏ธ **๐ ๐ ๏ธ** ๐โโ โฎ๏ธ ๐ ๐ธ ๐ ๐ฐ, & ๐ ๐ ๐จ ๐ช ๐ซ. |
|||
|
|||
๐โ ๐ ๐ **๐ ๐ ๏ธ** ๐ ๐ ๏ธ ๐, ๐ซ ๐ ๐ค **๐จโ๐ญ**. |
|||
|
|||
### ๐จโ๐ญ ๐ ๏ธ & โด |
|||
|
|||
๐ญ โช๏ธโก๏ธ ๐ฉบ [๐ ๐บ๐ธ๐](./https.md){.internal-link target=_blank} ๐ ๐ด 1๏ธโฃ ๐ ๏ธ ๐ช ๐ ๐ 1๏ธโฃ ๐ โด & ๐ข ๐ข ๐ฝ โ |
|||
|
|||
๐ โ. |
|||
|
|||
, ๐ช โ๏ธ **๐ ๐ ๏ธ** ๐ ๐ฐ, ๐ค โ๏ธ **๐ ๐ ๏ธ ๐ ๐ โด** ๐ โคด๏ธ ๐ถ ๐ป ๐ ๐จโ๐ญ ๐ ๏ธ ๐. |
|||
|
|||
### ๐พ ๐ ๐ ๏ธ |
|||
|
|||
๐, ๐โ ๐ ๐ ๐ ๐พ, ๐ผ, ๐ฐ ๐ซ ๐ท ๐ข, โ๏ธ ๐ โญ ๐ ๐ข, ๐ ๐ **๐ด ๐ ๐พ (๐พ)** ๐ฝ. |
|||
|
|||
& ๐ ๐ ๏ธ ๐ **๐ซ ๐ฐ ๐ ๐พ**. ๐ โ ๐ ๐ ๐ ๐ ๏ธ โ๏ธ ๐ฎ ๐ ๐, ๐ข, & ๐พ. & ๐ฅ ๐ ๐ฉ โญ ๐ธ ๐พ ๐ ๐, **๐ ๐ ๏ธ** ๐ ๐ด ๐ ๐ธ ๐พ. |
|||
|
|||
### ๐ฝ ๐พ |
|||
|
|||
๐ผ, ๐ฅ ๐ ๐ ๐ ๐ฐ ๐ซ ๐ท โฎ๏ธ **1๏ธโฃ ๐พ ๐**, ๐โ ๐ ๐ 1๏ธโฃ ๐ ๏ธ โฎ๏ธ ๐ ๐ ๏ธ, โซ๏ธ ๐ ๐ด ๐ 1๏ธโฃ ๐พ ๐พ. & ๐ฅ ๐ โถ๏ธ **4๏ธโฃ ๐ ๏ธ** (4๏ธโฃ ๐จโ๐ญ), ๐ ๐ ๐ด 1๏ธโฃ ๐พ ๐พ. ๐, ๐ ๐ ๏ธ ๐ ๐ด **4๏ธโฃ ๐พ ๐พ**. |
|||
|
|||
& ๐ฅ ๐ ๐ฐ ๐ฝ โ๏ธ ๐น ๐ฐ ๐ด โ๏ธ 3๏ธโฃ ๐พ ๐พ, ๐ ๐ ๐
๐ 4๏ธโฃ ๐พ ๐พ ๐ ๐ค โ . ๐ถ |
|||
|
|||
### ๐ ๐ ๏ธ - ๐ผ |
|||
|
|||
๐ ๐ผ, ๐ค **๐จโ๐ผ ๐ ๏ธ** ๐ โถ๏ธ & ๐ 2๏ธโฃ **๐จโ๐ญ ๐ ๏ธ**. |
|||
|
|||
๐ ๐จโ๐ผ ๐ ๏ธ ๐ ๐ฒ 1๏ธโฃ ๐ ๐ **โด** ๐ข. & โซ๏ธ ๐ ๐ถ ๐ ๐ป ๐จโ๐ญ ๐ ๏ธ. |
|||
|
|||
๐ ๐จโ๐ญ ๐ ๏ธ ๐ ๐ ๐โโ ๐ ๐ธ, ๐ซ ๐ ๐ญ ๐ ๐ ๐จ **๐จ** & ๐จ **๐จ**, & ๐ซ ๐ ๐ ๐ณ ๐ ๐ฎ ๐ข ๐พ. |
|||
|
|||
<img src="/img/deployment/concepts/process-ram.svg"> |
|||
|
|||
& โ๏ธ, ๐ ๐ฐ ๐ ๐ฒ โ๏ธ **๐ ๐ ๏ธ** ๐ ๐, โ๏ธ โช๏ธโก๏ธ ๐ ๐ธ. |
|||
|
|||
๐ โน ๐ ๐ **๐ฝ โ๏ธ** ๐ ๐ ๏ธ ๐ช **๐ช** ๐ ๐คญ ๐ฐ, โ๏ธ **๐พ (๐พ)** ๐ ๐ง ๐ โ๏ธ ๐ **โ**. |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ ๏ธ ๐ ๐จ โญ ๐ธ ๐ ๐ ๐ฐ & ๐ โ๏ธ ๐ ๐ฉโ๐ป, โคด๏ธ **๐ฝ ๐ ๏ธ** ๐ ๐ฒ *โ* (โฉ๏ธ ๐ง ๐ ๐ & ๐ฝ ๐). |
|||
|
|||
### ๐ผ ๐งฌ ๐งฐ & ๐ |
|||
|
|||
๐ค ๐ช ๐ ๐ฏ ๐ ๐, & ๐ค ๐ ๐ฌ ๐ ๐
๐ ๐ฏ ๐ โญ ๐, ๐ผ ๐โ ๐ฌ ๐ โ & ๐ฆ. |
|||
|
|||
๐ โ ๐ค ๐ ๐ค โ๏ธ **๐** ๐ฆฒ ๐ **โด** **๐ข ๐ข**. & โคด๏ธ โซ๏ธ โ๏ธ โ๏ธ ๐ **๐ถ** ๐ป ๐ **๐ ๏ธ/๐จโ๐ญ**. |
|||
|
|||
๐ฅ ๐ช ๐ & ๐: |
|||
|
|||
* **๐** ๐ ๏ธ **Uvicorn ๐จโ๐ญ** |
|||
* ๐ ๐ **๐ ๏ธ ๐จโ๐ผ** ๐ ๐ **๐ข** & **โด**, ๐งฌ ๐ โ๏ธ **๐ Uvicorn ๐จโ๐ญ ๐ ๏ธ** |
|||
* **Uvicorn** ๐ ๏ธ **Uvicorn ๐จโ๐ญ** |
|||
* 1๏ธโฃ Uvicorn **๐ ๏ธ ๐จโ๐ผ** ๐ ๐ ๐ **๐ข** & **โด**, & โซ๏ธ ๐ โถ๏ธ **๐ Uvicorn ๐จโ๐ญ ๐ ๏ธ** |
|||
* **Kubernete** & ๐ ๐ **๐ฆ โ๏ธ** |
|||
* ๐ณ **โ** ๐งฝ ๐ ๐ ๐ **๐ข** & **โด**. ๐งฌ ๐ โ๏ธ **๐ ๐ฆ**, ๐ โฎ๏ธ **1๏ธโฃ Uvicorn ๐ ๏ธ** ๐โโ |
|||
* **โ ๐โ๐ฆบ** ๐ ๐ต ๐ ๐ |
|||
* โ ๐โ๐ฆบ ๐ ๐ฒ **๐ต ๐งฌ ๐**. โซ๏ธ ๐ ๐ฒ โก๏ธ ๐ ๐ฌ **๐ ๏ธ ๐**, โ๏ธ **๐ฆ ๐ผ** โ๏ธ, ๐ ๐ผ, โซ๏ธ ๐ ๐
๐ฒ **๐ Uvicorn ๐ ๏ธ**, & โ ๐โ๐ฆบ ๐ ๐ ๐ โซ๏ธ. |
|||
|
|||
!!! tip |
|||
๐ซ ๐ ๐ฅ ๐ซ ๐ฌ ๐ **๐ฆ**, โ, โ๏ธ Kubernete ๐ซ โ ๐ ๐. |
|||
|
|||
๐ค ๐ ๐ฌ ๐ ๐
๐ ๐ฆ ๐ผ, โ, Kubernete, โ๏ธ. ๐ฎ ๐: [FastAPI ๐ฆ - โ](./docker.md){.internal-link target=_blank}. |
|||
|
|||
## โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
๐ค ๐ ๐ผ ๐โ ๐ ๐ ๐ญ ๐ถ **โญ โถ๏ธ** ๐ ๐ธ. |
|||
|
|||
๐ผ, ๐ ๐ช ๐ ๐ **๐ฝ ๐ ๏ธ**. |
|||
|
|||
โ๏ธ ๐
๐ผ, ๐ ๐ ๐ ๐ญ ๐ ๐ ๐ด **๐**. |
|||
|
|||
, ๐ ๐ ๐ โ๏ธ **๐ ๐ ๏ธ** ๐ญ ๐ **โฎ๏ธ ๐**, โญ โถ๏ธ ๐ธ. |
|||
|
|||
& ๐ ๐ โ๏ธ โ ๐ญ ๐ โซ๏ธ ๐ ๐ ๏ธ ๐ ๐ โฎ๏ธ ๐ ** ๐ฅ โฎ๏ธ, ๐ โถ๏ธ **๐ ๐ ๏ธ** (๐ ๐จโ๐ญ) ๐ธ โซ๏ธ. ๐ฅ ๐ ๐ ๐ **๐ ๐ ๏ธ**, ๐ซ ๐ **โ** ๐ท ๐โโ โซ๏ธ ๐ **๐**, & ๐ฅ ๐ถ ๐ณ ๐ ๐ ๐ฝ ๐ ๏ธ, ๐ซ ๐ช ๐ค โ โฎ๏ธ ๐ ๐. |
|||
|
|||
โ๏ธ, ๐ค ๐ผ ๐โ ๐ค ๐
โโ โ ๐ โฎ๏ธ ๐ ๐ ๐ฐ, ๐ ๐ผ, โซ๏ธ ๐ โฉ ๐ต. |
|||
|
|||
!!! tip |
|||
, โ๏ธ ๐คฏ ๐ โ๏ธ ๐ ๐ ๐ฅ, ๐ผ ๐ **5๏ธโฃ๐ ๐ซ ๐ช ๐ โฎ๏ธ ๐** โญ โถ๏ธ ๐ ๐ธ. |
|||
|
|||
๐ ๐ผ, ๐ ๐ซ๐ โ๏ธ ๐ ๐ ๐ ๐. ๐คท |
|||
|
|||
### ๐ผ โฎ๏ธ ๐ ๐ |
|||
|
|||
๐ ๐ **๐ช ๐** ๐ ๐ ๐ **๐ ๏ธ ๐ โ๏ธ**, & โซ๏ธ ๐ ๐ฒ ๐ ๐ ๐ โถ๏ธ ๐, ๐ โ, โ๏ธ. |
|||
|
|||
๐ฅ ๐ช ๐ญ: |
|||
|
|||
* "๐ ๐ฆ" Kubernete ๐ ๐ โญ ๐ ๐ฑ ๐ฆ |
|||
* ๐ โ ๐ ๐ โฎ๏ธ ๐ & โคด๏ธ โถ๏ธ ๐ ๐ธ |
|||
* ๐ ๐ ๐ช ๐ โถ๏ธ/โ *๐* ๐ โ, ๐ โ, โ๏ธ. |
|||
|
|||
!!! tip |
|||
๐ค ๐ ๐ค ๐ ๐
๐งฑ ๐ผ ๐จ ๐ โฎ๏ธ ๐ฆ ๐ฎ ๐: [FastAPI ๐ฆ - โ](./docker.md){.internal-link target=_blank}. |
|||
|
|||
## โน ๐ ๏ธ |
|||
|
|||
๐ ๐ฝ(โ) () **โน**, ๐ ๐ช ๐ด โ๏ธ **โ๏ธ**, โฎ๏ธ ๐ ๐, ๐ ๐ฐ ๐ ๐ฝ, & ๐พ ๐พ ๐ช. |
|||
|
|||
โ ๐
โ๏ธ โน ๐ ๐ ๐ฉ/โป โ โซ๏ธ ๐ช โฉ ๐ญ "๐ซ ๐
", โ๏ธ ๐, ๐ ๐ ๐ฒ ๐ ๐ด **๐
๐ช ๐ต ๐ฅ**. |
|||
|
|||
๐ฅ ๐ ๐ธ 3๏ธโฃ ๐ฝ โ๏ธ ๐ โ๏ธ ๐ด ๐ฅ ๐ ๐ซ ๐พ & ๐ฝ, ๐ ๐ฒ **๐ ๐ธ** ๐ถ, & ๐ฒ **๐ ๐ฝ ๐ฆ ๐๏ธ** ๐ถ, โ๏ธ. |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ช ๐ป โ๏ธ ๐ด 2๏ธโฃ ๐ฝ & โ๏ธ โ ๐ ๐ซ โน (๐ฝ, ๐พ, ๐พ, ๐ธ ๐ฟ, โ๏ธ). |
|||
|
|||
๐ ๐ โ, ๐ฅ ๐ โ๏ธ 2๏ธโฃ ๐ฝ & ๐ โ๏ธ **1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ ๐ซ ๐ฝ & ๐พ**, โ 1๏ธโฃ ๐ ๏ธ ๐ ๐ญ ๐
๐พ, & ๐ฝ ๐ โ๏ธ โ๏ธ ๐พ "๐พ" (โ ๐ช ๐ฏ ๐ฐ ๐), โ๏ธ **๐ฅ**. โ๏ธ 1๏ธโฃ ๐ ๏ธ ๐ช ๐ช ๐ & ๐ โ๏ธ โ โญ ๐ฝ ๐ ๐. |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ ๐ ๐ค **1๏ธโฃ โ ๐ฝ** & ๐ ๐ ๏ธ ๐ โซ๏ธ ๐ ๐ซ ๐ โ๏ธ **๐ฅ ๐พ & ๐ฝ ๐ฐ**. |
|||
|
|||
๐ค ๐ค ๐ ๐ค ๐ โ๏ธ **๐ต** โ๏ธ ๐ ๐ ๏ธ. ๐ฒ โซ๏ธ ๐ถ ๐ฆ , โ๏ธ ๐ฒ ๐ ๐โ๐ฆบ โ๏ธ ๐ค โถ๏ธ โ๏ธ โซ๏ธ. & ๐ ๐ช ๐ โ๏ธ โ โน ๐ ๐ ๐ผ. |
|||
|
|||
๐ ๐ช ๐ฎ **โ ๐ข** ๐ฏ, ๐ผ, ๐ณ **๐ 5๏ธโฃ0๏ธโฃ ๐ฏ 9๏ธโฃ0๏ธโฃ ๐ฏ** โน ๐ ๏ธ. โ ๐ ๐ ๐ฒ ๐ ๐ ๐ ๐ ๐ โ & โ๏ธ โ ๐ ๐ ๏ธ. |
|||
|
|||
๐ ๐ช โ๏ธ ๐
๐งฐ ๐ `htop` ๐ ๐ฝ & ๐พ โ๏ธ ๐ ๐ฝ โ๏ธ ๐ธ โ๏ธ ๐ ๐ ๏ธ. โ๏ธ ๐ ๐ช โ๏ธ ๐ ๐ โ ๐งฐ, โ 5๏ธโฃ๐ ๐ ๐คญ ๐ฝ, โ๏ธ. |
|||
|
|||
## ๐ |
|||
|
|||
๐ โ๏ธ ๐ ๐ฅ ๐ ๐ง ๐ ๐ ๐ ๐ฒ ๐ช โ๏ธ ๐คฏ ๐โ ๐ค โ ๐ ๏ธ ๐ ๐ธ: |
|||
|
|||
* ๐โโ - ๐บ๐ธ๐ |
|||
* ๐โโ ๐ ๐ด |
|||
* โ |
|||
* ๐งฌ (๐ข ๐ ๏ธ ๐) |
|||
* ๐พ |
|||
* โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
๐ค ๐ ๐ญ & โ โ ๐ซ ๐ ๐ค ๐ ๐ค ๐ช โ ๐ ๐ซ ๐โ ๐ ๏ธ & ๐ ๏ธ ๐ ๐ ๏ธ. ๐ถ |
|||
|
|||
โญ ๐, ๐ค ๐ ๐ค ๐ ๐
๐งฑ ๐ผ ๐ช ๐ ๐ ๐ช โฉ. ๐ถ |
@ -0,0 +1,698 @@ |
|||
# FastAPI ๐ฆ - โ |
|||
|
|||
๐โ ๐ ๏ธ FastAPI ๐ธ โ ๐ฏ ๐ **๐พ ๐ฆ ๐ผ**. โซ๏ธ ๐ ๐จ โ๏ธ <a href="https://www.docker.com/" class="external-link" target="_blank">**โ**</a>. ๐ ๐ช โคด๏ธ ๐ ๏ธ ๐ ๐ฆ ๐ผ 1๏ธโฃ ๐ฉโโคโ๐จ ๐ช ๐. |
|||
|
|||
โ๏ธ ๐พ ๐ฆ โ๏ธ ๐ ๐ โ
**๐โโ**, **๐ฌ**, **๐ฆ**, & ๐. |
|||
|
|||
!!! tip |
|||
๐ & โช ๐ญ ๐ ๐ฉ โ ๐ฆ [`Dockerfile` ๐ ๐ถ](#build-a-docker-image-for-fastapi). |
|||
|
|||
<details> |
|||
<summary>๐ ๐ฎ ๐ถ</summary> |
|||
|
|||
```Dockerfile |
|||
FROM python:3.9 |
|||
|
|||
WORKDIR /code |
|||
|
|||
COPY ./requirements.txt /code/requirements.txt |
|||
|
|||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt |
|||
|
|||
COPY ./app /code/app |
|||
|
|||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] |
|||
|
|||
# If running behind a proxy like Nginx or Traefik add --proxy-headers |
|||
# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] |
|||
``` |
|||
|
|||
</details> |
|||
|
|||
## โซ๏ธโ ๐ฆ |
|||
|
|||
๐ฆ (โด๏ธ ๐พ ๐ฆ) ๐ถ **๐ฟ** ๐ ๐ฆ ๐ธ โ
๐ ๐ซ ๐ & ๐ช ๐ โช ๐ง ๐ซ โ โช๏ธโก๏ธ ๐ ๐ฆ (๐ ๐ธ โ๏ธ ๐ฆฒ) ๐ โ๏ธ. |
|||
|
|||
๐พ ๐ฆ ๐ โ๏ธ ๐ ๐พ ๐พ ๐ฆ (๐ฐ, ๐น ๐ฐ, โ ๐ฝ, โ๏ธ). ๐ โ ๐ ๐ซ ๐ถ ๐ฟ (๐ฌ ๐ ๐น ๐ฐ ๐ ๐ ๐โโ โ๏ธ). |
|||
|
|||
๐ ๐, ๐ฆ ๐ด **๐ฅ โน**, ๐ธ โญ ๐โโ ๐ ๏ธ ๐ (๐น ๐ฐ ๐ ๐ด ๐
๐
). |
|||
|
|||
๐ฆ โ๏ธ ๐ซ ๐ **โ** ๐โโ ๐ ๏ธ (๐ 1๏ธโฃ ๐ ๏ธ), ๐ โ๏ธ, & ๐ธ, ๐ฌ ๐ ๏ธ, ๐โโ, ๐ ๏ธ, โ๏ธ. |
|||
|
|||
## โซ๏ธโ ๐ฆ ๐ผ |
|||
|
|||
**๐ฆ** ๐ โช๏ธโก๏ธ **๐ฆ ๐ผ**. |
|||
|
|||
๐ฆ ๐ผ **๐ป** โฌ ๐ ๐, ๐ ๐ข, & ๐ข ๐/๐ ๐ ๐ ๐ ๐ฆ. **๐ป** ๐ฅ โ ๐ ๐ฆ **๐ผ** ๐ซ ๐, โซ๏ธ ๐ซ โ ๐ ๏ธ, โซ๏ธ ๐ด ๐ฆ ๐ & ๐. |
|||
|
|||
๐
"**๐ฆ ๐ผ**" ๐ ๐ช ๐ป ๐,"**๐ฆ**" ๐ ๐ ๐โโ ๐, ๐ ๐ โ **๐ ๏ธ**. |
|||
|
|||
๐โ **๐ฆ** โถ๏ธ & ๐โโ (โถ๏ธ โช๏ธโก๏ธ **๐ฆ ๐ผ**) โซ๏ธ ๐ช โ โ๏ธ ๐ ๐, ๐ ๐ข, โ๏ธ. ๐ ๐ ๐ ๐ ๐ด ๐ ๐ฆ, โ๏ธ ๐ ๐ซ ๐ฃ ๐ฝ ๐ฆ ๐ผ (๐ ๐ซ ๐ ๐พ). |
|||
|
|||
๐ฆ ๐ผ โญ **๐** ๐ & ๐, โ
`python` & ๐ `main.py`. |
|||
|
|||
& **๐ฆ** โซ๏ธ (๐
**๐ฆ ๐ผ**) โ ๐ ๐ ๐ผ, โญ **๐ ๏ธ**. ๐, ๐ฆ ๐ ๐ด ๐โ โซ๏ธ โ๏ธ **๐ ๏ธ ๐** (& ๐ โซ๏ธ ๐ด ๐ ๐ ๏ธ). ๐ฆ โ๏ธ ๐โ ๐ค ๐
โโ ๐ ๏ธ ๐ โซ๏ธ. |
|||
|
|||
## ๐ฆ ๐ผ |
|||
|
|||
โ โ๏ธ 1๏ธโฃ ๐ ๐งฐ โ & ๐ ๏ธ **๐ฆ ๐ผ** & **๐ฆ**. |
|||
|
|||
& ๐ค ๐ข <a href="https://hub.docker.com/" class="external-link" target="_blank">โ ๐ก</a> โฎ๏ธ ๐ค-โ **๐ ๐ฆ ๐ผ** ๐ ๐งฐ, ๐, ๐ฝ, & ๐ธ. |
|||
|
|||
๐ผ, ๐ค ๐ <a href="https://hub.docker.com/_/python" class="external-link" target="_blank">๐ ๐ผ</a>. |
|||
|
|||
& ๐ค ๐ ๐ ๐ผ ๐ ๐ ๐ ๐ฝ, ๐ผ: |
|||
|
|||
* <a href="https://hub.docker.com/_/postgres" class="external-link" target="_blank">โณ</a> |
|||
* <a href="https://hub.docker.com/_/mysql" class="external-link" target="_blank">โณ</a> |
|||
* <a href="https://hub.docker.com/_/mongo" class="external-link" target="_blank">โณ</a> |
|||
* <a href="https://hub.docker.com/_/redis" class="external-link" target="_blank">โณ</a>, โ๏ธ. |
|||
|
|||
โ๏ธ ๐ค-โ ๐ฆ ๐ผ โซ๏ธ ๐ถ โฉ **๐** & โ๏ธ ๐ ๐งฐ. ๐ผ, ๐ ๐
๐ ๐ฝ. ๐
๐ผ, ๐ ๐ช โ๏ธ **๐ ๐ผ**, & ๐ ๐ซ โฎ๏ธ ๐ ๐ข. |
|||
|
|||
๐ ๐, ๐ ๐ผ ๐ ๐ช ๐ก ๐ ๐ฆ & โ & ๐ค-โ๏ธ ๐ ๐ก โฎ๏ธ ๐ ๐ ๐งฐ & ๐ฆฒ. |
|||
|
|||
, ๐ ๐ ๐ **๐ ๐ฆ** โฎ๏ธ ๐ ๐, ๐ ๐ฝ, ๐ ๐ธ, ๐ธ ๐ฝ โฎ๏ธ ๐ฅ ๐ธ ๐ธ, & ๐ ๐ซ ๐ฏโโ๏ธ ๐จ ๐ซ ๐ ๐ธ. |
|||
|
|||
๐ ๐ฆ ๐งพ โ๏ธ (๐ โ โ๏ธ Kubernete) โ๏ธ ๐ซ ๐ธ โ ๐ ๏ธ ๐ ๐ซ. |
|||
|
|||
## ๐ฆ & ๐ ๏ธ |
|||
|
|||
**๐ฆ ๐ผ** ๐ ๐ ๐ฎ ๐ ๐ข ๐ โ๏ธ ๐ ๐ ๐ ๐ ๐โ **๐ฆ** โถ๏ธ & ๐ข ๐ถโโ๏ธ ๐ ๐. ๐ถ ๐ โซ๏ธโ ๐ ๐ฅ โซ๏ธ ๐ โธ. |
|||
|
|||
๐โ **๐ฆ** โถ๏ธ, โซ๏ธ ๐ ๐ ๐ ๐/๐ (๐ ๐ ๐ช ๐ โซ๏ธ & โ โซ๏ธ ๐ ๐ ๐/๐). |
|||
|
|||
๐ฆ ๐ ๐ **๐ ๐ ๏ธ** (๐ โ๏ธ ๐) ๐. |
|||
|
|||
๐ฆ ๐ โ๏ธ **๐ ๐ ๏ธ**, โ๏ธ โซ๏ธ ๐ช โถ๏ธ โณ โช๏ธโก๏ธ ๐ ๐ ๏ธ, & ๐ ๐ ๐ ๐ โ๏ธ **๐ ๐ ๏ธ** ๐ ๐ฆ. |
|||
|
|||
โ๏ธ โซ๏ธ ๐ซ ๐ช โ๏ธ ๐โโ ๐ฆ ๐ต **๐ 1๏ธโฃ ๐โโ ๐ ๏ธ**. ๐ฅ ๐ ๐ ๏ธ โ๏ธ, ๐ฆ โ๏ธ. |
|||
|
|||
## ๐ โ ๐ผ FastAPI |
|||
|
|||
๐, โก๏ธ ๐ ๐ณ ๐ โ ๐ถ |
|||
|
|||
๐ค ๐ ๐ฆ ๐ โ ๐ **โ ๐ผ** FastAPI **โช๏ธโก๏ธ ๐**, โ๏ธ ๐ **๐ ๐** ๐ผ. |
|||
|
|||
๐ โซ๏ธโ ๐ ๐ ๐ **๐ ๐ผ**, ๐ผ: |
|||
|
|||
* โ๏ธ **Kubernete** โ๏ธ ๐ ๐งฐ |
|||
* ๐โ ๐โโ ๐ **๐ ๐ฒ** |
|||
* โ๏ธ โ ๐โ๐ฆบ ๐ ๐ ๐ ๐ฆ ๐ผ ๐, โ๏ธ. |
|||
|
|||
### ๐ฆ ๐ |
|||
|
|||
๐ ๐ ๐ โ๏ธ **๐ฆ ๐** ๐ ๐ธ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ช โด๏ธ ๐ ๐งฐ ๐ โ๏ธ **โ** ๐ ๐. |
|||
|
|||
๐
โ ๐ โซ๏ธ โ๏ธ ๐ `requirements.txt` โฎ๏ธ ๐ฆ ๐ & ๐ซ โฌ, 1๏ธโฃ ๐ โธ. |
|||
|
|||
๐ ๐ โ๏ธ โ๏ธ ๐ ๐ญ ๐ โ [๐ FastAPI โฌ](./versions.md){.internal-link target=_blank} โ โ โฌ. |
|||
|
|||
๐ผ, ๐ `requirements.txt` ๐ช ๐ ๐: |
|||
|
|||
``` |
|||
fastapi>=0.68.0,<0.69.0 |
|||
pydantic>=1.8.0,<2.0.0 |
|||
uvicorn>=0.15.0,<0.16.0 |
|||
``` |
|||
|
|||
& ๐ ๐ ๐ โ ๐ ๐ฆ ๐ โฎ๏ธ `pip`, ๐ผ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install -r requirements.txt |
|||
---> 100% |
|||
Successfully installed fastapi pydantic uvicorn |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
!!! info |
|||
๐ค ๐ ๐ & ๐งฐ ๐ฌ & โ ๐ฆ ๐. |
|||
|
|||
๐ค ๐ ๐ฆ ๐ ๐ผ โ๏ธ ๐ถ โช ๐ ๐. ๐ถ |
|||
|
|||
### โ **FastAPI** ๐ |
|||
|
|||
* โ `app` ๐ & โ โซ๏ธ. |
|||
* โ ๐ ๐ `__init__.py`. |
|||
* โ `main.py` ๐ โฎ๏ธ: |
|||
|
|||
```Python |
|||
from typing import Union |
|||
|
|||
from fastapi import FastAPI |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.get("/") |
|||
def read_root(): |
|||
return {"Hello": "World"} |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
def read_item(item_id: int, q: Union[str, None] = None): |
|||
return {"item_id": item_id, "q": q} |
|||
``` |
|||
|
|||
### ๐ |
|||
|
|||
๐ ๐ ๐ ๐ โ ๐ `Dockerfile` โฎ๏ธ: |
|||
|
|||
```{ .dockerfile .annotate } |
|||
# (1) |
|||
FROM python:3.9 |
|||
|
|||
# (2) |
|||
WORKDIR /code |
|||
|
|||
# (3) |
|||
COPY ./requirements.txt /code/requirements.txt |
|||
|
|||
# (4) |
|||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt |
|||
|
|||
# (5) |
|||
COPY ./app /code/app |
|||
|
|||
# (6) |
|||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] |
|||
``` |
|||
|
|||
1๏ธโฃ. โถ๏ธ โช๏ธโก๏ธ ๐ ๐ ๐งข ๐ผ. |
|||
|
|||
2๏ธโฃ. โ โฎ๏ธ ๐ท ๐ `/code`. |
|||
|
|||
๐ ๐โ ๐ฅ ๐ ๐ฎ `requirements.txt` ๐ & `app` ๐. |
|||
|
|||
3๏ธโฃ. ๐ ๐ โฎ๏ธ ๐ `/code` ๐. |
|||
|
|||
๐ **๐ด** ๐ โฎ๏ธ ๐ ๐ฅ, ๐ซ ๐ ๐. |
|||
|
|||
๐ ๐ **๐ซ ๐ ๐**, โ ๐ ๐ โซ๏ธ & โ๏ธ **๐พ** ๐ ๐, ๐ ๏ธ ๐พ โญ ๐ ๐โโ๏ธ. |
|||
|
|||
4๏ธโฃ. โ ๐ฆ ๐ ๐ ๐. |
|||
|
|||
`--no-cache-dir` ๐ ๐ฌ `pip` ๐ซ ๐ โฌ ๐ฆ ๐, ๐ ๐ด ๐ฅ `pip` ๐ ๐ ๐ โ ๐ ๐ฆ, โ๏ธ ๐ ๐ซ ๐ผ ๐โ ๐ท โฎ๏ธ ๐ฆ. |
|||
|
|||
!!! note |
|||
`--no-cache-dir` ๐ด ๐ `pip`, โซ๏ธ โ๏ธ ๐ณ โฎ๏ธ โ โ๏ธ ๐ฆ. |
|||
|
|||
`--upgrade` ๐ ๐ฌ `pip` โป ๐ฆ ๐ฅ ๐ซ โช โ. |
|||
|
|||
โฉ๏ธ โฎ๏ธ ๐ ๐จ ๐ ๐ช ๐ **โ ๐พ**, ๐ ๐ ๐ **โ๏ธ โ ๐พ** ๐โ ๐ช. |
|||
|
|||
โ๏ธ ๐พ ๐ ๐ ๐ **๐** ๐ ๐ **๐ฐ** ๐โ ๐ ๐ผ ๐ & ๐ โฎ๏ธ ๐ ๏ธ, โฉ๏ธ **โฌ & โ** ๐ ๐ **๐ ๐ฐ**. |
|||
|
|||
5๏ธโฃ. ๐ `./app` ๐ ๐ `/code` ๐. |
|||
|
|||
๐ โ๏ธ ๐ ๐ โ โซ๏ธโ **๐ ๐
๐** โ **๐พ** ๐ ๐ซ โ๏ธ ๐ โ๏ธ ๐ **๐ ๐** ๐ช. |
|||
|
|||
, โซ๏ธ โ ๐ฎ ๐ **๐ ๐** `Dockerfile`, ๐ฌ ๐ฆ ๐ผ ๐ ๐ฐ. |
|||
|
|||
6๏ธโฃ. โ **๐** ๐ `uvicorn` ๐ฝ. |
|||
|
|||
`CMD` โ ๐ ๐ป, ๐ ๐ซ ๐ป โซ๏ธโ ๐ ๐ ๐ ๐ โธ ๐ฝ ๐. |
|||
|
|||
๐ ๐ ๐ ๐ โช๏ธโก๏ธ **โฎ๏ธ ๐ท ๐**, ๐ `/code` ๐ ๐ โ ๐ โฎ๏ธ `WORKDIR /code`. |
|||
|
|||
โฉ๏ธ ๐ ๐ โถ๏ธ `/code` & ๐ โซ๏ธ ๐ `./app` โฎ๏ธ ๐ ๐, **Uvicorn** ๐ ๐ช ๐ & **๐** `app` โช๏ธโก๏ธ `app.main`. |
|||
|
|||
!!! tip |
|||
๐ โซ๏ธโ ๐ โธ ๐จ ๐ ๐ ๐ข ๐ญ ๐. ๐ถ |
|||
|
|||
๐ ๐ ๐ โ๏ธ ๐ ๐ ๐: |
|||
|
|||
``` |
|||
. |
|||
โโโ app |
|||
โย ย โโโ __init__.py |
|||
โ โโโ main.py |
|||
โโโ Dockerfile |
|||
โโโ requirements.txt |
|||
``` |
|||
|
|||
#### โ
๐ค โ ๐ณ |
|||
|
|||
๐ฅ ๐ ๐โโ ๐ ๐ฆ โ
๐ค โ ๐ณ (๐ โ) ๐ ๐ โ๏ธ Traefik, ๐ฎ ๐ `--proxy-headers`, ๐ ๐ ๐ฌ Uvicorn ๐ ๐ ๐จ ๐ ๐ณ ๐ฌ โซ๏ธ ๐ ๐ธ ๐ โ
๐บ๐ธ๐, โ๏ธ. |
|||
|
|||
```Dockerfile |
|||
CMD ["uvicorn", "app.main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"] |
|||
``` |
|||
|
|||
#### โ ๐พ |
|||
|
|||
๐ค โ ๐ฑ ๐ `Dockerfile`, ๐ฅ ๐ฅ ๐ **๐ โฎ๏ธ ๐ ๐**, ๐ซ ๐ ๐. โก๏ธ ๐ค ๐ฌ ๐ โซ๏ธโ ๐. |
|||
|
|||
```Dockerfile |
|||
COPY ./requirements.txt /code/requirements.txt |
|||
``` |
|||
|
|||
โ & ๐ ๐งฐ **๐** ๐ ๐ฆ ๐ผ **๐**, ๐ฎ **1๏ธโฃ ๐งฝ ๐ ๐ ๐**, โถ๏ธ โช๏ธโก๏ธ ๐ `Dockerfile` & โ ๐ ๐ โ ๐ ๐ฉโ๐พ `Dockerfile`. |
|||
|
|||
โ & ๐ ๐งฐ โ๏ธ **๐ ๐พ** ๐โ ๐ ๐ผ, ๐ฅ ๐ ๐ซ ๐ โฉ๏ธ ๐ ๐ฐ ๐ ๐ฆ ๐ผ, โคด๏ธ โซ๏ธ ๐ **๐ค-โ๏ธ ๐ ๐งฝ** โ ๐ ๐ฐ, โฉ๏ธ ๐จ ๐ ๐ & ๐ ๐ ๐งฝ โช๏ธโก๏ธ ๐. |
|||
|
|||
โ ๐ ๐ ๐ซ ๐ฏ ๐ ๐ ๐โโ๏ธ ๐
, โ๏ธ โฉ๏ธ โซ๏ธ โ๏ธ ๐พ ๐ ๐, โซ๏ธ ๐ช **โ๏ธ ๐พ โญ ๐**. ๐ผ, โซ๏ธ ๐ช โ๏ธ ๐พ ๐ฉโ๐พ ๐ โ ๐ โฎ๏ธ: |
|||
|
|||
```Dockerfile |
|||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt |
|||
``` |
|||
|
|||
๐ โฎ๏ธ ๐ฆ ๐ **๐ ๐ซ ๐ ๐**. , ๐จ ๐ด ๐ ๐, โ ๐ ๐ช **โ๏ธ ๐พ** ๐ ๐. |
|||
|
|||
& โคด๏ธ, โ ๐ ๐ช **โ๏ธ ๐พ โญ ๐** ๐ โฌ & โ ๐ ๐. & ๐ฅ ๐โ ๐ฅ **๐ ๐ ๐ฐ**. ๐ถ ...& โ ๐ฉ โ. ๐ถ ๐ถ |
|||
|
|||
โฌ & โ ๐ฆ ๐ **๐ช โ โฒ**, โ๏ธ โ๏ธ **๐พ** ๐ **โ ๐ฅ** ๐
. |
|||
|
|||
& ๐ ๐ ๐ ๐ฆ ๐ผ ๐ & ๐ โฎ๏ธ ๐ ๏ธ โ
๐ ๐ ๐ ๐ ๐ท, ๐ค ๐ ๐ ๐ฐ ๐ ๐ ๐. |
|||
|
|||
โคด๏ธ, ๐ ๐ `Dockerfile`, ๐ฅ ๐ ๐ ๐. ๐ โซ๏ธโ **๐ ๐ ๐**, ๐ฅ ๐ฎ โซ๏ธ ๐ ๐, โฉ๏ธ ๐ ๐ง, ๐ณ โฎ๏ธ ๐ ๐ ๐ ๐ซ ๐ช โ๏ธ ๐พ. |
|||
|
|||
```Dockerfile |
|||
COPY ./app /code/app |
|||
``` |
|||
|
|||
### ๐ โ ๐ผ |
|||
|
|||
๐ ๐ ๐ ๐ ๐ฅ, โก๏ธ ๐ ๐ฆ ๐ผ. |
|||
|
|||
* ๐ถ ๐ ๐ (๐โ ๐ `Dockerfile` , โ ๐ `app` ๐). |
|||
* ๐ ๐ FastAPI ๐ผ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ docker build -t myimage . |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
!!! tip |
|||
๐ `.` ๐, โซ๏ธ ๐ `./`, โซ๏ธ ๐ฌ โ ๐ โ๏ธ ๐ ๐ฆ ๐ผ. |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ โฎ๏ธ ๐ (`.`). |
|||
|
|||
### โถ๏ธ โ ๐ฆ |
|||
|
|||
* ๐ ๐ฆ โ๏ธ ๐ ๐ ๐ผ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ docker run -d --name mycontainer -p 80:80 myimage |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
## โ
โซ๏ธ |
|||
|
|||
๐ ๐ ๐ช โ
โซ๏ธ ๐ โ ๐ฆ ๐, ๐ผ: <a href="http://192.168.99.100/items/5?q=somequery" class="external-link" target="_blank">http://192.168.99.100/items/5?q=somequery</a> โ๏ธ <a href="http://127.0.0.1/items/5?q=somequery" class="external-link" target="_blank">http://127.0.0.1/items/5?q=somequery</a> (โ๏ธ ๐, โ๏ธ ๐ โ ๐ฆ ). |
|||
|
|||
๐ ๐ ๐ ๐ณ ๐: |
|||
|
|||
```JSON |
|||
{"item_id": 5, "q": "somequery"} |
|||
``` |
|||
|
|||
## ๐ ๐ ๏ธ ๐ฉบ |
|||
|
|||
๐ ๐ ๐ช ๐ถ <a href="http://192.168.99.100/docs" class="external-link" target="_blank">http://192.168.99.100/docs</a> โ๏ธ <a href="http://127.0.0.1/docs" class="external-link" target="_blank">http://127.0.0.1/docs</a> (โ๏ธ ๐, โ๏ธ ๐ โ ๐ฆ ). |
|||
|
|||
๐ ๐ ๐ ๐ง ๐ ๐ ๏ธ ๐งพ (๐ <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">๐ฆ ๐</a>): |
|||
|
|||
 |
|||
|
|||
## ๐ ๐ ๏ธ ๐ฉบ |
|||
|
|||
& ๐ ๐ช ๐ถ <a href="http://192.168.99.100/redoc" class="external-link" target="_blank">http://192.168.99.100/redoc</a> โ๏ธ <a href="http://127.0.0.1/redoc" class="external-link" target="_blank">http://127.0.0.1/redoc</a> (โ๏ธ ๐, โ๏ธ ๐ โ ๐ฆ ). |
|||
|
|||
๐ ๐ ๐ ๐ ๐ง ๐งพ (๐ <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">๐</a>): |
|||
|
|||
 |
|||
|
|||
## ๐ โ ๐ผ โฎ๏ธ ๐-๐ FastAPI |
|||
|
|||
๐ฅ ๐ FastAPI ๐ ๐, ๐ผ, `main.py` ๐ต `./app` ๐, ๐ ๐ ๐ ๐ช ๐ ๐ ๐: |
|||
|
|||
``` |
|||
. |
|||
โโโ Dockerfile |
|||
โโโ main.py |
|||
โโโ requirements.txt |
|||
``` |
|||
|
|||
โคด๏ธ ๐ ๐ โ๏ธ ๐ ๐ โก ๐ ๐ ๐ `Dockerfile`: |
|||
|
|||
```{ .dockerfile .annotate hl_lines="10 13" } |
|||
FROM python:3.9 |
|||
|
|||
WORKDIR /code |
|||
|
|||
COPY ./requirements.txt /code/requirements.txt |
|||
|
|||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt |
|||
|
|||
# (1) |
|||
COPY ./main.py /code/ |
|||
|
|||
# (2) |
|||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"] |
|||
``` |
|||
|
|||
1๏ธโฃ. ๐ `main.py` ๐ `/code` ๐ ๐ (๐ต ๐ `./app` ๐). |
|||
|
|||
2๏ธโฃ. ๐ Uvicorn & ๐ฌ โซ๏ธ ๐ `app` ๐ โช๏ธโก๏ธ `main` (โฉ๏ธ ๐ญ โช๏ธโก๏ธ `app.main`). |
|||
|
|||
โคด๏ธ ๐ Uvicorn ๐ โ๏ธ ๐ ๐น `main` โฉ๏ธ `app.main` ๐ FastAPI ๐ `app`. |
|||
|
|||
## ๐ ๏ธ ๐ง |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ ๐ [๐ ๏ธ ๐ง](./concepts.md){.internal-link target=_blank} โ ๐ฆ. |
|||
|
|||
๐ฆ โด๏ธ ๐งฐ ๐ ๐ ๏ธ **๐ & ๐ ๏ธ** ๐ธ, โ๏ธ ๐ซ ๐ซ ๐ ๏ธ ๐ฏ ๐ฏ ๐ต ๐ **๐ ๏ธ ๐ง**, & ๐ค ๐ ๐ช ๐. |
|||
|
|||
**๐ ๐ฐ** ๐ โฎ๏ธ ๐ ๐ ๐ ๐ค ๐ ๐ ๐ ๐ ๏ธ ๐ง. ๐ถ |
|||
|
|||
โก๏ธ ๐ ๐ **๐ ๏ธ ๐ง** โ ๐ฆ: |
|||
|
|||
* ๐บ๐ธ๐ |
|||
* ๐โโ ๐ ๐ด |
|||
* โ |
|||
* ๐งฌ (๐ข ๐ ๏ธ ๐) |
|||
* ๐พ |
|||
* โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
## ๐บ๐ธ๐ |
|||
|
|||
๐ฅ ๐ฅ ๐ฏ ๐ **๐ฆ ๐ผ** FastAPI ๐ธ (& โช ๐โโ **๐ฆ**), ๐บ๐ธ๐ ๐ ๐ ๐ต **๐** โ1๏ธโฃ ๐งฐ. |
|||
|
|||
โซ๏ธ ๐ช โ1๏ธโฃ ๐ฆ, ๐ผ โฎ๏ธ <a href="https://traefik.io/" class="external-link" target="_blank">Traefik</a>, ๐ **๐บ๐ธ๐** & **๐ง** ๐ ๏ธ **๐**. |
|||
|
|||
!!! tip |
|||
Traefik โ๏ธ ๐ ๏ธ โฎ๏ธ โ, Kubernete, & ๐, โซ๏ธ ๐ถ โฉ โ ๐ & ๐ ๐บ๐ธ๐ ๐ ๐ฆ โฎ๏ธ โซ๏ธ. |
|||
|
|||
๐, ๐บ๐ธ๐ ๐ช ๐ต โ ๐โ๐ฆบ 1๏ธโฃ ๐ซ ๐โ๐ฆบ (โช ๐ ๐ธ ๐ฆ). |
|||
|
|||
## ๐โโ ๐ ๐ด & โ |
|||
|
|||
๐ค ๐ โ1๏ธโฃ ๐งฐ ๐ **โถ๏ธ & ๐โโ** ๐ ๐ฆ. |
|||
|
|||
โซ๏ธ ๐ช **โ** ๐, **โ โ**, **Kubernete**, **โ ๐โ๐ฆบ**, โ๏ธ. |
|||
|
|||
๐
(โ๏ธ ๐) ๐ผ, ๐ค ๐
๐ ๐ ๏ธ ๐ ๐ฆ ๐ ๐ด & ๐ ๏ธ โ ๐ โ. ๐ผ, โ, โซ๏ธ ๐ โธ ๐ `--restart`. |
|||
|
|||
๐ต โ๏ธ ๐ฆ, โ ๐ธ ๐ ๐ ๐ด & โฎ๏ธ โ ๐ช โ & โ . โ๏ธ ๐โ **๐ท โฎ๏ธ ๐ฆ** ๐
๐ผ ๐ ๐ ๏ธ ๐ ๐ข. ๐ถ |
|||
|
|||
## ๐งฌ - ๐ข ๐ ๏ธ |
|||
|
|||
๐ฅ ๐ โ๏ธ <abbr title="A group of machines that are configured to be connected and work together in some way.">๐</abbr> ๐ฐ โฎ๏ธ **โ**, โ ๐ ๐ณ, ๐, โ๏ธ โ1๏ธโฃ ๐ ๐ โ๏ธ ๐ ๏ธ ๐ ๐ฆ ๐ ๐ ๐ฐ, โคด๏ธ ๐ ๐ ๐ฒ ๐ **๐ต ๐งฌ** **๐ ๐** โฉ๏ธ โ๏ธ **๐ ๏ธ ๐จโ๐ผ** (๐ ๐ โฎ๏ธ ๐จโ๐ญ) ๐ ๐ฆ. |
|||
|
|||
1๏ธโฃ ๐ ๐ ๐ฆ ๐งพ โ๏ธ ๐ Kubernete ๐ โ๏ธ ๐ ๏ธ ๐ ๐ **๐งฌ ๐ฆ** โช ๐ **๐ โ** ๐จ ๐จ. ๐ **๐ ๐**. |
|||
|
|||
๐ ๐ผ, ๐ ๐ ๐ฒ ๐ ๐ **โ ๐ผ โช๏ธโก๏ธ ๐** [๐ฌ ๐](#dockerfile), โ ๐ ๐, & ๐โโ **๐ Uvicorn ๐ ๏ธ** โฉ๏ธ ๐โโ ๐ณ ๐ ๐ โฎ๏ธ Uvicorn ๐จโ๐ญ. |
|||
|
|||
### ๐ โ |
|||
|
|||
๐โ โ๏ธ ๐ฆ, ๐ ๐ ๐ โ๏ธ ๐ฆฒ **๐ ๐ ๐ โด**. โซ๏ธ ๐ช ๐ฒ โ1๏ธโฃ ๐ฆ ๐ **๐ค โ ๐ณ** ๐ต **๐บ๐ธ๐** โ๏ธ ๐ ๐งฐ. |
|||
|
|||
๐ ๐ฆฒ ๐ โ **๐** ๐จ & ๐ ๐ ๐ช ๐จโ๐ญ (๐ค) **โ** ๐, โซ๏ธ ๐ ๐ค **๐ โ**. |
|||
|
|||
!!! tip |
|||
๐ **๐ค โ ๐ณ** ๐ฆฒ โ๏ธ ๐บ๐ธ๐ ๐ ๐ฒ **๐ โ**. |
|||
|
|||
& ๐โ ๐ท โฎ๏ธ ๐ฆ, ๐ โ๏ธ ๐ โ๏ธ โถ๏ธ & ๐ ๏ธ ๐ซ ๐ โช โ๏ธ ๐ ๐งฐ ๐ถ **๐ธ ๐ป** (โ
๐บ๐ธ๐ ๐จ) โช๏ธโก๏ธ ๐ **๐ โ** (๐ ๐ช **๐ค โ ๐ณ**) ๐ฆ(โ) โฎ๏ธ ๐ ๐ฑ. |
|||
|
|||
### 1๏ธโฃ ๐ โ - ๐ ๐จโ๐ญ ๐ฆ |
|||
|
|||
๐โ ๐ท โฎ๏ธ **Kubernete** โ๏ธ ๐ ๐ ๐ฆ ๐งพ โ๏ธ, โ๏ธ ๐ซ ๐ ๐ธ ๐ ๏ธ ๐ โ ๐ **๐ โ** ๐ ๐ ๐ ๐ **โด** ๐ถ ๐ป (๐จ) ๐ฒ **๐ ๐ฆ** ๐ ๐ ๐ฑ. |
|||
|
|||
๐ ๐ซ ๐ฆ ๐โโ ๐ ๐ฑ ๐ ๐ โ๏ธ **1๏ธโฃ ๐ ๏ธ** (โ
Uvicorn ๐ ๏ธ ๐ ๐ FastAPI ๐ธ). ๐ซ ๐ ๐ **๐ ๐ฆ**, ๐โโ ๐ ๐, โ๏ธ ๐ โฎ๏ธ ๐ฎ ๐ ๐ ๏ธ, ๐พ, โ๏ธ. ๐ ๐ ๐ ๐ โ ๐ **๐ ๏ธ** **๐ ๐** ๐ฝ, โ๏ธ **๐ ๐ฐ**. |
|||
|
|||
& ๐ ๐ฆ โ๏ธ โฎ๏ธ **๐ โ** ๐ **๐ ๐จ** ๐ 1๏ธโฃ ๐ฆ โฎ๏ธ ๐ ๐ฑ **๐**. , ๐ ๐จ ๐ช ๐ต 1๏ธโฃ ๐ **๐ ๐ฆ** ๐ ๐ ๐ฑ. |
|||
|
|||
& ๐ ๐ **๐ โ** ๐ ๐ช ๐ต ๐จ ๐ ๐ถ *๐* ๐ฑ ๐ ๐ (โ
๐ ๐, โ๏ธ ๐ฝ ๐ ๐ โก ๐ก), & ๐ ๐ถ ๐ ๐ป โถ๏ธ๏ธ ๐ฆ *๐ ๐* ๐ธ ๐โโ ๐ ๐. |
|||
|
|||
### 1๏ธโฃ ๐ ๏ธ ๐ ๐ฆ |
|||
|
|||
๐ ๐ ๐, ๐ ๐ฒ ๐ ๐ โ๏ธ **๐ (Uvicorn) ๐ ๏ธ ๐ ๐ฆ**, ๐ ๐ โช ๐ ๐งฌ ๐ ๐. |
|||
|
|||
, ๐ ๐ผ, ๐ **๐ ๐ซ** ๐ โ๏ธ ๐ ๏ธ ๐จโ๐ผ ๐ ๐ โฎ๏ธ Uvicorn ๐จโ๐ญ, โ๏ธ Uvicorn โ๏ธ ๐ฎ ๐ Uvicorn ๐จโ๐ญ. ๐ ๐ ๐ โ๏ธ **๐ Uvicorn ๐ ๏ธ** ๐ ๐ฆ (โ๏ธ ๐ฒ ๐ ๐ฆ). |
|||
|
|||
โ๏ธ โ1๏ธโฃ ๐ ๏ธ ๐จโ๐ผ ๐ ๐ฆ (๐ โฎ๏ธ ๐ โ๏ธ Uvicorn ๐ ๏ธ Uvicorn ๐จโ๐ญ) ๐ ๐ด ๐ฎ **๐ ๐** ๐ ๐ ๐
๐ฒ โช โ ๐
โฎ๏ธ ๐ ๐ โ๏ธ. |
|||
|
|||
### ๐ฆ โฎ๏ธ ๐ ๐ ๏ธ & ๐ ๐ผ |
|||
|
|||
โ๏ธ, ๐ค **๐ ๐ผ** ๐โ ๐ ๐ช ๐ โ๏ธ **๐ฆ** โฎ๏ธ **๐ ๐ ๏ธ ๐จโ๐ผ** โถ๏ธ ๐ **Uvicorn ๐จโ๐ญ ๐ ๏ธ** ๐. |
|||
|
|||
๐ ๐ผ, ๐ ๐ช โ๏ธ **๐ โ ๐ผ** ๐ ๐ **๐** ๐ ๏ธ ๐จโ๐ผ ๐โโ ๐ **Uvicorn ๐จโ๐ญ ๐ ๏ธ**, & ๐ข โ ๐ ๐ข ๐จโ๐ญ โ๏ธ ๐ โฎ๏ธ ๐ฝ ๐ ๐. ๐ค ๐ ๐ฌ ๐ ๐
๐ โซ๏ธ ๐ [๐ โ ๐ผ โฎ๏ธ ๐ - Uvicorn](#official-docker-image-with-gunicorn-uvicorn). |
|||
|
|||
๐ฅ ๐ผ ๐โ ๐ ๐ช โ ๐: |
|||
|
|||
#### ๐
๐ฑ |
|||
|
|||
๐ ๐ช ๐ ๐ ๏ธ ๐จโ๐ผ ๐ฆ ๐ฅ ๐ ๐ธ **๐
๐ฅ** ๐ ๐ ๐ซ ๐ช (๐ฅ ๐ซ) ๐-๐ถ ๐ข ๐ ๏ธ ๐โโ๏ธ ๐
, & ๐ ๐ช โ๏ธ ๐ง ๐ข (โฎ๏ธ ๐ โ ๐ผ), & ๐ ๐โโ โซ๏ธ ๐ **๐ ๐ฝ**, ๐ซ ๐. |
|||
|
|||
#### โ โ |
|||
|
|||
๐ ๐ช ๐ ๏ธ **๐ ๐ฝ** (๐ซ ๐) โฎ๏ธ **โ โ**, ๐ ๐ซ๐ โ๏ธ โฉ ๐ ๐ ๏ธ ๐งฌ ๐ฆ (โฎ๏ธ โ โ) โช ๐ก ๐ ๐ธ & **๐ โ**. |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ โ๏ธ **๐ ๐ฆ** โฎ๏ธ **๐ ๏ธ ๐จโ๐ผ** โถ๏ธ **๐ ๐จโ๐ญ ๐ ๏ธ** ๐. |
|||
|
|||
#### ๐คด & ๐ ๐ค |
|||
|
|||
๐ ๐ช โ๏ธ **๐ ๐ค** ๐ ๐ โ โซ๏ธ โฉ โ๏ธ **๐ ๐ฆ** โฎ๏ธ **๐ ๐ ๏ธ** โฉ๏ธ โ๏ธ **๐ ๐ฆ** โฎ๏ธ **๐ ๐ ๏ธ** ๐ ๐ซ. |
|||
|
|||
๐ผ (๐ช ๐ ๐ ๐ฅ) ๐ ๐ช โ๏ธ ๐งฐ ๐ ๐คด ๐ญ ๐ ๐ฆ ๐ ๐ โ๏ธ ๐ **๐ ๐จ** ๐ ๐. |
|||
|
|||
๐ ๐ผ, ๐ฅ ๐ โ๏ธ **๐ ๐ฆ**, ๐ข, ๐โ ๐คด ๐ **โ โ**, โซ๏ธ ๐ ๐ค ๐ **๐ ๐ฆ ๐ ๐ฐ** (๐ฆ ๐ ๐ต ๐ ๐ฏ ๐จ), โฉ๏ธ ๐ค **๐ โ** ๐ ๐ ๐ฆ. |
|||
|
|||
โคด๏ธ, ๐ ๐ผ, โซ๏ธ ๐ช ๐
โ๏ธ **1๏ธโฃ ๐ฆ** โฎ๏ธ **๐ ๐ ๏ธ**, & ๐ง๐ฟ ๐งฐ (โ
๐คด ๐ญ) ๐ ๐ ๐ฆ ๐ ๐คด โ ๐ ๐ ๐ ๏ธ & ๐ฆ ๐ โ ๐ ๐ ๐ ๐ฆ. |
|||
|
|||
--- |
|||
|
|||
๐ โ, **๐** ๐ **๐ซ โ ๐ฟ** ๐ ๐ โ๏ธ ๐ โฉ. ๐ ๐ช โ๏ธ ๐ซ ๐ญ **๐ฌ ๐ ๐ โ๏ธ ๐ผ** & ๐ญ โซ๏ธโ ๐ ๐ฏ ๐ โ๏ธ, โ
๐
โ ๐ ๏ธ ๐ง: |
|||
|
|||
* ๐โโ - ๐บ๐ธ๐ |
|||
* ๐โโ ๐ ๐ด |
|||
* โ |
|||
* ๐งฌ (๐ข ๐ ๏ธ ๐) |
|||
* ๐พ |
|||
* โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
## ๐พ |
|||
|
|||
๐ฅ ๐ ๐ **๐ ๐ ๏ธ ๐ ๐ฆ** ๐ ๐ โ๏ธ ๐
โ๏ธ ๐ ๐-๐ฌ, โ, & ๐ ๐ธ ๐พ ๐ด ๐ ๐ ๐ฆ (๐
๐ 1๏ธโฃ ๐ฅ ๐ซ ๐). |
|||
|
|||
& โคด๏ธ ๐ ๐ช โ ๐ ๐ ๐พ ๐ & ๐ ๐ ๐ณ ๐ ๐ฆ ๐งพ โ๏ธ (๐ผ **Kubernete**). ๐ ๐ โซ๏ธ ๐ ๐ช **๐ ๐ฆ** **๐ช ๐ฐ** โ ๐ ๐ง ๐ธ ๐พ ๐ช ๐ซ, & ๐ธ ๐ช ๐ฐ ๐. |
|||
|
|||
๐ฅ ๐ ๐ธ **๐
**, ๐ ๐ ๐ฒ **๐ซ โ **, & ๐ ๐ช ๐ซ ๐ช โ ๐๏ธ ๐พ ๐. โ๏ธ ๐ฅ ๐ **โ๏ธ ๐ ๐พ** (๐ผ โฎ๏ธ **๐ฐ ๐ซ** ๐ท), ๐ ๐ โ
โ ๐
๐พ ๐ ๐ฉ & ๐ **๐ข ๐ฆ** ๐ ๐ **๐ ๐ฐ** (& ๐ฒ ๐ฎ ๐ ๐ฐ ๐ ๐). |
|||
|
|||
๐ฅ ๐ ๐ **๐ ๐ ๏ธ ๐ ๐ฆ** (๐ผ โฎ๏ธ ๐ โ ๐ผ) ๐ ๐ โ๏ธ โ ๐ญ ๐ ๐ข ๐ ๏ธ โถ๏ธ ๐ซ **๐ด ๐ ๐พ** ๐ โซ๏ธโ ๐ช. |
|||
|
|||
## โฎ๏ธ ๐ โญ โถ๏ธ & ๐ฆ |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ฆ (โ
โ, Kubernete), โคด๏ธ ๐ค 2๏ธโฃ ๐ ๐ฏ ๐ ๐ช โ๏ธ. |
|||
|
|||
### ๐ ๐ฆ |
|||
|
|||
๐ฅ ๐ โ๏ธ **๐ ๐ฆ**, ๐ฒ ๐ 1๏ธโฃ ๐ **๐ ๐ ๏ธ** (๐ผ, **Kubernete** ๐), โคด๏ธ ๐ ๐ ๐ฒ ๐ โ๏ธ **๐ ๐ฆ** ๐จ ๐ท **โฎ๏ธ ๐ถ** ๐ ๐ฆ, ๐ ๐ ๐ ๏ธ, **โญ** ๐ ๐ ๐จโ๐ญ ๐ฆ. |
|||
|
|||
!!! info |
|||
๐ฅ ๐ โ๏ธ Kubernete, ๐ ๐ ๐ฒ <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" class="external-link" target="_blank">๐ ๐ฆ</a>. |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ผ ๐ค ๐
โโ โ ๐โโ ๐ โฎ๏ธ ๐ถ **๐ ๐ฐ ๐** (๐ผ ๐ฅ ๐ ๐ซ ๐ ๐ฝ ๐ ๏ธ, โ๏ธ โ
๐ฅ ๐ฝ ๐), โคด๏ธ ๐ ๐ช ๐ฎ ๐ซ ๐ ๐ฆ โถ๏ธ๏ธ โญ โถ๏ธ ๐ ๐ ๏ธ. |
|||
|
|||
### ๐ ๐ฆ |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐
๐ฅ, โฎ๏ธ **๐ ๐ฆ** ๐ โคด๏ธ โถ๏ธ ๐ **๐จโ๐ญ ๐ ๏ธ** (โ๏ธ 1๏ธโฃ ๐ ๏ธ), โคด๏ธ ๐ ๐ช ๐ ๐ โฎ๏ธ ๐ ๐ ๐ฆ, โถ๏ธ๏ธ โญ โถ๏ธ ๐ ๏ธ โฎ๏ธ ๐ฑ. ๐ โ ๐ผ ๐โ๐ฆบ ๐ ๐. |
|||
|
|||
## ๐ โ ๐ผ โฎ๏ธ ๐ - Uvicorn |
|||
|
|||
๐ค ๐ โ ๐ผ ๐ ๐ ๐ ๐โโ โฎ๏ธ Uvicorn ๐จโ๐ญ, โน โฎ๏ธ ๐: [๐ฝ ๐จโ๐ญ - ๐ โฎ๏ธ Uvicorn](./server-workers.md){.internal-link target=_blank}. |
|||
|
|||
๐ ๐ผ ๐ โ โด๏ธ โ ๐ฌ ๐: [๐ฆ โฎ๏ธ ๐ ๐ ๏ธ & ๐ ๐ผ](#containers-with-multiple-processes-and-special-cases). |
|||
|
|||
* <a href="https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker" class="external-link" target="_blank">tiangolo/uvicorn-๐-fastapi</a>. |
|||
|
|||
!!! warning |
|||
๐ค โ ๐ค ๐ ๐ **๐ซ** ๐ช ๐ ๐งข ๐ผ โ๏ธ ๐ ๐ ๐ 1๏ธโฃ, & ๐ ๐ป ๐ ๐ ๐ผ โช๏ธโก๏ธ ๐ [๐ฌ ๐: ๐ โ ๐ผ FastAPI](#build-a-docker-image-for-fastapi). |
|||
|
|||
๐ ๐ผ โ๏ธ **๐-๐ณ** ๐ ๏ธ ๐ โ **๐ข ๐จโ๐ญ ๐ ๏ธ** โ๏ธ ๐ ๐ฝ ๐ ๐ช. |
|||
|
|||
โซ๏ธ โ๏ธ **๐ค ๐ข**, โ๏ธ ๐ ๐ช ๐ & โน ๐ ๐ณ โฎ๏ธ **๐ ๐ข** โ๏ธ ๐ณ ๐. |
|||
|
|||
โซ๏ธ ๐โ๐ฆบ ๐ <a href="https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker#pre_start_path" class="external-link" target="_blank">**โฎ๏ธ ๐ โญ โถ๏ธ**</a> โฎ๏ธ โ. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ณ & ๐, ๐ถ โ ๐ผ ๐: <a href="https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker" class="external-link" target="_blank">Tiangolo/uvicorn-๐-fastapi</a>. |
|||
|
|||
### ๐ข ๐ ๏ธ ๐ ๐ โ ๐ผ |
|||
|
|||
**๐ข ๐ ๏ธ** ๐ ๐ ๐ผ **๐ ๐** โช๏ธโก๏ธ ๐ฝ **๐** ๐ช. |
|||
|
|||
๐ โ ๐ โซ๏ธ ๐ ๐ **๐** ๐
**๐ญ** โช๏ธโก๏ธ ๐ฝ ๐ช. |
|||
|
|||
๐ ๐ช ๐ โซ๏ธ โฎ๏ธ ๐ณ โ๏ธ **๐ ๐ข**, โ๏ธ. |
|||
|
|||
โ๏ธ โซ๏ธ โ ๐ ๐ข ๐ ๏ธ ๐ช ๐ ๐ฝ ๐ฆ ๐, **๐ธ ๐พ ๐ด** ๐ ๐ช ๐ ๐. |
|||
|
|||
, ๐ฅ ๐ ๐ธ ๐ด ๐ ๐พ (๐ผ โฎ๏ธ ๐ฐ ๐ซ ๐ท), & ๐ ๐ฝ โ๏ธ ๐ ๐ฝ ๐ **โ๏ธ ๐ฅ ๐พ**, โคด๏ธ ๐ ๐ฆ ๐ช ๐ ๐ ๐ โ๏ธ ๐
๐พ ๐ โซ๏ธโ ๐ช, & ๐ค ๐ญ ๐ (โ๏ธ ๐ฅ). ๐ถ |
|||
|
|||
### โ `Dockerfile` |
|||
|
|||
๐ฅ โ ๐ ๐ โ `Dockerfile` โ๏ธ ๐ ๐ ๐ผ: |
|||
|
|||
```Dockerfile |
|||
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9 |
|||
|
|||
COPY ./requirements.txt /app/requirements.txt |
|||
|
|||
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt |
|||
|
|||
COPY ./app /app |
|||
``` |
|||
|
|||
### ๐ฆ ๐ธ |
|||
|
|||
๐ฅ ๐ โฉ ๐ ๐ ๐ [๐ฆ ๐ธ โฎ๏ธ ๐ ๐](../tutorial/bigger-applications.md){.internal-link target=_blank}, ๐ `Dockerfile` ๐ช โฉ๏ธ ๐ ๐: |
|||
|
|||
```Dockerfile hl_lines="7" |
|||
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9 |
|||
|
|||
COPY ./requirements.txt /app/requirements.txt |
|||
|
|||
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt |
|||
|
|||
COPY ./app /app/app |
|||
``` |
|||
|
|||
### ๐โ โ๏ธ |
|||
|
|||
๐ ๐ ๐ฒ **๐ซ** โ๏ธ ๐ ๐ ๐งข ๐ผ (โ๏ธ ๐ ๐ ๐ 1๏ธโฃ) ๐ฅ ๐ โ๏ธ **Kubernete** (โ๏ธ ๐) & ๐ โช โ **๐งฌ** ๐ ๐, โฎ๏ธ ๐ **๐ฆ**. ๐ ๐ผ, ๐ ๐ ๐ **๐ ๐ผ โช๏ธโก๏ธ ๐** ๐ฌ ๐: [๐ โ ๐ผ FastAPI](#build-a-docker-image-for-fastapi). |
|||
|
|||
๐ ๐ผ ๐ โ โด๏ธ ๐ ๐ผ ๐ฌ ๐ [๐ฆ โฎ๏ธ ๐ ๐ ๏ธ & ๐ ๐ผ](#containers-with-multiple-processes-and-special-cases). ๐ผ, ๐ฅ ๐ ๐ธ **๐
๐ฅ** ๐ โ ๐ข ๐ข ๐ ๏ธ โ๏ธ ๐ ๐ฝ ๐ท ๐, ๐ ๐ซ ๐ ๐ฅ โฎ๏ธ โ ๐ ๏ธ ๐งฌ ๐ ๐, & ๐ ๐ซ ๐ ๐
๐ 1๏ธโฃ ๐ฆ โฎ๏ธ ๐ ๐ฑ. โ๏ธ ๐ฅ ๐ ๐ ๏ธ โฎ๏ธ **โ โ**, ๐ ๐ ๐ ๐ฝ, โ๏ธ. |
|||
|
|||
## ๐ ๏ธ ๐ฆ ๐ผ |
|||
|
|||
โฎ๏ธ โ๏ธ ๐ฆ (โ) ๐ผ ๐ค ๐ ๐ ๐ ๏ธ โซ๏ธ. |
|||
|
|||
๐ผ: |
|||
|
|||
* โฎ๏ธ **โ โ** ๐ ๐ฝ |
|||
* โฎ๏ธ **Kubernete** ๐ |
|||
* โฎ๏ธ โ ๐ ๐ณ ๐ |
|||
* โฎ๏ธ โ1๏ธโฃ ๐งฐ ๐ ๐ |
|||
* โฎ๏ธ โ ๐โ๐ฆบ ๐ โ ๐ ๐ฆ ๐ผ & ๐ ๏ธ โซ๏ธ |
|||
|
|||
## โ ๐ผ โฎ๏ธ ๐ถ |
|||
|
|||
๐ฅ ๐ โ๏ธ <a href="https://python-poetry.org/" class="external-link" target="_blank">๐ถ</a> ๐ ๏ธ ๐ ๐ ๐, ๐ ๐ช โ๏ธ โ ๐-โถ๏ธ ๐: |
|||
|
|||
```{ .dockerfile .annotate } |
|||
# (1) |
|||
FROM python:3.9 as requirements-stage |
|||
|
|||
# (2) |
|||
WORKDIR /tmp |
|||
|
|||
# (3) |
|||
RUN pip install poetry |
|||
|
|||
# (4) |
|||
COPY ./pyproject.toml ./poetry.lock* /tmp/ |
|||
|
|||
# (5) |
|||
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes |
|||
|
|||
# (6) |
|||
FROM python:3.9 |
|||
|
|||
# (7) |
|||
WORKDIR /code |
|||
|
|||
# (8) |
|||
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt |
|||
|
|||
# (9) |
|||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt |
|||
|
|||
# (10) |
|||
COPY ./app /code/app |
|||
|
|||
# (11) |
|||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] |
|||
``` |
|||
|
|||
1๏ธโฃ. ๐ ๐ฅ โถ๏ธ, โซ๏ธ ๐ `requirements-stage`. |
|||
|
|||
2๏ธโฃ. โ `/tmp` โฎ๏ธ ๐ท ๐. |
|||
|
|||
๐ฅ ๐โ ๐ฅ ๐ ๐ ๐ `requirements.txt` |
|||
|
|||
3๏ธโฃ. โ ๐ถ ๐ โ โถ๏ธ. |
|||
|
|||
4๏ธโฃ. ๐ `pyproject.toml` & `poetry.lock` ๐ `/tmp` ๐. |
|||
|
|||
โฉ๏ธ โซ๏ธ โ๏ธ `./poetry.lock*` (โถ๏ธ โฎ๏ธ `*`), โซ๏ธ ๐ ๐ซ ๐ฅ ๐ฅ ๐ ๐ ๐ซ ๐ช. |
|||
|
|||
5๏ธโฃ. ๐ `requirements.txt` ๐. |
|||
|
|||
6๏ธโฃ. ๐ ๐ โถ๏ธ, ๐ณ ๐ฅ ๐ ๐ก ๐ ๐ฆ ๐ผ. |
|||
|
|||
7๏ธโฃ. โ โฎ๏ธ ๐ท ๐ `/code`. |
|||
|
|||
8๏ธโฃ. ๐ `requirements.txt` ๐ `/code` ๐. |
|||
|
|||
๐ ๐ ๐ด ๐ โฎ๏ธ โ โถ๏ธ, ๐ โซ๏ธโ ๐ฅ โ๏ธ `--from-requirements-stage` ๐ โซ๏ธ. |
|||
|
|||
9๏ธโฃ. โ ๐ฆ ๐ ๐ `requirements.txt` ๐. |
|||
|
|||
1๏ธโฃ0๏ธโฃ. ๐ `app` ๐ `/code` ๐. |
|||
|
|||
1๏ธโฃ1๏ธโฃ. ๐ `uvicorn` ๐, ๐ฌ โซ๏ธ โ๏ธ `app` ๐ ๐ โช๏ธโก๏ธ `app.main`. |
|||
|
|||
!!! tip |
|||
๐ ๐ญ ๐ข ๐ โซ๏ธโ ๐ โธ ๐จ. |
|||
|
|||
**โ โถ๏ธ** ๐ `Dockerfile` ๐ ๐ท **๐ ๐ฆ ๐ผ** ๐ ๐ด โ๏ธ ๐ ๐ โ๏ธ โช. |
|||
|
|||
๐ฅ โถ๏ธ ๐ ๐ด โ๏ธ **โ ๐ถ** & **๐ `requirements.txt`** โฎ๏ธ ๐ ๐ ๐ โช๏ธโก๏ธ ๐ถ `pyproject.toml` ๐. |
|||
|
|||
๐ `requirements.txt` ๐ ๐ โ๏ธ โฎ๏ธ `pip` โช **โญ โถ๏ธ**. |
|||
|
|||
๐ ๐ฆ ๐ผ **๐ด ๐ โถ๏ธ** ๐ก. โฎ๏ธ โถ๏ธ(โ) ๐ โ. |
|||
|
|||
๐โ โ๏ธ ๐ถ, โซ๏ธ ๐ โ ๐ โ๏ธ **โ ๐-โถ๏ธ ๐** โฉ๏ธ ๐ ๐ซ ๐ค ๐ช โ๏ธ ๐ถ & ๐ฎ ๐ โ ๐ ๐ฆ ๐ผ, ๐ **๐ด ๐ช** โ๏ธ ๐ `requirements.txt` ๐ โ ๐ ๐ ๐. |
|||
|
|||
โคด๏ธ โญ (& ๐) โถ๏ธ ๐ ๐ ๐ ๐ผ ๐
โ๏ธ ๐ ๐ ๐ ๐ฌ โญ. |
|||
|
|||
### โ
๐ค โ ๐ณ - ๐ถ |
|||
|
|||
๐, ๐ฅ ๐ ๐โโ ๐ ๐ฆ โ
๐ค โ ๐ณ (๐ โ) ๐ ๐ โ๏ธ Traefik, ๐ฎ ๐ `--proxy-headers` ๐: |
|||
|
|||
```Dockerfile |
|||
CMD ["uvicorn", "app.main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"] |
|||
``` |
|||
|
|||
## ๐ |
|||
|
|||
โ๏ธ ๐ฆ โ๏ธ (โ
โฎ๏ธ **โ** & **Kubernete**) โซ๏ธ โถ๏ธ๏ธ ๐ถ ๐ฏ ๐ต ๐ **๐ ๏ธ ๐ง**: |
|||
|
|||
* ๐บ๐ธ๐ |
|||
* ๐โโ ๐ ๐ด |
|||
* โ |
|||
* ๐งฌ (๐ข ๐ ๏ธ ๐) |
|||
* ๐พ |
|||
* โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
๐
๐ผ, ๐ ๐ฒ ๐ ๐ซ ๐ โ๏ธ ๐ ๐งข ๐ผ, & โฉ๏ธ **๐ ๐ฆ ๐ผ โช๏ธโก๏ธ ๐** 1๏ธโฃ โ๏ธ ๐ ๐ ๐ โ ๐ผ. |
|||
|
|||
โ ๐
**โ** ๐ฉโ๐พ `Dockerfile` & **โ ๐พ** ๐ ๐ช **๐ ๐ ๐ฐ**, ๐ ๐ ๐ (& โ ๐ฉ). ๐ถ |
|||
|
|||
๐ฏ ๐ ๐ผ, ๐ ๐ช ๐ โ๏ธ ๐ โ ๐ผ FastAPI. ๐ถ |
@ -0,0 +1,190 @@ |
|||
# ๐ ๐บ๐ธ๐ |
|||
|
|||
โซ๏ธ โฉ ๐ค ๐ ๐บ๐ธ๐ ๐ณ ๐ "๐ ๏ธ" โ๏ธ ๐ซ. |
|||
|
|||
โ๏ธ โซ๏ธ ๐ ๐ ๐ ๐ ๐. |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ โ๏ธ ๐ซ ๐
, ๐ฃ โฎ๏ธ โญ ๐ ๐ ๐ ๐ฉโ๐พ โ ๐ ๐ โฎ๏ธ ๐ โ. |
|||
|
|||
**๐ก ๐ฐ ๐บ๐ธ๐**, โช๏ธโก๏ธ ๐ฌ ๐ค, โ
<a href="https://howhttps.works/" class="external-link" target="_blank">https://howhttps.works/</a>. |
|||
|
|||
๐, โช๏ธโก๏ธ **๐ฉโ๐ป ๐ค**, ๐ฅ ๐ ๐ โ๏ธ ๐คฏ โช ๐ญ ๐ ๐บ๐ธ๐: |
|||
|
|||
* ๐บ๐ธ๐, **๐ฝ** ๐ช **โ๏ธ "๐"** ๐ **๐ฅ ๐ฅณ**. |
|||
* ๐ ๐ ๐ค **๐** โช๏ธโก๏ธ ๐ฅ ๐ฅณ, ๐ซ "๐". |
|||
* ๐ โ๏ธ **1๏ธโฃ2๏ธโฃ๐๏ธ**. |
|||
* ๐ซ **๐**. |
|||
* & โคด๏ธ ๐ซ ๐ช **โป**, **๐ ๐** โช๏ธโก๏ธ ๐ฅ ๐ฅณ. |
|||
* ๐ ๐ ๐จ **๐ธ ๐**. |
|||
* ๐ 1๏ธโฃ ๐งฝ **๐ ๐บ๐ธ๐**. |
|||
* , **๐ & ๐** ๐ต ๐จ **โญ ๐บ๐ธ๐**. |
|||
* **๐ธ ๐ซ ๐ญ ๐ "๐"**. ๐ด ๐ ๐ข ๐ข. |
|||
* โน ๐ **๐ฏ ๐** ๐จ ๐ถ **๐บ๐ธ๐ ๐ฝ**. |
|||
* **๐บ๐ธ๐ ๐** "โ" **๐ฏ ๐**, โ๏ธ ๐ ๏ธ & ๐ ๐จ ๐ธ ๐, **โญ ๐ญ** โ ๐ โ ๐
โฎ๏ธ. |
|||
* **๐ข**, ๐ ๐ โ ๐ ๐ ๐ช ๐ด โ๏ธ **1๏ธโฃ ๐บ๐ธ๐ ๐ ๐ ๐ข ๐ข**. |
|||
* ๐
โโ ๐ค โ ๐ฆ ๐ ๐ฝ โ๏ธ โ ๐คช ๐ ๐ธ ๐ โ๏ธ ๐ โซ๏ธ ๐ช. |
|||
* ๐ค **โ** ๐, ๐. |
|||
* ๐ค **โ** **๐ค** ๐ ๏ธ (1๏ธโฃ ๐ ๐ ๐ธ ๐, โญ ๐บ๐ธ๐) ๐ค **<a href="https://en.wikipedia.org/wiki/Server_Name_Indication" class="external-link" target="_blank"><abbr title="Server Name Indication">๐ฒ</abbr></a>**. |
|||
* ๐ ๐ฒ โ โ 1๏ธโฃ ๐ ๐ฝ (โฎ๏ธ **๐ ๐ข ๐ข**) โ๏ธ **๐ ๐บ๐ธ๐ ๐** & ๐ฆ **๐ ๐บ๐ธ๐ ๐/๐ธ**. |
|||
* ๐ ๐ท, **๐** ๐ฆฒ (๐) ๐ ๐ ๐ฝ, ๐ ๐ **๐ข ๐ข ๐ข**, ๐ โ๏ธ **๐ ๐บ๐ธ๐ ๐** ๐ฝ. |
|||
* **โฎ๏ธ** ๐ ๐ ๐, ๐ป ๐ ๏ธ **๐บ๐ธ๐**. |
|||
* ๐ **๐**, โ๏ธ ๐ซ โ ๐จ โฎ๏ธ **๐บ๐ธ๐ ๐ ๏ธ**. |
|||
|
|||
โซ๏ธ โ ๐ก โ๏ธ **1๏ธโฃ ๐/๐บ๐ธ๐ ๐ฝ** ๐ ๐ ๐ฝ (๐ฐ, ๐ฆ , โ๏ธ.) & **๐ ๏ธ ๐ ๐บ๐ธ๐ ๐**: ๐จ **๐ ๐บ๐ธ๐ ๐จ**, ๐จ **๐ ๐บ๐ธ๐ ๐จ** โ ๐บ๐ธ๐ ๐ธ ๐ ๐ ๐ฝ ( **FastAPI** ๐ธ, ๐ ๐ผ), โ **๐บ๐ธ๐ ๐จ** โช๏ธโก๏ธ ๐ธ, **๐ โซ๏ธ** โ๏ธ โ **๐บ๐ธ๐ ๐** & ๐จ โซ๏ธ ๐ ๐ฉโ๐ป โ๏ธ **๐บ๐ธ๐**. ๐ ๐ฝ ๐ ๐ค **<a href="https://en.wikipedia.org/wiki/TLS_termination_proxy" class="external-link" target="_blank">๐ค โ ๐ณ</a>**. |
|||
|
|||
๐ ๐ ๐ช โ๏ธ ๐ค โ ๐ณ: |
|||
|
|||
* Traefik (๐ ๐ช ๐ต ๐ ๐) |
|||
* ๐ฅ (๐ ๐ช ๐ต ๐ ๐) |
|||
* ๐ |
|||
* โณ |
|||
|
|||
## โก๏ธ ๐ |
|||
|
|||
โญ โก๏ธ ๐, ๐ซ **๐บ๐ธ๐ ๐** ๐ฒ ๐ ๐ฅ ๐ฅณ. |
|||
|
|||
๐ ๏ธ ๐ 1๏ธโฃ ๐ซ ๐ โ๏ธ โ , ๐ ๐ & ๐ ๐ฅ. |
|||
|
|||
โ๏ธ โคด๏ธ **<a href="https://letsencrypt.org/" class="external-link" target="_blank">โก๏ธ ๐</a>** โ. |
|||
|
|||
โซ๏ธ ๐ โช๏ธโก๏ธ ๐พ ๐. โซ๏ธ ๐ **๐บ๐ธ๐ ๐ ๐**, ๐ง ๐. ๐ซ ๐ โ๏ธ ๐ ๐ฉ ๐ ๐โโ, & ๐-๐ (๐ 3๏ธโฃ ๐๏ธ), **๐โโ ๐ค ๐** โฉ๏ธ ๐ซ ๐ ๐. |
|||
|
|||
๐ ๐ โ & ๐ ๐ ๐. ๐ โ ๐ง ๐ ๐ซ ๐. |
|||
|
|||
๐ญ ๐ง ๐ ๏ธ & ๐ ๐ซ ๐ ๐ ๐ ๐ช โ๏ธ **๐ ๐บ๐ธ๐, ๐, โพ**. |
|||
|
|||
## ๐บ๐ธ๐ ๐ฉโ๐ป |
|||
|
|||
๐ฅ ๐ผ โ ๐บ๐ธ๐ ๐ ๏ธ ๐ช ๐ ๐, ๐ ๐, ๐ธ ๐ โด๏ธ ๐ญ โ ๐ฉโ๐ป. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
โซ๏ธ ๐ ๐ฒ ๐ โถ๏ธ ๐ **๐** **๐ ๐**. โคด๏ธ, ๐ ๐ ๐ โซ๏ธ ๐ ๐ฝ (๐ฒ ๐ ๐ โ ๐โ๐ฆบ). |
|||
|
|||
๐ ๐ ๐ฒ ๐ค โ ๐ฝ (๐น ๐ฐ) โ๏ธ ๐ณ ๐, & โซ๏ธ ๐ โ๏ธ <abbr title="That doesn't change">๐ง</abbr> **๐ข ๐ข ๐ข**. |
|||
|
|||
๐ ๐ฝ(โ) ๐ ๐ ๐ โบ ("`A record`") โ **๐ ๐** ๐ข **๐ข ๐ข ๐ ๐ฝ**. |
|||
|
|||
๐ ๐ ๐ฒ ๐ ๐, ๐ฅ ๐ฐ, ๐โ โ ๐ ๐. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ ๐ ๐ โญ ๐บ๐ธ๐, โ๏ธ ๐ ๐ช ๐ ๐ & ๐ข ๐ข, โซ๏ธ ๐ธ ๐ฌ โซ๏ธ ๐ฅ. |
|||
|
|||
### ๐ |
|||
|
|||
๐ โก๏ธ ๐ฏ ๐ ๐ โ ๐บ๐ธ๐ ๐. |
|||
|
|||
๐ฅ, ๐ฅ ๐ โ
โฎ๏ธ **๐ ๐ฝ** โซ๏ธโ **๐ข ๐**, ๐ ๐ผ, `someapp.example.com`. |
|||
|
|||
๐ ๐ฝ ๐ ๐ฌ ๐ฅ โ๏ธ ๐ฏ **๐ข ๐ข**. ๐ ๐ ๐ข ๐ข ๐ข โ๏ธ ๐ ๐ฝ, ๐ ๐ ๐ ๐ ๐ฝ. |
|||
|
|||
<img src="/img/deployment/https/https01.svg"> |
|||
|
|||
### ๐ค ๐ค โถ๏ธ |
|||
|
|||
๐ฅ ๐ โคด๏ธ ๐ โฎ๏ธ ๐ ๐ข ๐ข ๐ **โด 4๏ธโฃ4๏ธโฃ3๏ธโฃ** (๐บ๐ธ๐ โด). |
|||
|
|||
๐ฅ ๐ ๐ป ๐ ๏ธ ๐ ๐ ๐ฉโ๐ป & ๐ฝ & ๐ญ ๐ ๐ ๐ซ ๐ โ๏ธ, โ๏ธ. |
|||
|
|||
<img src="/img/deployment/https/https02.svg"> |
|||
|
|||
๐ ๐ ๐ ๐ฉโ๐ป & ๐ฝ ๐ ๏ธ ๐ค ๐ ๐ค **๐ค ๐ค**. |
|||
|
|||
### ๐ค โฎ๏ธ ๐ฒ โ |
|||
|
|||
**๐ด 1๏ธโฃ ๐ ๏ธ** ๐ฝ ๐ช ๐ ๐ ๐ฏ **โด** ๐ฏ **๐ข ๐ข**. ๐ค ๐ช ๐ ๐ ๏ธ ๐ ๐ ๐ โด ๐ ๐ข ๐ข, โ๏ธ ๐ด 1๏ธโฃ ๐ ๐ ๐ข ๐ข & โด. |
|||
|
|||
๐ค (๐บ๐ธ๐) โ๏ธ ๐ฏ โด `443` ๐ข. ๐ โด ๐ฅ ๐ ๐ช. |
|||
|
|||
๐ด 1๏ธโฃ ๐ ๏ธ ๐ช ๐ ๐ ๐ โด, ๐ ๏ธ ๐ ๐ โซ๏ธ ๐ **๐ค โ ๐ณ**. |
|||
|
|||
๐ค โ ๐ณ ๐ โ๏ธ ๐ 1๏ธโฃ โ๏ธ ๐
**๐ค ๐** (๐บ๐ธ๐ ๐). |
|||
|
|||
โ๏ธ **๐ฒ โ** ๐ฌ ๐, ๐ค โ ๐ณ ๐ โ
โ ๐ค (๐บ๐ธ๐) ๐ ๐ช โซ๏ธ ๐ โ๏ธ ๐ ๐, โ๏ธ 1๏ธโฃ ๐ ๐ ๐ ๐ ๐ฉโ๐ป. |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ โ๏ธ ๐ `someapp.example.com`. |
|||
|
|||
<img src="/img/deployment/https/https03.svg"> |
|||
|
|||
๐ฉโ๐ป โช **๐** ๐จโ๐ผ ๐ ๐ ๐ ๐ค ๐ (๐ ๐ผ โก๏ธ ๐, โ๏ธ ๐ฅ ๐ ๐ ๐ ๐ โช), โซ๏ธ ๐ช **โ** ๐ ๐ โ. |
|||
|
|||
โคด๏ธ, โ๏ธ ๐, ๐ฉโ๐ป & ๐ค โ ๐ณ **๐ญ โ ๐** ๐ **๐ธ ๐ป**. ๐ ๐ **๐ค ๐ค** ๐. |
|||
|
|||
โฎ๏ธ ๐, ๐ฉโ๐ป & ๐ฝ โ๏ธ **๐ ๐ธ ๐**, ๐ โซ๏ธโ ๐ค ๐. & โคด๏ธ ๐ซ ๐ช โ๏ธ ๐ ๐ โถ๏ธ โ **๐บ๐ธ๐ ๐ป**. |
|||
|
|||
& ๐ โซ๏ธโ **๐บ๐ธ๐** , โซ๏ธ โ
**๐บ๐ธ๐** ๐ **๐ ๐ค ๐** โฉ๏ธ ๐ (๐ฝ) ๐ธ ๐. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ ๐ป ๐จ **๐ธ ๐**, ๐ซ ๐บ๐ธ๐ ๐. |
|||
|
|||
### ๐บ๐ธ๐ ๐จ |
|||
|
|||
๐ ๐ ๐ฉโ๐ป & ๐ฝ (๐ฏ ๐ฅ & ๐ค โ ๐ณ) โ๏ธ **๐ ๐ธ ๐**, ๐ซ ๐ช โถ๏ธ **๐บ๐ธ๐ ๐ป**. |
|||
|
|||
, ๐ฉโ๐ป ๐จ **๐บ๐ธ๐ ๐จ**. ๐ ๐บ๐ธ๐ ๐จ ๐ ๐ ๐ค ๐. |
|||
|
|||
<img src="/img/deployment/https/https04.svg"> |
|||
|
|||
### ๐ ๐จ |
|||
|
|||
๐ค โ ๐ณ ๐ โ๏ธ ๐ โ **๐ ๐จ**, & ๐ ๐ถ **โ
(๐) ๐บ๐ธ๐ ๐จ** ๐ ๏ธ ๐ ๐ธ (๐ผ ๐ ๏ธ โฎ๏ธ Uvicorn ๐โโ FastAPI ๐ธ). |
|||
|
|||
<img src="/img/deployment/https/https05.svg"> |
|||
|
|||
### ๐บ๐ธ๐ ๐จ |
|||
|
|||
๐ธ ๐ ๐ ๏ธ ๐จ & ๐จ **โ
(๐ฝ) ๐บ๐ธ๐ ๐จ** ๐ค โ ๐ณ. |
|||
|
|||
<img src="/img/deployment/https/https06.svg"> |
|||
|
|||
### ๐บ๐ธ๐ ๐จ |
|||
|
|||
๐ค โ ๐ณ ๐ โคด๏ธ **๐ ๐จ** โ๏ธ โ โ โญ (๐ โถ๏ธ โฎ๏ธ ๐ `someapp.example.com`), & ๐จ โซ๏ธ ๐ ๐ฅ. |
|||
|
|||
โญ, ๐ฅ ๐ โ ๐ ๐จ โ & ๐ โฎ๏ธ โถ๏ธ๏ธ ๐ ๐, โ๏ธ. โซ๏ธ ๐ โคด๏ธ **๐ ๐จ** & ๐ ๏ธ โซ๏ธ. |
|||
|
|||
<img src="/img/deployment/https/https07.svg"> |
|||
|
|||
๐ฉโ๐ป (๐ฅ) ๐ ๐ญ ๐ ๐จ ๐ โช๏ธโก๏ธ โ ๐ฝ โฉ๏ธ โซ๏ธ โ๏ธ โ ๐ซ โ โ๏ธ **๐บ๐ธ๐ ๐** โญ. |
|||
|
|||
### ๐ ๐ธ |
|||
|
|||
๐ ๐ฝ (โ๏ธ ๐ฝ), ๐ค ๐ช **๐ ๐ธ**, ๐ผ, ๐ ๐ ๏ธ ๐ โ๏ธ ๐ฝ. |
|||
|
|||
๐ด 1๏ธโฃ ๐ ๏ธ ๐ช ๐ ๐ฏ ๐ข & โด (๐ค โ ๐ณ ๐ ๐ผ) โ๏ธ ๐ ๐ธ/๐ ๏ธ ๐ช ๐ ๐ ๐ฝ(โ) ๐โโ๏ธ, ๐ ๐ซ ๐ซ ๐ โ๏ธ ๐ **๐ ๐ข ๐ข & โด**. |
|||
|
|||
<img src="/img/deployment/https/https08.svg"> |
|||
|
|||
๐ ๐, ๐ค โ ๐ณ ๐ช ๐ต ๐บ๐ธ๐ & ๐ **๐ ๐**, ๐ ๐ธ, & โคด๏ธ ๐ถ ๐จ โถ๏ธ๏ธ ๐ธ ๐ ๐ผ. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
โ ๐ฎ, ๐ ๐ ๐ **๐** (๐ 3๏ธโฃ ๐๏ธ โฎ๏ธ ๐ โซ๏ธ). |
|||
|
|||
& โคด๏ธ, ๐ค ๐ โ1๏ธโฃ ๐ (๐ผ โซ๏ธ โ1๏ธโฃ ๐, ๐ผ โซ๏ธ ๐ช ๐ ๐ค โ ๐ณ) ๐ ๐ ๐ฌ โก๏ธ ๐, & โป ๐(โ). |
|||
|
|||
<img src="/img/deployment/https/https.svg"> |
|||
|
|||
**๐ค ๐** **๐ โฎ๏ธ ๐ ๐**, ๐ซ โฎ๏ธ ๐ข ๐ข. |
|||
|
|||
, โป ๐, ๐ ๐ ๐ช **๐ฆ** ๐ (โก๏ธ ๐) ๐ โซ๏ธ ๐ **"๐" & ๐ ๐ ๐**. |
|||
|
|||
๐, & ๐ ๐ ๐ธ ๐ช, ๐ค ๐ ๐ โซ๏ธ ๐ช โซ๏ธ. ๐ ๐: |
|||
|
|||
* **๐ ๐ โบ**. |
|||
* ๐, ๐ ๐ ๐ช ๐โ๐ฆบ ๐ ๐ ๐โ๐ฆบ,, โ๏ธ ๐ ๐ ๐โ๐ฆบ ๐ โ๏ธ, ๐ 5๏ธโฃ๐ โ๏ธ ๐ช ๐ซ ๐. |
|||
* **๐ ๐ฝ** (๐ โฎ๏ธ ๐ ๐ ๏ธ ๐ ๏ธ) ๐ ๐ข ๐ข ๐ข ๐ โฎ๏ธ ๐. |
|||
* ๐ฅ ๐ฌ ๐, ๐ด 1๏ธโฃ ๐ ๏ธ ๐ช ๐ ๐ ๐ฏ ๐ข & โด. |
|||
* ๐ 1๏ธโฃ ๐ค โซ๏ธโ โซ๏ธ ๐ถ โ ๐โ ๐ ๐ค โ ๐ณ โ ๐
๐ ๐ ๐ ๏ธ. |
|||
* โช, ๐ ๐ช โ๏ธ โ๏ธ ๐ค โ ๐ณ ๐, โถ๏ธ ๐ ๐ ๐ ๐, โคด๏ธ ๐ ๐ซ โฎ๏ธ ๐ค โ ๐ณ, & โคด๏ธ โ ๐ค โ ๐ณ. ๐ ๐ซ ๐ฏ, ๐ ๐ฑ(โ) ๐ ๐ซ ๐ช โฎ๏ธ ๐ฐ ๐ ๐ค โ ๐ณ ๐. |
|||
|
|||
๐ ๐ ๐ ๐ ๏ธ, โช ๐ฆ ๐ฑ, 1๏ธโฃ ๐ ๐ค โซ๏ธโ ๐ ๐ ๐ โ๏ธ **๐ โ๏ธ ๐ต ๐บ๐ธ๐** โฎ๏ธ ๐ค โ ๐ณ โฉ๏ธ โ๏ธ ๐ค ๐ โฎ๏ธ ๐ธ ๐ฝ ๐ (โ
Uvicorn). |
|||
|
|||
## ๐ |
|||
|
|||
โ๏ธ **๐บ๐ธ๐** ๐ถ โ , & **๐ฏ** ๐ ๐ผ. ๐
๐ฏ ๐ ๐ฉโ๐ป โ๏ธ ๐ฎ ๐คญ ๐บ๐ธ๐ ๐ **๐ค ๐ ๐ง** & โ ๐ซ ๐ท. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ญ ๐ฐ โน **๐บ๐ธ๐ ๐ฉโ๐ป** ๐ ๐ช ๐ช ๐ & ๐ ๐ ๐งฐ โน ๐ ๐ ๏ธ ๐ ๐
๐. |
|||
|
|||
โญ ๐, ๐ค ๐ ๐ฆ ๐ ๐ ๐งฑ ๐ผ โ โ ๐ **๐บ๐ธ๐** **FastAPI** ๐ธ. ๐ถ |
@ -0,0 +1,21 @@ |
|||
# ๐ ๏ธ |
|||
|
|||
๐ ๏ธ **FastAPI** ๐ธ ๐ถ โฉ. |
|||
|
|||
## โซ๏ธโ ๐จ ๐ ๏ธ โ |
|||
|
|||
**๐ ๏ธ** ๐ธ โ ๐ญ ๐ช ๐ถ โ โซ๏ธ **๐ช ๐ฉโ๐ป**. |
|||
|
|||
**๐ธ ๐ ๏ธ**, โซ๏ธ ๐ ๐ ๐ฎ โซ๏ธ **๐ฐ ๐ฐ**, โฎ๏ธ **๐ฝ ๐** ๐ ๐ ๐ ๐ญ, โ, โ๏ธ, ๐ ๐ **๐ฉโ๐ป** ๐ช **๐** ๐ธ โป & ๐ต ๐ โ๏ธ โ . |
|||
|
|||
๐ ๐
**๐ ๏ธ** โถ๏ธ, ๐โ ๐ ๐ง ๐ ๐, ๐ โซ๏ธ & โป โซ๏ธ, โ๏ธ & ๐ ๐ ๏ธ ๐ฝ, โ๏ธ. |
|||
|
|||
## ๐ ๏ธ ๐ |
|||
|
|||
๐ค ๐ ๐ โซ๏ธ โ๏ธ ๐ ๐ ๐ฏ โ๏ธ ๐ผ & ๐งฐ ๐ ๐ โ๏ธ. |
|||
|
|||
๐ ๐ช **๐ ๏ธ ๐ฝ** ๐ โ๏ธ ๐ ๐งฐ, ๐ ๐ช โ๏ธ **โ ๐โ๐ฆบ** ๐ ๐จ ๐ ๐ท ๐, โ๏ธ ๐ ๐ช ๐. |
|||
|
|||
๐ค ๐ ๐ฆ ๐ ๐ ๐ง ๐ ๐ ๐ฒ โ๏ธ ๐คฏ ๐โ ๐ ๏ธ **FastAPI** ๐ธ (๐ ๐
โซ๏ธ โ ๐ ๐ ๐ ๐ธ ๐ธ). |
|||
|
|||
๐ ๐ ๐ ๐ โน โ๏ธ ๐คฏ & โ โซ๏ธ โญ ๐. ๐ถ |
@ -0,0 +1,145 @@ |
|||
# ๐ ๐ฝ โ - Uvicorn |
|||
|
|||
๐ ๐ ๐ ๐ช ๐ **FastAPI** ๐ธ ๐ฐ ๐ฝ ๐ฐ ๐ซ ๐ฝ ๐ ๐ **Uvicorn**. |
|||
|
|||
๐ค 3๏ธโฃ ๐ ๐: |
|||
|
|||
* <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a>: โ ๐ญ ๐ซ ๐ฝ. |
|||
* <a href="https://pgjones.gitlab.io/hypercorn/" class="external-link" target="_blank">Hypercorn</a>: ๐ซ ๐ฝ ๐ โฎ๏ธ ๐บ๐ธ๐/2๏ธโฃ & ๐ป ๐ช ๐ โ. |
|||
* <a href="https://github.com/django/daphne" class="external-link" target="_blank">๐ธ</a>: ๐ซ ๐ฝ ๐ โณ ๐ป. |
|||
|
|||
## ๐ฝ ๐ฐ & ๐ฝ ๐ |
|||
|
|||
๐ค ๐คช โน ๐ ๐ โ๏ธ ๐คฏ. ๐ถ |
|||
|
|||
๐ค "**๐ฝ**" ๐ โ๏ธ ๐ ๐ฏโโ๏ธ ๐ฐ/โ ๐ป (โ โ๏ธ ๐น ๐ฐ) & ๐ ๐ ๐โโ ๐ ๐ ๐ฐ (โ
Uvicorn). |
|||
|
|||
โ๏ธ ๐ ๐คฏ ๐โ ๐ โ "๐ฝ" ๐ข, โซ๏ธ ๐ช ๐ 1๏ธโฃ ๐ 2๏ธโฃ ๐. |
|||
|
|||
๐โ ๐ ๐ฐ ๐ฐ, โซ๏ธ โ ๐ค โซ๏ธ **๐ฝ**, โ๏ธ **๐ฐ**, **๐พ** (๐น ๐ฐ), **๐ธ**. ๐ ๐ ๐ ๐ ๐ฐ ๐ฐ, ๐ ๐โโ ๐พ, ๐โ ๐ ๐ ๐. |
|||
|
|||
## โ ๐ฝ ๐ |
|||
|
|||
๐ ๐ช โ ๐ซ ๐ ๐ฝ โฎ๏ธ: |
|||
|
|||
=== "Uvicorn" |
|||
|
|||
* <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a>, ๐ฉ-โฉ ๐ซ ๐ฝ, ๐ ๐ uvloop & httptool. |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install "uvicorn[standard]" |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
!!! tip |
|||
โ `standard`, Uvicorn ๐ โ & โ๏ธ ๐ โ ๐. |
|||
|
|||
๐ โ
`uvloop`, โ-๐ญ ๐ง-โป `asyncio`, ๐ ๐ ๐ฆ ๐ ๏ธ ๐ญ ๐. |
|||
|
|||
=== "Hypercorn" |
|||
|
|||
* <a href="https://gitlab.com/pgjones/hypercorn" class="external-link" target="_blank">Hypercorn</a>, ๐ซ ๐ฝ ๐ โฎ๏ธ ๐บ๐ธ๐/2๏ธโฃ. |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install hypercorn |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
...โ๏ธ ๐ ๐ ๐ซ ๐ฝ. |
|||
|
|||
## ๐ ๐ฝ ๐ |
|||
|
|||
๐ ๐ช โคด๏ธ ๐ ๐ ๐ธ ๐ ๐ ๐ โ๏ธ โ ๐ฐ, โ๏ธ ๐ต `--reload` ๐, โ
: |
|||
|
|||
=== "Uvicorn" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --host 0.0.0.0 --port 80 |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
=== "Hypercorn" |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ hypercorn main:app --bind 0.0.0.0:80 |
|||
|
|||
Running on 0.0.0.0:8080 over http (CTRL + C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
!!! warning |
|||
๐ญ โ `--reload` ๐ ๐ฅ ๐ โ๏ธ โซ๏ธ. |
|||
|
|||
`--reload` ๐ ๐ด ๐
๐
โน, ๐
โ , โ๏ธ. |
|||
|
|||
โซ๏ธ โน ๐ โฎ๏ธ **๐ ๏ธ**, โ๏ธ ๐ **๐ซ๐ ๐ซ** โ๏ธ โซ๏ธ **๐ญ**. |
|||
|
|||
## Hypercorn โฎ๏ธ ๐ป |
|||
|
|||
๐ & **FastAPI** โ๏ธ ๐ <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a>, โ โ ๐ซ ๐ โฎ๏ธ ๐ฏโโ๏ธ ๐ ๐ฉ ๐ <a href="https://docs.python.org/3/library/asyncio-task.html" class="external-link" target="_blank">โณ</a> & <a href="https://trio.readthedocs.io/en/stable/" class="external-link" target="_blank">๐ป</a>. |
|||
|
|||
๐, Uvicorn โณ ๐ด ๐ โฎ๏ธ โณ, & โซ๏ธ ๐ โ๏ธ <a href="https://github.com/MagicStack/uvloop" class="external-link" target="_blank">`uvloop`</a>, โ-๐ญ ๐ง-โป `asyncio`. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ ๐ โ๏ธ **๐ป**, โคด๏ธ ๐ ๐ช โ๏ธ **Hypercorn** โซ๏ธ ๐โ๐ฆบ โซ๏ธ. ๐ถ |
|||
|
|||
### โ Hypercorn โฎ๏ธ ๐ป |
|||
|
|||
๐ฅ ๐ ๐ช โ Hypercorn โฎ๏ธ ๐ป ๐โ๐ฆบ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install "hypercorn[trio]" |
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
### ๐ โฎ๏ธ ๐ป |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ถโโ๏ธ ๐ โธ ๐ `--worker-class` โฎ๏ธ ๐ฒ `trio`: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ hypercorn main:app --worker-class trio |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
& ๐ ๐ โถ๏ธ Hypercorn โฎ๏ธ ๐ ๐ฑ โ๏ธ ๐ป ๐ฉโ๐ป. |
|||
|
|||
๐ ๐ ๐ช โ๏ธ ๐ป ๐ ๐ ๐ฑ. โ๏ธ ๐, ๐ ๐ช โ๏ธ AnyIO, ๐ง ๐ ๐ ๐ โฎ๏ธ ๐ฏโโ๏ธ ๐ป & โณ. ๐ถ |
|||
|
|||
## ๐ ๏ธ ๐ง |
|||
|
|||
๐ซ ๐ผ ๐ ๐ฝ ๐ (๐ง.โ Uvicorn), โถ๏ธ **๐ ๐ ๏ธ**, ๐ ๐ ๐ ๐ข (`0.0.0.0`) ๐ ๐ โด (โ
`80`). |
|||
|
|||
๐ ๐ฐ ๐ญ. โ๏ธ ๐ ๐ ๐ฒ ๐ โ ๐
๐ ๐, ๐: |
|||
|
|||
* ๐โโ - ๐บ๐ธ๐ |
|||
* ๐โโ ๐ ๐ด |
|||
* โ |
|||
* ๐งฌ (๐ข ๐ ๏ธ ๐) |
|||
* ๐พ |
|||
* โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
๐ค ๐ ๐ฌ ๐ ๐
๐ ๐ ๐ซ ๐ง, โ ๐ญ ๐ ๐ซ, & ๐งฑ ๐ผ โฎ๏ธ ๐ ๐ต ๐ซ โญ ๐. ๐ถ |
@ -0,0 +1,178 @@ |
|||
# ๐ฝ ๐จโ๐ญ - ๐ โฎ๏ธ Uvicorn |
|||
|
|||
โก๏ธ โ
๐ ๐ ๐ ๏ธ ๐ง โช๏ธโก๏ธ โญ: |
|||
|
|||
* ๐โโ - ๐บ๐ธ๐ |
|||
* ๐โโ ๐ ๐ด |
|||
* โ |
|||
* **๐งฌ (๐ข ๐ ๏ธ ๐)** |
|||
* ๐พ |
|||
* โฎ๏ธ ๐ โญ โถ๏ธ |
|||
|
|||
๐ ๐ โ, โฎ๏ธ ๐ ๐ฐ ๐ฉบ, ๐ โ๏ธ ๐ฒ ๐โโ **๐ฝ ๐** ๐ Uvicorn, ๐โโ **๐ ๐ ๏ธ**. |
|||
|
|||
๐โ ๐ ๏ธ ๐ธ ๐ ๐ ๐ฒ ๐ โ๏ธ **๐งฌ ๐ ๏ธ** โ ๐ **๐ ๐** & ๐ช ๐ต ๐
๐จ. |
|||
|
|||
๐ ๐ โฎ๏ธ ๐ ๐ [๐ ๏ธ ๐ง](./concepts.md){.internal-link target=_blank}, ๐ค ๐ ๐ ๐ ๐ช โ๏ธ. |
|||
|
|||
๐ฅ ๐ค ๐ ๐ฆ ๐ โ โ๏ธ <a href="https://gunicorn.org/" class="external-link" target="_blank">**๐**</a> โฎ๏ธ **Uvicorn ๐จโ๐ญ ๐ ๏ธ**. |
|||
|
|||
!!! info |
|||
๐ฅ ๐ โ๏ธ ๐ฆ, ๐ผ โฎ๏ธ โ โ๏ธ Kubernete, ๐ค ๐ ๐ฌ ๐ ๐
๐ ๐ โญ ๐: [FastAPI ๐ฆ - โ](./docker.md){.internal-link target=_blank}. |
|||
|
|||
๐ฏ, ๐โ ๐ ๐ **Kubernete** ๐ ๐ ๐ฒ **๐ซ** ๐ โ๏ธ ๐ & โฉ๏ธ ๐ **๐ Uvicorn ๐ ๏ธ ๐ ๐ฆ**, โ๏ธ ๐ค ๐ ๐ฌ ๐ ๐ โซ๏ธ โช ๐ ๐. |
|||
|
|||
## ๐ โฎ๏ธ Uvicorn ๐จโ๐ญ |
|||
|
|||
**๐** โด๏ธ ๐ธ ๐ฝ โ๏ธ **๐จ๐ป ๐ฉ**. ๐ โ ๐ ๐ ๐ช ๐ฆ ๐ธ ๐ ๐บ & โณ. ๐ โซ๏ธ ๐ซ ๐ โฎ๏ธ **FastAPI**, FastAPI โ๏ธ ๐ **<a href="https://asgi.readthedocs.io/en/latest/" class="external-link" target="_blank">๐ซ ๐ฉ</a>**. |
|||
|
|||
โ๏ธ ๐ ๐โ๐ฆบ ๐ท **๐ ๏ธ ๐จโ๐ผ** & ๐ค ๐ฉโ๐ป ๐ฌ โซ๏ธ โ ๐ฏ **๐จโ๐ญ ๐ ๏ธ ๐** โ๏ธ. โคด๏ธ ๐ ๐ โถ๏ธ 1๏ธโฃ โ๏ธ ๐ **๐จโ๐ญ ๐ ๏ธ** โ๏ธ ๐ ๐. |
|||
|
|||
& **Uvicorn** โ๏ธ **๐-๐ ๐จโ๐ญ ๐**. |
|||
|
|||
โ๏ธ ๐ ๐, ๐ ๐ ๐ซ **๐ ๏ธ ๐จโ๐ผ**, ๐ ๐ **โด** & **๐ข**. & โซ๏ธ ๐ **๐ถ** ๐ป ๐จโ๐ญ ๐ ๏ธ ๐ **Uvicorn ๐**. |
|||
|
|||
& โคด๏ธ ๐-๐ **Uvicorn ๐จโ๐ญ** ๐ ๐ ๐ ๐ญ ๐ ๐จ ๐ ๐ซ ๐ฉ FastAPI โ๏ธ โซ๏ธ. |
|||
|
|||
## โ ๐ & Uvicorn |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install "uvicorn[standard]" gunicorn |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ โ ๐ฏโโ๏ธ Uvicorn โฎ๏ธ `standard` โ ๐ฆ (๐ค โ ๐ญ) & ๐. |
|||
|
|||
## ๐ ๐ โฎ๏ธ Uvicorn ๐จโ๐ญ |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ ๐ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:80 |
|||
|
|||
[19499] [INFO] Starting gunicorn 20.1.0 |
|||
[19499] [INFO] Listening at: http://0.0.0.0:80 (19499) |
|||
[19499] [INFO] Using worker: uvicorn.workers.UvicornWorker |
|||
[19511] [INFO] Booting worker with pid: 19511 |
|||
[19513] [INFO] Booting worker with pid: 19513 |
|||
[19514] [INFO] Booting worker with pid: 19514 |
|||
[19515] [INFO] Booting worker with pid: 19515 |
|||
[19511] [INFO] Started server process [19511] |
|||
[19511] [INFO] Waiting for application startup. |
|||
[19511] [INFO] Application startup complete. |
|||
[19513] [INFO] Started server process [19513] |
|||
[19513] [INFO] Waiting for application startup. |
|||
[19513] [INFO] Application startup complete. |
|||
[19514] [INFO] Started server process [19514] |
|||
[19514] [INFO] Waiting for application startup. |
|||
[19514] [INFO] Application startup complete. |
|||
[19515] [INFO] Started server process [19515] |
|||
[19515] [INFO] Waiting for application startup. |
|||
[19515] [INFO] Application startup complete. |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โก๏ธ ๐ โซ๏ธโ ๐ ๐ ๐ โ: |
|||
|
|||
* `main:app`: ๐ ๐ โ โ๏ธ Uvicorn, `main` โ ๐ ๐น ๐ "`main`",, ๐ `main.py`. & `app` ๐ ๐ข ๐ **FastAPI** ๐ธ. |
|||
* ๐ ๐ช ๐ ๐ `main:app` ๐ ๐ `import` ๐ ๐: |
|||
|
|||
```Python |
|||
from main import app |
|||
``` |
|||
|
|||
* , โค `main:app` ๐ ๐ ๐ `import` ๐ `from main import app`. |
|||
* `--workers`: ๐ข ๐จโ๐ญ ๐ ๏ธ โ๏ธ, ๐ ๐ ๐ Uvicorn ๐จโ๐ญ, ๐ ๐ผ, 4๏ธโฃ ๐จโ๐ญ. |
|||
* `--worker-class`: ๐-๐ ๐จโ๐ญ ๐ โ๏ธ ๐จโ๐ญ ๐ ๏ธ. |
|||
* ๐ฅ ๐ฅ ๐ถโโ๏ธ ๐ ๐ ๐ ๐ช ๐ & โ๏ธ โฎ๏ธ: |
|||
|
|||
```Python |
|||
import uvicorn.workers.UvicornWorker |
|||
``` |
|||
|
|||
* `--bind`: ๐ ๐ฌ ๐ ๐ข & โด ๐, โ๏ธ โค (`:`) ๐ ๐ข & โด. |
|||
* ๐ฅ ๐ ๐โโ Uvicorn ๐, โฉ๏ธ `--bind 0.0.0.0:80` (๐ ๐) ๐ ๐ โ๏ธ `--host 0.0.0.0` & `--port 80`. |
|||
|
|||
๐ข, ๐ ๐ช ๐ ๐ โซ๏ธ ๐ฆ **๐น** (๐ ๏ธ ๐) ๐ ๐ ๏ธ (โซ๏ธ ๐ข). |
|||
|
|||
๐ ๐ช ๐ ๐: |
|||
|
|||
* ๐ **๐ ๏ธ ๐จโ๐ผ** โถ๏ธ โฎ๏ธ ๐น `19499` (๐ ๐ผ โซ๏ธ ๐ ๐ ๐ข). |
|||
* โคด๏ธ โซ๏ธ โถ๏ธ `Listening at: http://0.0.0.0:80`. |
|||
* โคด๏ธ โซ๏ธ ๐ ๐ โซ๏ธ โ๏ธ โ๏ธ ๐จโ๐ญ ๐ `uvicorn.workers.UvicornWorker`. |
|||
* & โคด๏ธ โซ๏ธ โถ๏ธ **4๏ธโฃ ๐จโ๐ญ**, ๐ โฎ๏ธ ๐ฎ ๐ ๐น: `19511`, `19513`, `19514`, & `19515`. |
|||
|
|||
๐ ๐ โ ๐
๐ ๏ธ **โ ๏ธ ๐ ๏ธ** & **๐** ๐ ๐ ๐ฅ ๐ ๐ง ๐ข ๐จโ๐ญ. ๐ โน ๐ โฎ๏ธ **โ** ๐ง โช๏ธโก๏ธ ๐ ๐. |
|||
|
|||
๐, ๐ ๐ ๐ฒ ๐ โ๏ธ ๐ณ ๐ โ ๐ญ **โ ๐** ๐ฅ ๐ช, & **๐ โซ๏ธ ๐ ๐ด**, โ๏ธ. |
|||
|
|||
## Uvicorn โฎ๏ธ ๐จโ๐ญ |
|||
|
|||
Uvicorn โ๏ธ ๐ โถ๏ธ & ๐ ๐ **๐จโ๐ญ ๐ ๏ธ**. |
|||
|
|||
๐, ๐, Uvicorn ๐ ๏ธ ๐ ๐จโ๐ญ ๐ ๏ธ ๐
๐ ๐ ๐. , ๐ฅ ๐ ๐ โ๏ธ ๐ ๏ธ ๐จโ๐ผ ๐ ๐ (๐ ๐), โคด๏ธ โซ๏ธ ๐ช ๐ ๐ โฎ๏ธ ๐ ๐ ๏ธ ๐จโ๐ผ. |
|||
|
|||
๐ ๐ผ, ๐ ๐ ๐ โซ๏ธ ๐ ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --host 0.0.0.0 --port 8080 --workers 4 |
|||
<font color="#A6E22E">INFO</font>: Uvicorn running on <b>http://0.0.0.0:8080</b> (Press CTRL+C to quit) |
|||
<font color="#A6E22E">INFO</font>: Started parent process [<font color="#A1EFE4"><b>27365</b></font>] |
|||
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27368</font>] |
|||
<font color="#A6E22E">INFO</font>: Waiting for application startup. |
|||
<font color="#A6E22E">INFO</font>: Application startup complete. |
|||
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27369</font>] |
|||
<font color="#A6E22E">INFO</font>: Waiting for application startup. |
|||
<font color="#A6E22E">INFO</font>: Application startup complete. |
|||
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27370</font>] |
|||
<font color="#A6E22E">INFO</font>: Waiting for application startup. |
|||
<font color="#A6E22E">INFO</font>: Application startup complete. |
|||
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27367</font>] |
|||
<font color="#A6E22E">INFO</font>: Waiting for application startup. |
|||
<font color="#A6E22E">INFO</font>: Application startup complete. |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ด ๐ ๐ ๐ฅ `--workers` ๐ฌ Uvicorn โถ๏ธ 4๏ธโฃ ๐จโ๐ญ ๐ ๏ธ. |
|||
|
|||
๐ ๐ช ๐ ๐ โซ๏ธ ๐ฆ **๐น** ๐ ๐ ๏ธ, `27365` ๐ช ๐ ๏ธ (๐ **๐ ๏ธ ๐จโ๐ผ**) & 1๏ธโฃ ๐ ๐จโ๐ญ ๐ ๏ธ: `27368`, `27369`, `27370`, & `27367`. |
|||
|
|||
## ๐ ๏ธ ๐ง |
|||
|
|||
๐ฅ ๐ ๐ โ โ๏ธ **๐** (โ๏ธ Uvicorn) ๐ ๏ธ **Uvicorn ๐จโ๐ญ ๐ ๏ธ** **๐** ๐ ๏ธ ๐ธ, โ ๐ **๐ ๐** ๐ฝ, & ๐ช ๐ฆ **๐
๐จ**. |
|||
|
|||
โช๏ธโก๏ธ ๐ ๐ ๏ธ ๐ง โช๏ธโก๏ธ ๐, โ๏ธ ๐จโ๐ญ ๐ โด๏ธ โน โฎ๏ธ **๐งฌ** ๐, & ๐ฅ ๐ โฎ๏ธ **โ**, โ๏ธ ๐ ๐ช โ ๐
๐: |
|||
|
|||
* **๐โโ - ๐บ๐ธ๐** |
|||
* **๐โโ ๐ ๐ด** |
|||
* ***โ*** |
|||
* ๐งฌ (๐ข ๐ ๏ธ ๐) |
|||
* **๐พ** |
|||
* **โฎ๏ธ ๐ โญ โถ๏ธ** |
|||
|
|||
## ๐ฆ & โ |
|||
|
|||
โญ ๐ ๐ [FastAPI ๐ฆ - โ](./docker.md){.internal-link target=_blank} ๐ค ๐ ๐ฌ ๐ ๐ ๐ช โ๏ธ ๐ต ๐ **๐ ๏ธ ๐ง**. |
|||
|
|||
๐ค ๐ ๐ฆ ๐ **๐ โ ๐ผ** ๐ ๐ **๐ โฎ๏ธ Uvicorn ๐จโ๐ญ** & ๐ข ๐ณ ๐ ๐ช โ ๐
๐ผ. |
|||
|
|||
๐ค ๐ค ๐ ๐ฆ ๐ โ **๐ ๐ ๐ ๐ผ โช๏ธโก๏ธ ๐** ๐ ๐ Uvicorn ๐ ๏ธ (๐ต ๐). โซ๏ธ ๐
๐ ๏ธ & ๐ฒ โซ๏ธโ ๐ ๐ ๐ ๐โ โ๏ธ ๐ ๐ฆ ๐งพ โ๏ธ ๐ **Kubernete**. |
|||
|
|||
## ๐ |
|||
|
|||
๐ ๐ช โ๏ธ **๐** (โ๏ธ Uvicorn) ๐ ๏ธ ๐จโ๐ผ โฎ๏ธ Uvicorn ๐จโ๐ญ โ ๐ **๐-๐ ๐ฝ**, ๐ **๐ ๐ ๏ธ ๐**. |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐งฐ & ๐ญ ๐ฅ ๐ โ ๐ **๐ ๐ ๐ ๏ธ โ๏ธ** โช โ ๐
๐ ๐ ๏ธ ๐ง ๐. |
|||
|
|||
โ
๐
โญ ๐ ๐ก ๐ **FastAPI** โฎ๏ธ ๐ฆ (โ
โ & Kubernete). ๐ ๐ ๐ ๐ ๐ ๐งฐ โ๏ธ ๐
๐ โ ๐ **๐ ๏ธ ๐ง** ๐. ๐ถ |
@ -0,0 +1,87 @@ |
|||
# ๐ FastAPI โฌ |
|||
|
|||
**FastAPI** โช โ โ๏ธ ๐ญ ๐ ๐ธ & โ๏ธ. & ๐ฏ ๐ฐ ๐ง 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ. โ๏ธ ๐ฎ ๐ ๏ธ ๐ ๐. |
|||
|
|||
๐ โ ๐ฎ ๐, ๐ ๐ง ๐, & ๐ ๐ ๐. |
|||
|
|||
๐ โซ๏ธโ โฎ๏ธ โฌ `0.x.x`, ๐ ๐จ ๐ ๐ โฌ ๐ช โ โ๏ธ ๐ ๐. ๐ โฉ <a href="https://semver.org/" class="external-link" target="_blank">โ ๐ ๏ธ</a> ๐. |
|||
|
|||
๐ ๐ช โ ๐ญ ๐ธ โฎ๏ธ **FastAPI** โถ๏ธ๏ธ ๐ (& ๐ โ๏ธ ๐ฒ ๐จ โซ๏ธ ๐ฐ), ๐ โ๏ธ โ ๐ญ ๐ ๐ โ๏ธ โฌ ๐ ๐ท โ โฎ๏ธ ๐ ๐ ๐. |
|||
|
|||
## ๐ ๐ `fastapi` โฌ |
|||
|
|||
๐ฅ ๐ ๐ ๐ "๐" โฌ **FastAPI** ๐ โ๏ธ ๐ฏ ๐ฐ โฌ ๐ ๐ ๐ญ ๐ท โ ๐ ๐ธ. |
|||
|
|||
๐ผ, โก๏ธ ๐ฌ ๐ โ๏ธ โฌ `0.45.0` ๐ ๐ฑ. |
|||
|
|||
๐ฅ ๐ โ๏ธ `requirements.txt` ๐ ๐ ๐ช โ โฌ โฎ๏ธ: |
|||
|
|||
```txt |
|||
fastapi==0.45.0 |
|||
``` |
|||
|
|||
๐ ๐ โ ๐ ๐ ๐ โ๏ธ โซ๏ธโ โฌ `0.45.0`. |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ โซ๏ธ โฎ๏ธ: |
|||
|
|||
```txt |
|||
fastapi>=0.45.0,<0.46.0 |
|||
``` |
|||
|
|||
๐ ๐ โ ๐ ๐ ๐ โ๏ธ โฌ `0.45.0` โ๏ธ ๐, โ๏ธ ๐ ๐ `0.46.0`, ๐ผ, โฌ `0.45.2` ๐ ๐ซ. |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ ๐ ๐งฐ ๐ ๏ธ ๐ ๐ทโโ, ๐ ๐ถ, Pipenv, โ๏ธ ๐, ๐ซ ๐ โ๏ธ ๐ ๐ ๐ ๐ช โ๏ธ ๐ฌ ๐ฏ โฌ ๐ ๐ฆ. |
|||
|
|||
## ๐ช โฌ |
|||
|
|||
๐ ๐ช ๐ ๐ช โฌ (โ
โ
โซ๏ธโ โฎ๏ธ ๐ฐ) [๐ ๐](../release-notes.md){.internal-link target=_blank}. |
|||
|
|||
## ๐ โฌ |
|||
|
|||
๐ โ ๐ ๏ธ ๐, ๐ โฌ ๐ `1.0.0` ๐ช โ ๐ฎ ๐ ๐. |
|||
|
|||
FastAPI โฉ ๐ ๐ ๐ "๐" โฌ ๐ ๐ ๐ง & ๐ซ-๐ ๐. |
|||
|
|||
!!! tip |
|||
"๐" ๐ ๐ข, ๐ผ, `0.2.3`, ๐ โฌ `3`. |
|||
|
|||
, ๐ ๐ ๐ช ๐ โฌ ๐: |
|||
|
|||
```txt |
|||
fastapi>=0.45.0,<0.46.0 |
|||
``` |
|||
|
|||
๐ ๐ & ๐ โ ๐ฎ "๐บ๐ฒ" โฌ. |
|||
|
|||
!!! tip |
|||
"๐บ๐ฒ" ๐ข ๐, ๐ผ, `0.2.3`, ๐บ๐ฒ โฌ `2`. |
|||
|
|||
## โป FastAPI โฌ |
|||
|
|||
๐ ๐ ๐ฎ ๐ฏ ๐ ๐ฑ. |
|||
|
|||
โฎ๏ธ **FastAPI** โซ๏ธ ๐ถ โฉ (๐ ๐), โ
๐ฉบ: [๐ฌ](../tutorial/testing.md){.internal-link target=_blank} |
|||
|
|||
โฎ๏ธ ๐ โ๏ธ ๐ฏ, โคด๏ธ ๐ ๐ช โป **FastAPI** โฌ ๐ โฎ๏ธ 1๏ธโฃ, & โ ๐ญ ๐ ๐ ๐ ๐ ๐ท โ ๐ ๐ ๐ฏ. |
|||
|
|||
๐ฅ ๐ ๐ท, โ๏ธ โฎ๏ธ ๐ โ ๐ช ๐, & ๐ ๐ ๐ฏ ๐ถโโ๏ธ, โคด๏ธ ๐ ๐ช ๐ ๐ `fastapi` ๐ ๐ โฎ๏ธ โฌ. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ ๐ซ๐ ๐ซ ๐ โฌ `starlette`. |
|||
|
|||
๐ โฌ **FastAPI** ๐ โ๏ธ ๐ฏ ๐ โฌ ๐. |
|||
|
|||
, ๐ ๐ช โก๏ธ **FastAPI** โ๏ธ โ ๐ โฌ. |
|||
|
|||
## ๐ Pydantic |
|||
|
|||
Pydantic ๐ ๐ฏ **FastAPI** โฎ๏ธ ๐ฎ ๐ ๐ฏ, ๐ โฌ Pydantic (๐ `1.0.0`) ๐ง ๐ โฎ๏ธ FastAPI. |
|||
|
|||
๐ ๐ช ๐ Pydantic ๐ โฌ ๐ `1.0.0` ๐ ๐ท ๐ & ๐ `2.0.0`. |
|||
|
|||
๐ผ: |
|||
|
|||
```txt |
|||
pydantic>=1.2.0,<2.0.0 |
|||
``` |
@ -0,0 +1,91 @@ |
|||
# ๐ข ๐ & ๐ |
|||
|
|||
**FastAPI** โ๏ธ ๐ ๐ช ๐ง ๐. |
|||
|
|||
๐ค ๐ ๐ค, ๐, ๐งฐ, & ๐, ๐ **FastAPI**. |
|||
|
|||
๐ฅ โ ๐ ๐ซ. |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ โ๏ธ ๐, ๐, ๐งฐ, โ๏ธ ๐ณ ๐ **FastAPI** ๐ ๐ซ ๐ ๐ฅ, โ <a href="https://github.com/tiangolo/fastapi/edit/master/docs/en/data/external_links.yml" class="external-link" target="_blank">๐ฒ ๐จ โ โซ๏ธ</a>. |
|||
|
|||
## ๐ |
|||
|
|||
### ๐ช๐ธ |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.articles.english %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
### ๐ฏ๐ต |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.articles.japanese %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
### ๐ป๐ณ |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.articles.vietnamese %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
### ๐ท๐บ |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.articles.russian %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
### ๐ฉ๐ช |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.articles.german %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
### ๐น๐ผ |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.articles.taiwanese %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
## ๐ป |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.podcasts.english %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
## ๐ฌ |
|||
|
|||
{% if external_links %} |
|||
{% for article in external_links.talks.english %} |
|||
|
|||
* <a href="{{ article.link }}" class="external-link" target="_blank">{{ article.title }}</a> <a href="{{ article.author_link }}" class="external-link" target="_blank">{{ article.author }}</a>. |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
## ๐ |
|||
|
|||
โช ๐ ๐ โฎ๏ธ โ `fastapi`: |
|||
|
|||
<div class="github-topic-projects"> |
|||
</div> |
@ -0,0 +1,178 @@ |
|||
# FastAPI ๐ซ๐ซ |
|||
|
|||
FastAPI โ๏ธ ๐ ๐ช ๐ ๐ ๐ซ๐ซ โช๏ธโก๏ธ ๐ ๐ฅ. |
|||
|
|||
## ๐ผ - ๐ |
|||
|
|||
๐ โ ๐ถ |
|||
|
|||
๐ ๐ค: |
|||
|
|||
{% if people %} |
|||
<div class="user-list user-list-center"> |
|||
{% for user in people.maintainers %} |
|||
|
|||
<div class="user"><a href="{{ user.url }}" target="_blank"><div class="avatar-wrapper"><img src="{{ user.avatarUrl }}"/></div><div class="title">@{{ user.login }}</div></a> <div class="count">โ: {{ user.answers }}</div><div class="count">๐ฒ ๐จ: {{ user.prs }}</div></div> |
|||
{% endfor %} |
|||
|
|||
</div> |
|||
{% endif %} |
|||
|
|||
๐ค ๐ผ & ๐ **FastAPI**. ๐ ๐ช โ ๐
๐ ๐ [โน FastAPI - ๐ค โน - ๐ โฎ๏ธ ๐](help-fastapi.md#connect-with-the-author){.internal-link target=_blank}. |
|||
|
|||
...โ๏ธ ๐ฅ ๐ค ๐ ๐ฆ ๐ ๐ช. |
|||
|
|||
--- |
|||
|
|||
**FastAPI** ๐จ ๐ ๐โ๐ฆบ โช๏ธโก๏ธ ๐ช. & ๐ค ๐ ๐ฆ ๐ซ ๐ฐ. |
|||
|
|||
๐ซ ๐ซ๐ซ ๐: |
|||
|
|||
* [โน ๐ โฎ๏ธ โ ๐](help-fastapi.md#help-others-with-questions-in-github){.internal-link target=_blank}. |
|||
* [โ ๐ฒ ๐จ](help-fastapi.md#create-a-pull-request){.internal-link target=_blank}. |
|||
* ๐ ๐ฒ ๐จ, [โด๏ธ โ โ](contributing.md#translations){.internal-link target=_blank}. |
|||
|
|||
๐ ๐ซ. ๐ถ ๐ถ |
|||
|
|||
## ๐
๐ฆ ๐ฉโ๐ป ๐ ๐๏ธ |
|||
|
|||
๐ซ ๐ฉโ๐ป ๐ โ๏ธ [๐ค ๐ ๐ โฎ๏ธ โ ๐](help-fastapi.md#help-others-with-questions-in-github){.internal-link target=_blank} โฎ๏ธ ๐ ๐๏ธ. ๐ถ |
|||
|
|||
{% if people %} |
|||
<div class="user-list user-list-center"> |
|||
{% for user in people.last_month_active %} |
|||
|
|||
<div class="user"><a href="{{ user.url }}" target="_blank"><div class="avatar-wrapper"><img src="{{ user.avatarUrl }}"/></div><div class="title">@{{ user.login }}</div></a> <div class="count">โ ๐จ: {{ user.count }}</div></div> |
|||
{% endfor %} |
|||
|
|||
</div> |
|||
{% endif %} |
|||
|
|||
## ๐ด |
|||
|
|||
๐ฅ **FastAPI ๐ด**. ๐ถ |
|||
|
|||
๐ซ ๐ฉโ๐ป ๐ โ๏ธ [โน ๐ ๐ โฎ๏ธ โ ๐](help-fastapi.md#help-others-with-questions-in-github){.internal-link target=_blank} ๐ *๐ ๐ฐ*. |
|||
|
|||
๐ซ โ๏ธ ๐ฆ ๐ด ๐ค ๐ ๐. ๐ถ |
|||
|
|||
{% if people %} |
|||
<div class="user-list user-list-center"> |
|||
{% for user in people.experts %} |
|||
|
|||
<div class="user"><a href="{{ user.url }}" target="_blank"><div class="avatar-wrapper"><img src="{{ user.avatarUrl }}"/></div><div class="title">@{{ user.login }}</div></a> <div class="count">โ ๐จ: {{ user.count }}</div></div> |
|||
{% endfor %} |
|||
|
|||
</div> |
|||
{% endif %} |
|||
|
|||
## ๐ ๐จโ๐ฌ |
|||
|
|||
๐ฅ **๐ ๐จโ๐ฌ**. ๐ถ |
|||
|
|||
๐ ๐ฉโ๐ป โ๏ธ [โ ๐ ๐ฒ ๐จ](help-fastapi.md#create-a-pull-request){.internal-link target=_blank} ๐ โ๏ธ *๐*. |
|||
|
|||
๐ซ โ๏ธ ๐ โน ๐, ๐งพ, โ, โ๏ธ. ๐ถ |
|||
|
|||
{% if people %} |
|||
<div class="user-list user-list-center"> |
|||
{% for user in people.top_contributors %} |
|||
|
|||
<div class="user"><a href="{{ user.url }}" target="_blank"><div class="avatar-wrapper"><img src="{{ user.avatarUrl }}"/></div><div class="title">@{{ user.login }}</div></a> <div class="count">๐ฒ ๐จ: {{ user.count }}</div></div> |
|||
{% endfor %} |
|||
|
|||
</div> |
|||
{% endif %} |
|||
|
|||
๐ค ๐ ๐ ๐จโ๐ฌ (๐
๐ ๐ฏ), ๐ ๐ช ๐ ๐ซ ๐ <a href="https://github.com/tiangolo/fastapi/graphs/contributors" class="external-link" target="_blank">FastAPI ๐ ๐จโ๐ฌ ๐</a>. ๐ถ |
|||
|
|||
## ๐ ๐จโ๐ฌ |
|||
|
|||
๐ซ ๐ฉโ๐ป **๐ ๐จโ๐ฌ**. ๐ถ ๐ถ |
|||
|
|||
### ๐ โ |
|||
|
|||
๐ค ๐ด ๐ฌ ๐ฉโโคโ๐จ ๐ช๐ธ (& ๐ซ ๐ถ ๐ ๐ถ). , ๐จโ๐ฌ ๐ ๐ โ๏ธ [**๐๏ธ โ โ**](contributing.md#translations){.internal-link target=_blank} ๐งพ. ๐ต ๐ซ, ๐ค ๐ซ๐ ๐งพ ๐ ๐ ๐ช๐ธ. |
|||
|
|||
--- |
|||
|
|||
**๐ ๐จโ๐ฌ** ๐ถ ๐ถ โ๏ธ ๐ ๐ ๐ฒ ๐จ โช๏ธโก๏ธ ๐, ๐ ๐ ๐, ๐งพ, & โด๏ธ, **โ**. |
|||
|
|||
{% if people %} |
|||
<div class="user-list user-list-center"> |
|||
{% for user in people.top_reviewers %} |
|||
|
|||
<div class="user"><a href="{{ user.url }}" target="_blank"><div class="avatar-wrapper"><img src="{{ user.avatarUrl }}"/></div><div class="title">@{{ user.login }}</div></a> <div class="count">๐: {{ user.count }}</div></div> |
|||
{% endfor %} |
|||
|
|||
</div> |
|||
{% endif %} |
|||
|
|||
## ๐ฐ |
|||
|
|||
๐ซ **๐ฐ**. ๐ถ |
|||
|
|||
๐ซ ๐ ๐ ๐ท โฎ๏ธ **FastAPI** (& ๐), โด๏ธ ๐ <a href="https://github.com/sponsors/tiangolo" class="external-link" target="_blank">๐ ๐ฐ</a>. |
|||
|
|||
{% if sponsors %} |
|||
|
|||
{% if sponsors.gold %} |
|||
|
|||
### ๐ ๐ฐ |
|||
|
|||
{% for sponsor in sponsors.gold -%} |
|||
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
{% if sponsors.silver %} |
|||
|
|||
### ๐ฅ1st ๐ฐ |
|||
|
|||
{% for sponsor in sponsors.silver -%} |
|||
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
{% if sponsors.bronze %} |
|||
|
|||
### ๐ฅ2nd ๐ฐ |
|||
|
|||
{% for sponsor in sponsors.bronze -%} |
|||
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
{% endif %} |
|||
|
|||
### ๐ฏ ๐ฐ |
|||
|
|||
{% if github_sponsors %} |
|||
{% for group in github_sponsors.sponsors %} |
|||
|
|||
<div class="user-list user-list-center"> |
|||
|
|||
{% for user in group %} |
|||
{% if user.login not in sponsors_badge.logins %} |
|||
|
|||
<div class="user"><a href="{{ user.url }}" target="_blank"><div class="avatar-wrapper"><img src="{{ user.avatarUrl }}"/></div><div class="title">@{{ user.login }}</div></a></div> |
|||
|
|||
{% endif %} |
|||
{% endfor %} |
|||
|
|||
</div> |
|||
|
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
## ๐ ๐ - ๐ก โน |
|||
|
|||
๐ ๐ฏ ๐ ๐ ๐ฆ ๐ฏ ๐ช โน ๐. |
|||
|
|||
โด๏ธ โ
๐ฏ ๐ ๐ ๐ โญ, & ๐ ๐ผ ๐
๐ฉ, ๐ ๐ค ๐ โฎ๏ธ โ & โ ๐ฒ ๐จ โฎ๏ธ โ. |
|||
|
|||
๐ฝ โ ๐ ๐๏ธ, ๐ ๐ช โ <a href="https://github.com/tiangolo/fastapi/blob/master/.github/actions/people/app/main.py" class="external-link" target="_blank">โน ๐ ๐ฅ</a>. |
|||
|
|||
๐ฅ ๐ค ๐ฆ ๐ฐ โช๏ธโก๏ธ ๐ฐ. |
|||
|
|||
๐ค ๐ฆ โถ๏ธ๏ธ โน ๐, ๐, โก, โ๏ธ (๐ผ ๐คท). |
@ -0,0 +1,200 @@ |
|||
# โ |
|||
|
|||
## FastAPI โ |
|||
|
|||
**FastAPI** ๐ค ๐ ๐: |
|||
|
|||
### โ๏ธ ๐ ๐ ๐ฉ |
|||
|
|||
* <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>๐</strong></a> ๐ ๏ธ ๐, โ
๐ <abbr title="also known as: endpoints, routes">โก</abbr> <abbr title="also known as HTTP methods, as POST, GET, PUT, DELETE">๐ ๏ธ</abbr>, ๐ข, ๐ช ๐จ, ๐โโ, โ๏ธ. |
|||
* ๐ง ๐ ๐ท ๐งพ โฎ๏ธ <a href="https://json-schema.org/" class="external-link" target="_blank"><strong>๐ป ๐</strong></a> (๐ โซ๏ธ ๐งข ๐ ๐ป ๐). |
|||
* ๐ง ๐คญ ๐ซ ๐ฉ, โฎ๏ธ ๐ ๐ฌ. โฉ๏ธ ๐ ๐งฝ ๐ ๐. |
|||
* ๐ โ โ๏ธ ๐ง **๐ฉโ๐ป ๐ โก** ๐ ๐ช๐ธ. |
|||
|
|||
### ๐ง ๐ฉบ |
|||
|
|||
๐ ๐ ๏ธ ๐งพ & ๐ฌ ๐ธ ๐ฉโ๐ป ๐ข. ๐ ๏ธ โ๏ธ ๐ ๐, ๐ค ๐ ๐, 2๏ธโฃ ๐ ๐ข. |
|||
|
|||
* <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank"><strong>๐ฆ ๐</strong></a>, โฎ๏ธ ๐ ๐ฌ, ๐ค & ๐ฏ ๐ ๐ ๏ธ ๐ โช๏ธโก๏ธ ๐ฅ. |
|||
|
|||
 |
|||
|
|||
* ๐ ๐ ๏ธ ๐งพ โฎ๏ธ <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank"><strong>๐</strong></a>. |
|||
|
|||
 |
|||
|
|||
### ๐ ๐ |
|||
|
|||
โซ๏ธ ๐ โ๏ธ ๐ ๐ฉ **๐ 3๏ธโฃ.6๏ธโฃ ๐** ๐ (๐ Pydantic). ๐
โโ ๐ โ ๐ก. ๐ฉ ๐ ๐. |
|||
|
|||
๐ฅ ๐ ๐ช 2๏ธโฃ โฒ โ๏ธ โ โ๏ธ ๐ ๐ (๐ฅ ๐ ๐ซ โ๏ธ FastAPI), โ
๐ ๐ฐ: [๐ ๐](python-types.md){.internal-link target=_blank}. |
|||
|
|||
๐ โ ๐ฉ ๐ โฎ๏ธ ๐: |
|||
|
|||
```Python |
|||
from datetime import date |
|||
|
|||
from pydantic import BaseModel |
|||
|
|||
# Declare a variable as a str |
|||
# and get editor support inside the function |
|||
def main(user_id: str): |
|||
return user_id |
|||
|
|||
|
|||
# A Pydantic model |
|||
class User(BaseModel): |
|||
id: int |
|||
name: str |
|||
joined: date |
|||
``` |
|||
|
|||
๐ ๐ช โคด๏ธ โ๏ธ ๐: |
|||
|
|||
```Python |
|||
my_user: User = User(id=3, name="John Doe", joined="2018-07-19") |
|||
|
|||
second_user_data = { |
|||
"id": 4, |
|||
"name": "Mary", |
|||
"joined": "2018-11-30", |
|||
} |
|||
|
|||
my_second_user: User = User(**second_user_data) |
|||
``` |
|||
|
|||
!!! info |
|||
`**second_user_data` โ: |
|||
|
|||
๐ถโโ๏ธ ๐ & ๐ฒ `second_user_data` #๏ธโฃ ๐ ๐-๐ฒ โ, ๐: `User(id=4, name="Mary", joined="2018-11-30")` |
|||
|
|||
### ๐จโ๐จ ๐โ๐ฆบ |
|||
|
|||
๐ ๐ ๏ธ ๐ โฉ & ๐๏ธ โ๏ธ, ๐ ๐ซ ๐ฏ ๐ ๐ ๐จโ๐จ โญ โถ๏ธ ๐ ๏ธ, ๐ ๐ ๐ ๏ธ ๐ก. |
|||
|
|||
๐ ๐ ๐ฉโ๐ป ๐ฌ โซ๏ธ ๐ <a href="https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features" class="external-link" target="_blank">๐ ๐
โ๏ธ โ "โ"</a>. |
|||
|
|||
๐ **FastAPI** ๐ ๏ธ โ๏ธ ๐ ๐. โ ๐ท ๐. |
|||
|
|||
๐ ๐ ๐ ๐ช ๐ ๐ ๐ฉบ. |
|||
|
|||
๐ฅ โ ๐ ๐จโ๐จ ๐ช โน ๐: |
|||
|
|||
* <a href="https://code.visualstudio.com/" class="external-link" target="_blank">๐ ๐ ๐</a>: |
|||
|
|||
 |
|||
|
|||
* <a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">๐</a>: |
|||
|
|||
 |
|||
|
|||
๐ ๐ ๐ค ๐ ๏ธ ๐ ๐ 5๏ธโฃ๐ ๐ค ๐ช โญ. ๐ผ, `price` ๐ ๐ ๐ป ๐ช (๐ ๐ช โ๏ธ ๐ฆ) ๐ ๐ โช๏ธโก๏ธ ๐จ. |
|||
|
|||
๐
โโ ๐ โจ โ ๐ ๐, ๐ ๐ & โก ๐ ๐ฉบ, โ๏ธ ๐ ๐ & ๐ฝ ๐ ๐ฅ ๐ ๐ โ๏ธ `username` โ๏ธ `user_name`. |
|||
|
|||
### ๐ |
|||
|
|||
โซ๏ธ โ๏ธ ๐ค **๐ข** ๐, โฎ๏ธ ๐ฆ ๐ณ ๐. ๐ ๐ข ๐ช ๐-๐ง โซ๏ธโ ๐ ๐ช & ๐ฌ ๐ ๏ธ ๐ ๐ช. |
|||
|
|||
โ๏ธ ๐ข, โซ๏ธ ๐ **"๐ท"**. |
|||
|
|||
### ๐ฌ |
|||
|
|||
* ๐ฌ ๐
(โ๏ธ ๐ โ) ๐ **๐ฝ ๐**, ๐: |
|||
* ๐ป ๐ (`dict`). |
|||
* ๐ป ๐ป (`list`) โ ๐ฌ ๐. |
|||
* ๐ป (`str`) ๐, ๐ฌ ๐ & ๐ ๐. |
|||
* ๐ข (`int`, `float`) โฎ๏ธ ๐ & ๐ ๐ฒ, โ๏ธ. |
|||
|
|||
* ๐ฌ ๐
๐ ๐, ๐: |
|||
* ๐. |
|||
* ๐ง. |
|||
* ๐. |
|||
* ...& ๐. |
|||
|
|||
๐ ๐ฌ ๐ต ๐-๐ & ๐๏ธ **Pydantic**. |
|||
|
|||
### ๐โโ & ๐ค |
|||
|
|||
๐โโ & ๐ค ๐ ๏ธ. ๐ต ๐ โ โฎ๏ธ ๐ฝ โ๏ธ ๐ ๐ท. |
|||
|
|||
๐ ๐โโ โ ๐ฌ ๐, ๐: |
|||
|
|||
* ๐บ๐ธ๐ ๐ฐ. |
|||
* **Oauth2๏ธโฃ** (โฎ๏ธ **๐ฅ ๐ค**). โ
๐ฐ ๐ [Oauth2๏ธโฃ โฎ๏ธ ๐ฅ](tutorial/security/oauth2-jwt.md){.internal-link target=_blank}. |
|||
* ๐ ๏ธ ๐: |
|||
* ๐. |
|||
* ๐ข ๐ข. |
|||
* ๐ช, โ๏ธ. |
|||
|
|||
โ ๐ ๐โโ โ โช๏ธโก๏ธ ๐ (๐ **๐ ๐ช**). |
|||
|
|||
๐ ๐ โป ๐งฐ & ๐ฆฒ ๐ โฉ ๐ ๏ธ โฎ๏ธ ๐ โ๏ธ, ๐ ๐ช, ๐ & โ ๐ฝ, โ๏ธ. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
FastAPI ๐ ๐ถ โฉ โ๏ธ, โ๏ธ ๐ถ ๐๏ธ <abbr title='also known as "components", "resources", "services", "providers"'><strong>๐ ๐</strong></abbr> โ๏ธ. |
|||
|
|||
* ๐ ๐ช โ๏ธ ๐, ๐ ๐ โ๏ธ **"๐" ๐**. |
|||
* ๐ **๐ ๐ต** ๐ ๏ธ. |
|||
* ๐ ๐ ๐ช ๐ ๐ฝ โช๏ธโก๏ธ ๐จ & **โ โก ๐ ๏ธ** โ & ๐ง ๐งพ. |
|||
* **๐ง ๐ฌ** *โก ๐ ๏ธ* ๐ข ๐ฌ ๐. |
|||
* ๐โ๐ฆบ ๐ ๐ฉโ๐ป ๐ค โ๏ธ, **๐ฝ ๐**, โ๏ธ. |
|||
* **๐
โโ โ ** โฎ๏ธ ๐ฝ, ๐ธ, โ๏ธ. โ๏ธ โฉ ๐ ๏ธ โฎ๏ธ ๐ ๐ซ. |
|||
|
|||
### โพ "๐-๐" |
|||
|
|||
โ๏ธ ๐ ๐, ๐
โโ ๐ช ๐ซ, ๐ & โ๏ธ ๐ ๐ ๐ช. |
|||
|
|||
๐ ๐ ๏ธ ๐ ๐
โ๏ธ (โฎ๏ธ ๐) ๐ ๐ ๐ช โ "๐-" ๐ ๐ธ 2๏ธโฃ โธ ๐ โ๏ธ ๐ ๐ & โ โ๏ธ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
### ๐ฏ |
|||
|
|||
* 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ <abbr title="The amount of code that is automatically tested">๐ฏ ๐ฐ</abbr>. |
|||
* 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ <abbr title="Python type annotations, with this your editor and external tools can give you better support">๐ โ</abbr> ๐ ๐งข. |
|||
* โ๏ธ ๐ญ ๐ธ. |
|||
|
|||
## ๐ โ |
|||
|
|||
**FastAPI** ๐ ๐ โฎ๏ธ (& โ๏ธ ๐) <a href="https://www.starlette.io/" class="external-link" target="_blank"><strong>๐</strong></a>. , ๐ ๐ ๐ ๐ ๐ โ๏ธ, ๐ ๐ท. |
|||
|
|||
`FastAPI` ๐ค ๐ง-๐ `Starlette`. , ๐ฅ ๐ โช ๐ญ โ๏ธ โ๏ธ ๐, ๐
๐ ๏ธ ๐ ๐ท ๐ ๐. |
|||
|
|||
โฎ๏ธ **FastAPI** ๐ ๐ค ๐ **๐**'โ โ (FastAPI ๐ ๐ ๐): |
|||
|
|||
* ๐ค ๐ ๐ญ. โซ๏ธ <a href="https://github.com/encode/starlette#performance" class="external-link" target="_blank">1๏ธโฃ โฉ ๐ ๐ ๏ธ ๐ช, ๐ ๐ท๐ช โฎ๏ธ **โณ** & **๐ถ**</a>. |
|||
* ** *๏ธโฃ ** ๐โ๐ฆบ. |
|||
* -๐ ๏ธ ๐ฅ ๐. |
|||
* ๐ด & ๐คซ ๐. |
|||
* ๐ฏ ๐ฉโ๐ป ๐ ๐ ๐ธ๐ฒ. |
|||
* **โ**, ๐, ๐ป ๐, ๐ ๐จ. |
|||
* **๐ & ๐ช** ๐โ๐ฆบ. |
|||
* 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ ๐ฏ ๐ฐ. |
|||
* 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ ๐ โ โ. |
|||
|
|||
## Pydantic โ |
|||
|
|||
**FastAPI** ๐ ๐ โฎ๏ธ (& โ๏ธ ๐) <a href="https://pydantic-docs.helpmanual.io" class="external-link" target="_blank"><strong>Pydantic</strong></a>. , ๐ ๐ Pydantic ๐ ๐ โ๏ธ, ๐ ๐ท. |
|||
|
|||
โ
๐ข ๐ โ๏ธ ๐ Pydantic, <abbr title="Object-Relational Mapper">๐</abbr>โ, <abbr title="Object-Document Mapper">๐ญ</abbr>โ ๐ฝ. |
|||
|
|||
๐ โ ๐ ๐ ๐ผ ๐ ๐ช ๐ถโโ๏ธ ๐ ๐ ๐ ๐ค โช๏ธโก๏ธ ๐จ **๐ ๐ฝ**, ๐ โ ๐. |
|||
|
|||
๐ โ ๐ ๐ ๐คญ, ๐ ๐ผ ๐ ๐ช ๐ถโโ๏ธ ๐ ๐ ๐ค โช๏ธโก๏ธ ๐ฝ **๐ ๐ฉโ๐ป**. |
|||
|
|||
โฎ๏ธ **FastAPI** ๐ ๐ค ๐ **Pydantic**'โ โ (FastAPI โ๏ธ ๐ Pydantic ๐ ๐ฝ ๐): |
|||
|
|||
* **๐
โโ ๐ **: |
|||
* ๐
โโ ๐ ๐ ๐ โพ-๐ช๐ธ ๐ก. |
|||
* ๐ฅ ๐ ๐ญ ๐ ๐ ๐ ๐ญ โ โ๏ธ Pydantic. |
|||
* ๐คพ ๐ โฎ๏ธ ๐ **<abbr title="Integrated Development Environment, similar to a code editor">๐พ</abbr>/<abbr title="A program that checks for code errors">๐งถ</abbr>/๐ง **: |
|||
* โฉ๏ธ Pydantic ๐ ๐ ๐ ๐ ๐ ๐ฌ; ๐-๐ ๏ธ, ๐งฝ, โ & ๐ ๐ค ๐ ๐ ๐ท โ โฎ๏ธ ๐ โ ๐ฝ. |
|||
* **โฉ**: |
|||
* <a href="https://pydantic-docs.helpmanual.io/benchmarks/" class="external-link" target="_blank">๐</a> Pydantic โฉ ๐ ๐ ๐ ๐ฏ ๐. |
|||
* โ **๐ ๐**: |
|||
* โ๏ธ ๐ Pydantic ๐ท, ๐ `typing`'โ `List` & `Dict`, โ๏ธ. |
|||
* & ๐ณ โ ๐ ๐ฝ ๐ ๐ฏ & ๐ช ๐ฌ, โ
& ๐ ๐ป ๐. |
|||
* ๐ ๐ช โ๏ธ ๐ **๐ฆ ๐ป** ๐ & โ๏ธ ๐ซ ๐ โ & โ. |
|||
* **๐ง**: |
|||
* Pydantic โ ๐ ๐ ๐ ๐ฌ โ๏ธ ๐ ๐ช โ ๐ฌ โฎ๏ธ ๐ฉโ๐ฌ ๐ ๐ท ๐ โฎ๏ธ ๐ณ ๐จโ๐จ. |
|||
* 1๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ ๐ฏ ๐ฐ. |
@ -0,0 +1,265 @@ |
|||
# โน FastAPI - ๐ค โน |
|||
|
|||
๐ ๐ **FastAPI**โ |
|||
|
|||
๐ ๐ ๐ โน FastAPI, ๐ ๐ฉโ๐ป, & ๐ โ |
|||
|
|||
โ๏ธ ๐ ๐ ๐ ๐ค โน โฎ๏ธ **FastAPI**โ |
|||
|
|||
๐ค ๐ถ ๐
๐ โน (๐ ๐ 1๏ธโฃ โ๏ธ 2๏ธโฃ ๐). |
|||
|
|||
& ๐ค ๐ ๐ ๐ค โน ๐โโ๏ธ. |
|||
|
|||
## ๐ฑ๐ ๐ฐ |
|||
|
|||
๐ ๐ช ๐ฑ๐ (๐) [**FastAPI & ๐จโ๐งโ๐ฆ** ๐ฐ](/newsletter/){.internal-link target=_blank} ๐ง โน ๐: |
|||
|
|||
* ๐ฐ ๐ FastAPI & ๐จโ๐งโ๐ฆ ๐ถ |
|||
* ๐ฆฎ ๐ถ |
|||
* โ ๐ถ |
|||
* ๐ ๐ ๐ถ |
|||
* ๐โโ & ๐ฑ ๐ถ |
|||
|
|||
## โฉ FastAPI ๐ ๐ฑ๐ |
|||
|
|||
<a href="https://twitter.com/fastapi" class="external-link" target="_blank">โฉ ๐ถ Fastapi ๐ **๐ฑ๐**</a> ๐ค ๐ฐ ๐ฐ ๐ **FastAPI**. ๐ถ |
|||
|
|||
## โด **FastAPI** ๐ |
|||
|
|||
๐ ๐ช "โด" FastAPI ๐ (๐ โด ๐ผ ๐ โถ๏ธ๏ธ): <a href="https://github.com/tiangolo/fastapi" class="external-link" target="_blank">https://github.com/tiangolo/fastapi</a>. ๐ถ ๐ถ |
|||
|
|||
โ โด, ๐ ๐ฉโ๐ป ๐ ๐ช ๐ โซ๏ธ ๐
๐ช & ๐ ๐ โซ๏ธ โ๏ธ โช โ ๐. |
|||
|
|||
## โ ๐ ๐ ๐ |
|||
|
|||
๐ ๐ช "โ" FastAPI ๐ (๐ "โ" ๐ผ ๐ โถ๏ธ๏ธ): <a href="https://github.com/tiangolo/fastapi" class="external-link" target="_blank">https://github.com/tiangolo/fastapi</a>. ๐ถ |
|||
|
|||
๐ค ๐ ๐ช ๐ "๐ ๐ด". |
|||
|
|||
๐จ โซ๏ธ, ๐ ๐ ๐จ ๐จ (๐ ๐ง) ๐โ ๐ค ๐ ๐ (๐ โฌ) **FastAPI** โฎ๏ธ ๐ ๐ง & ๐ โ. |
|||
|
|||
## ๐ โฎ๏ธ ๐ |
|||
|
|||
๐ ๐ช ๐ โฎ๏ธ <a href="https://tiangolo.com" class="external-link" target="_blank">๐ค (๐น๐ฆ ๐ฉ๐ฌ / `tiangolo`)</a>, ๐. |
|||
|
|||
๐ ๐ช: |
|||
|
|||
* <a href="https://github.com/tiangolo" class="external-link" target="_blank">โฉ ๐ค ๐ **๐**</a>. |
|||
* ๐ ๐ ๐ โน ๐ ๐ค โ๏ธ โ ๐ ๐ช โน ๐. |
|||
* โฉ ๐ค ๐ ๐โ ๐ค โ ๐ ๐ โน ๐. |
|||
* <a href="https://twitter.com/tiangolo" class="external-link" target="_blank">โฉ ๐ค ๐ **๐ฑ๐**</a> โ๏ธ <a href="https://fosstodon.org/@tiangolo" class="external-link" target="_blank">โ </a>. |
|||
* ๐ฌ ๐ค โ ๐ โ๏ธ FastAPI (๐ค ๐ ๐ ๐). |
|||
* ๐ ๐โ ๐ค โ ๐ โ๏ธ ๐ ๐ ๐งฐ. |
|||
* ๐ ๐ช <a href="https://twitter.com/fastapi" class="external-link" target="_blank">โฉ ๐ถ Fastapi ๐ ๐ฑ๐</a> (๐ ๐ง). |
|||
* <a href="https://www.linkedin.com/in/tiangolo/" class="external-link" target="_blank">๐ โฎ๏ธ ๐ค ๐ **๐ฑ๐**</a>. |
|||
* ๐ ๐โ ๐ค โ ๐ โ๏ธ ๐ ๐ ๐งฐ (๐ ๐ค โ๏ธ ๐ฑ๐ ๐ ๐ ๐คท โ). |
|||
* โ โซ๏ธโ ๐ค โ (โ๏ธ โฉ ๐ค) ๐ <a href="https://dev.to/tiangolo" class="external-link" target="_blank">**๐ธ๐ฒ.**</a> โ๏ธ <a href="https://medium.com/@tiangolo" class="external-link" target="_blank">**๐**</a>. |
|||
* โ ๐ ๐ญ, ๐, & โ ๐ ๐งฐ ๐ค โ๏ธ โ. |
|||
* โฉ ๐ค โ ๐โ ๐ค โ ๐ณ ๐. |
|||
|
|||
## ๐ฑ๐ ๐ **FastAPI** |
|||
|
|||
<a href="https://twitter.com/compose/tweet?text=I'm loving @fastapi because... https://github.com/tiangolo/fastapi" class="external-link" target="_blank">๐ฑ๐ ๐ **FastAPI**</a> & โก๏ธ ๐ค & ๐ ๐ญ โซ๏ธโ ๐ ๐ โซ๏ธ. ๐ถ |
|||
|
|||
๐ค ๐ ๐ ๐ โ **FastAPI** ๐โโ โ๏ธ, โซ๏ธโ ๐ โ๏ธ ๐ โซ๏ธ, โ ๐/๐ข ๐ โ๏ธ โซ๏ธ, โ๏ธ. |
|||
|
|||
## ๐ณ FastAPI |
|||
|
|||
* <a href="https://www.slant.co/options/34241/~fastapi-review" class="external-link" target="_blank">๐ณ **FastAPI** ๐</a>. |
|||
* <a href="https://alternativeto.net/software/fastapi/" class="external-link" target="_blank">๐ณ **FastAPI** ๐ฑ</a>. |
|||
* <a href="https://stackshare.io/pypi-fastapi" class="external-link" target="_blank">๐ฌ ๐ โ๏ธ **FastAPI** ๐ โน</a>. |
|||
|
|||
## โน ๐ โฎ๏ธ โ ๐ |
|||
|
|||
๐ ๐ช ๐ & โน ๐ โฎ๏ธ ๐ซ โ: |
|||
|
|||
* <a href="https://github.com/tiangolo/fastapi/discussions/categories/questions?discussions_q=category%3AQuestions+is%3Aunanswered" class="external-link" target="_blank">๐ ๐ฌ</a> |
|||
* <a href="https://github.com/tiangolo/fastapi/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Aquestion+-label%3Aanswered+" class="external-link" target="_blank">๐ โ</a> |
|||
|
|||
๐ ๐ผ ๐ 5๏ธโฃ๐ โช ๐ญ โ ๐ โ. ๐ถ |
|||
|
|||
๐ฅ ๐ ๐ค ๐ ๐ซ๐ซ โฎ๏ธ ๐ซ โ, ๐ ๐ โถ๏ธ๏ธ ๐ [FastAPI ๐ด](fastapi-people.md#experts){.internal-link target=_blank}. ๐ถ |
|||
|
|||
๐ญ, ๐ โ โ: ๐ ๐. ๐ซ๐ซ ๐ โฎ๏ธ ๐ซ ๐ฉ & ๐ ๐ผ ๐ซ ๐ญ ๐ ๐, โ๏ธ ๐ ๐ ๐ ๐ช ๐. ๐ถ |
|||
|
|||
๐ญ **FastAPI** ๐ช ๐ & ๐. ๐ ๐ฐ, ๐ซ ๐ซ ๐ญ โ๏ธ ๐ ๐ญ โคต ๐. ๐ฅ โ๏ธ โ ๐
๐ ๐. |
|||
|
|||
--- |
|||
|
|||
๐ฅ โ โน ๐ โฎ๏ธ โ (๐ฌ โ๏ธ โ): |
|||
|
|||
### ๐ค โ |
|||
|
|||
* โ
๐ฅ ๐ ๐ช ๐ค โซ๏ธโ **๐ฏ** & โ๏ธ ๐ผ ๐จโ๐ผ ๐ฌ. |
|||
|
|||
* โคด๏ธ โ
๐ฅ โ (โญ ๐ช โ) **๐**. |
|||
|
|||
* ๐ ๐ผ โ ๐ญ ๐ ๐ฝ โ โช๏ธโก๏ธ ๐ฉโ๐ป, โ๏ธ ๐ค ๐ช **๐** 1๏ธโฃ. ๐ฅ ๐ ๐ช ๐ค โ & โ๏ธ ๐ผ ๐, ๐ ๐ช ๐ช ๐ค ๐ **๐ โ**. |
|||
|
|||
* ๐ฅ ๐ ๐ช ๐ซ ๐ค โ, ๐ญ ๐ **โน**. |
|||
|
|||
### ๐ฌ โ |
|||
|
|||
๐
๐ผ & ๐ โ ๐ค ๐ณ ๐ ๐จโ๐ผ **โฎ๏ธ ๐**. |
|||
|
|||
๐ ๐ผ ๐ซ ๐ ๐ด ๐ ๐งฌ ๐, โ๏ธ ๐ ๐ซ ๐ฅ **๐ฌ โ **. |
|||
|
|||
* ๐ ๐ช ๐ญ ๐ซ ๐ <a href="https://stackoverflow.com/help/minimal-reproducible-example" class="external-link" target="_blank">โญ, ๐ฌ, ๐ผ</a>, ๐ ๐ ๐ช **๐-๐** & ๐ ๐ ๐ ๐ โ โ๏ธ ๐ญ ๐ซ ๐, โ๏ธ ๐ค ๐ซ โ๏ธ ๐ผ ๐. |
|||
|
|||
* ๐ฅ ๐ ๐ ๐โโ๏ธ ๐, ๐ ๐ช ๐ **โ ๐ผ** ๐ ๐ ๐, ๐งข ๐ ๐ โ . โ๏ธ ๐คฏ ๐ ๐ ๐ช โ ๐ ๐ฐ & โซ๏ธ ๐ช ๐ป ๐ญ ๐ซ โ โ ๐ฅ. |
|||
|
|||
### ๐ค โ |
|||
|
|||
* โฎ๏ธ ๐โโ ๐ช ๐ค โ, ๐ ๐ช ๐ค ๐ซ ๐ช **โ**. |
|||
|
|||
* ๐ ๐ผ, โซ๏ธ ๐ ๐ค ๐ซ **๐ โ โ๏ธ โ๏ธ ๐ผ**, โฉ๏ธ ๐ค 5๏ธโฃ๐ ๐ ๐ โ โซ๏ธ ๐ โซ๏ธโ ๐ซ ๐. |
|||
|
|||
### ๐ญ ๐ |
|||
|
|||
๐ฅ ๐ซ ๐จ, ๐ค โ ๐ค ๐ ๐ โ๏ธ โ ๐ซ โ , ใ, **๐ ๐**โ ๐ฆธ |
|||
|
|||
* ๐, ๐ฅ ๐ โ ๐ซ โ , ๐ ๐ช ๐ญ ๐ซ: |
|||
|
|||
* ๐ ๐ฌ: โข ๐ค **โ**. |
|||
* ๐ โ: **๐** โ**. |
|||
|
|||
## โ ๐ ๐ |
|||
|
|||
๐ ๐ช "โ" FastAPI ๐ (๐ "โ" ๐ผ ๐ โถ๏ธ๏ธ): <a href="https://github.com/tiangolo/fastapi" class="external-link" target="_blank">https://github.com/tiangolo/fastapi</a>. ๐ถ |
|||
|
|||
๐ฅ ๐ ๐ "๐" โฉ๏ธ "๐ ๐ด" ๐ ๐ ๐จ ๐จ ๐โ ๐ฑ โ ๐ โ โ๏ธ โ. ๐ ๐ช โ ๐ ๐ ๐ด ๐ ๐จ ๐ ๐ โ, โ๏ธ ๐ฌ, โ๏ธ ๐ธ, โ๏ธ. |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ & โน ๐ซ โ ๐ โ. |
|||
|
|||
## ๐ญ โ |
|||
|
|||
๐ ๐ช <a href="https://github.com/tiangolo/fastapi/discussions/new?category=questions" class="external-link" target="_blank">โ ๐ โ</a> ๐ ๐, ๐ผ: |
|||
|
|||
* ๐ญ **โ** โ๏ธ ๐ญ ๐ **โ **. |
|||
* ๐ค ๐ **โ**. |
|||
|
|||
**๐**: ๐ฅ ๐ โซ๏ธ, โคด๏ธ ๐ค ๐ ๐ญ ๐ โน ๐. ๐ถ |
|||
|
|||
## ๐ ๐ฒ ๐จ |
|||
|
|||
๐ ๐ช โน ๐ค ๐ ๐ฒ ๐จ โช๏ธโก๏ธ ๐. |
|||
|
|||
๐, ๐ ๐ ๐ ๐ ๐. ๐ถ |
|||
|
|||
--- |
|||
|
|||
๐ฅ โซ๏ธโ โ๏ธ ๐คฏ & โ ๐ ๐ฒ ๐จ: |
|||
|
|||
### ๐ค โ |
|||
|
|||
* ๐ฅ, โ ๐ญ ๐ **๐ค โ ** ๐ ๐ฒ ๐จ ๐ โ. โซ๏ธ ๐ช โ๏ธ ๐ ๐ฌ ๐ ๐ฌ โ๏ธ โ. |
|||
|
|||
* ๐ค ๐ ๐ค ๐ ๐ฒ ๐จ ๐ซ ๐ค ๐ช โฉ๏ธ โ ๐ช โ **๐ ๐**. โคด๏ธ ๐ ๐ช ๐ค โ๏ธ ๐ญ ๐ ๐. |
|||
|
|||
### ๐ซ ๐ ๐ ๐ |
|||
|
|||
* ๐ซ ๐ ๐โโ๏ธ ๐
๐ ๐ ๐ ๐ ๐ง ๐, ๐ค ๐ ๐ฅฌ & ๐ ๐ ๐ โ. |
|||
|
|||
* ๐ซ ๐ ๐ ๐ ๐ซ, ๐ค โช ๐ง ๐งฐ โ
๐. |
|||
|
|||
& ๐ฅ ๐ค ๐ ๐ ๐ โ๏ธ โ ๐ช, ๐ค ๐ ๐ญ ๐ ๐, โ๏ธ ๐ค ๐ ๐ฎ ๐ ๐ ๐ โฎ๏ธ ๐ช ๐. |
|||
|
|||
### โ
๐ |
|||
|
|||
* โ
& โ ๐, ๐ ๐ฅ โซ๏ธ โ ๐, **๐ โซ๏ธ ๐** & ๐ ๐ฅ โซ๏ธ ๐ค โ โ . |
|||
|
|||
* โคด๏ธ **๐ค** ๐ฌ ๐ ๐ ๐, ๐ โ ๐ค ๐ ๐ญ ๐ ๐ค โ
โซ๏ธ. |
|||
|
|||
!!! info |
|||
๐, ๐ค ๐ช ๐ซ ๐ฏ ๐ ๐ธ ๐ โ๏ธ ๐ โ. |
|||
|
|||
๐ ๐ฐ โซ๏ธ โ๏ธ ๐จ ๐ ๐ค ๐ธ โฎ๏ธ 3๏ธโฃ, 5๏ธโฃ โ๏ธ ๐
โ, ๐ฒ โฉ๏ธ ๐ ๐, โ๏ธ ๐โ ๐ค โ
๐ธ, ๐ซ ๐ค ๐, โ๏ธ ๐, โ๏ธ ๐ซ โ โ ๐ซ ๐ โ. ๐ถ |
|||
|
|||
, โซ๏ธ ๐ค โ ๐ ๐ ๐ค โ & ๐ ๐, & โก๏ธ ๐ค ๐ญ ๐ค ๐ ๐. ๐ถ |
|||
|
|||
* ๐ฅ ๐ต๐ท ๐ช ๐ ๐, ๐ ๐ช ๐ญ ๐, โ๏ธ ๐ค ๐
โโ ๐ช ๐โโ๏ธ ๐, ๐ค 5๏ธโฃ๐ ๐ ๐ค โ ๐ (& ๐ค ๐ โ๏ธ ๐ ๐ ๐ ๐ถ), โซ๏ธ ๐ป ๐ฅ ๐ ๐ช ๐ฏ ๐ โ ๐. |
|||
|
|||
### ๐ฏ |
|||
|
|||
* โน ๐ค โ
๐ ๐ต๐ท โ๏ธ **๐ฏ**. |
|||
|
|||
* โ
๐ ๐ฏ **โ** โญ ๐ต๐ท. ๐ถ |
|||
|
|||
* โคด๏ธ โ
๐ ๐ฏ **๐ถโโ๏ธ** โฎ๏ธ ๐ต๐ท. ๐ถ |
|||
|
|||
* ๐ ๐ธ ๐ซ โ๏ธ ๐ฏ, ๐ ๐ช **๐** ๐ซ ๐ฎ ๐ฏ, โ๏ธ ๐ ๐ช **๐ค** ๐ฏ ๐. ๐ 1๏ธโฃ ๐ ๐ ๐ด ๐
๐ฐ & ๐ ๐ช โน ๐ โฎ๏ธ ๐. |
|||
|
|||
* โคด๏ธ ๐ค โซ๏ธโ ๐ ๐, ๐ ๐ ๐ค ๐ ๐ญ ๐ ๐ โ
โซ๏ธ. ๐ถ |
|||
|
|||
## โ ๐ฒ ๐จ |
|||
|
|||
๐ ๐ช [๐](contributing.md){.internal-link target=_blank} โน ๐ โฎ๏ธ ๐ฒ ๐จ, ๐ผ: |
|||
|
|||
* ๐ง ๐คญ ๐ ๐ ๐ ๐งพ. |
|||
* ๐ฐ ๐, ๐น, โ๏ธ ๐ป ๐ โ โ๏ธ ๐ ๐ FastAPI <a href="https://github.com/tiangolo/fastapi/edit/master/docs/en/data/external_links.yml" class="external-link" target="_blank">โ ๐ ๐</a>. |
|||
* โ ๐ญ ๐ ๐ฎ ๐ ๐ โถ๏ธ ๐ ๐. |
|||
* โน [๐ฌ ๐งพ](contributing.md#translations){.internal-link target=_blank} ๐ ๐ช๐ธ. |
|||
* ๐ ๐ช โน ๐ โ โ ๐. |
|||
* ๐ ๏ธ ๐ ๐งพ ๐. |
|||
* ๐ง โป โ/๐. |
|||
* โ ๐ญ ๐ฎ ๐ฏ. |
|||
* ๐ฎ ๐ โ. |
|||
* โ ๐ญ ๐ฎ ๐ฏ. |
|||
* โ ๐ญ ๐ฎ ๐งพ ๐ฅ โซ๏ธ ๐. |
|||
|
|||
## โน ๐ง FastAPI |
|||
|
|||
โน ๐ค ๐ง **FastAPI**โ ๐ถ |
|||
|
|||
๐ค ๐ ๐ท, & ๐ โซ๏ธ, **๐** ๐ช โซ๏ธ. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ช โถ๏ธ๏ธ ๐: |
|||
|
|||
* [โน ๐ โฎ๏ธ โ ๐](#help-others-with-questions-in-github){.internal-link target=_blank} (๐ ๐ ๐). |
|||
* [๐ ๐ฒ ๐จ](#review-pull-requests){.internal-link target=_blank} (๐ ๐ ๐). |
|||
|
|||
๐ 2๏ธโฃ ๐ โซ๏ธโ **๐ด ๐ฐ ๐**. ๐ ๐ ๐ท ๐ FastAPI. |
|||
|
|||
๐ฅ ๐ ๐ช โน ๐ค โฎ๏ธ ๐, **๐ ๐ค ๐ค ๐ง FastAPI** & โ ๐ญ โซ๏ธ ๐ง **๐ ๏ธ โฉ & ๐ป**. ๐ถ |
|||
|
|||
## ๐ ๐ฌ |
|||
|
|||
๐ ๐ถ <a href="https://discord.gg/VQjSZaeJmf" class="external-link" target="_blank">๐ง ๐ฌ ๐ฝ</a> ๐ถ & ๐ค ๐
โฎ๏ธ ๐ FastAPI ๐ช. |
|||
|
|||
!!! tip |
|||
โ, ๐ญ ๐ซ <a href="https://github.com/tiangolo/fastapi/discussions/new?category=questions" class="external-link" target="_blank">๐ ๐ฌ</a>, ๐ค ๐
๐ ๐ค ๐ ๐ ๐จ โน [FastAPI ๐ด](fastapi-people.md#experts){.internal-link target=_blank}. |
|||
|
|||
โ๏ธ ๐ฌ ๐ด ๐ ๐ข ๐ฌ. |
|||
|
|||
๐ค โฎ๏ธ <a href="https://gitter.im/tiangolo/fastapi" class="external-link" target="_blank">๐ฅ ๐ฌ</a>, โ๏ธ โซ๏ธ ๐ซ โ๏ธ ๐ป & ๐ง โ, ๐ฌ ๐ โ , ๐ง ๐ ๐ โ๏ธ. |
|||
|
|||
### ๐ซ โ๏ธ ๐ฌ โ |
|||
|
|||
โ๏ธ ๐คฏ ๐ ๐ฌ โ ๐
"๐ ๐ฌ", โซ๏ธ โฉ ๐ญ โ ๐ ๐โโ๏ธ ๐ข & ๐
โ โ,, ๐ ๐ช ๐ซ ๐จ โ. |
|||
|
|||
๐, ๐ ๐ ๐ฆฎ ๐ โ โถ๏ธ๏ธ โ ๐ ๐ ๐ช ๐ ๐ช ๐ค ๐ โ, โ๏ธ โ โ ๐ โญ ๐ฌ. & ๐ ๐ค ๐ช โ ๐ญ ๐ค ๐ง โ ๐, ๐ฅ โซ๏ธ โ ๐ฐ. ๐ค ๐ช ๐ซ ๐ค ๐ โฎ๏ธ ๐ฌ โ๏ธ. ๐ถ |
|||
|
|||
๐ฌ ๐ฌ โ๏ธ ๐ซ ๐ช ๐ ๐, โ & โ 5๏ธโฃ๐ ๐ค ๐ธ ๐ฌ. & ๐ด ๐ ๐ ๐ฏ โถ๏ธ๏ธ [FastAPI ๐ด](fastapi-people.md#experts){.internal-link target=_blank}, ๐ ๐ ๐
๐ฒ ๐จ ๐
๐ ๐. |
|||
|
|||
๐ ๐ ๐, ๐ค ๐ฏ ๐ฉโ๐ป ๐ฌ โ๏ธ, ๐ค โ ๐ค ๐ ๐ ๐ ๐ฑ ๐ฌ ๐ค, ๐ ๐ ๐ฐ. ๐ถ |
|||
|
|||
## ๐ฐ ๐ |
|||
|
|||
๐ ๐ช ๐ฐ ๐โ๐ฆบ ๐ (๐ค) ๐ <a href="https://github.com/sponsors/tiangolo" class="external-link" target="_blank">๐ ๐ฐ</a>. |
|||
|
|||
๐ค ๐ ๐ช ๐ ๐ค โ ๐ถ ๐ถ ๐ฌ ๐. ๐ถ |
|||
|
|||
& ๐ ๐ช โถ๏ธ๏ธ ๐ฅ1st โ๏ธ ๐ ๐ฐ FastAPI. ๐ถ ๐ถ |
|||
|
|||
## ๐ฐ ๐งฐ ๐ ๐๏ธ FastAPI |
|||
|
|||
๐ โ๏ธ ๐ ๐งพ, FastAPI ๐ง ๐ โ ๐, ๐ & Pydantic. |
|||
|
|||
๐ ๐ช ๐ฐ: |
|||
|
|||
* <a href="https://github.com/sponsors/samuelcolvin" class="external-link" target="_blank">โก ๐ (Pydantic)</a> |
|||
* <a href="https://github.com/sponsors/encode" class="external-link" target="_blank">๐ (๐, Uvicorn)</a> |
|||
|
|||
--- |
|||
|
|||
๐ โ ๐ถ |
@ -0,0 +1,79 @@ |
|||
# ๐, ๐ง & ๐ฎ |
|||
|
|||
๐ฐ ๐, <a href="https://github.com/tiangolo/fastapi/issues/3#issuecomment-454956920" class="external-link" target="_blank"> **FastAPI** ๐ฉโ๐ป ๐ญ</a>: |
|||
|
|||
> โซ๏ธโ ๐ ๐ ๐ โ โซ๏ธ ๐ โ๏ธ ๐ โช๏ธโก๏ธ ๐ณ ๐ ๐ฉโโคโ๐จ ๐๏ธ [...] |
|||
|
|||
๐ฅ ๐ฅ ๐ ๐ ๐. |
|||
|
|||
## ๐ |
|||
|
|||
๐ค โ๏ธ ๐ ๐ โฎ๏ธ ๐ ๐ ๐ 1๏ธโฃ2๏ธโฃ๐๏ธ (๐ฐ ๐ซ, ๐ โ๏ธ, ๐ ๐จโ๐ญ, โ ๐ฝ, โ๏ธ), โ๏ธ ๐ ๐ ๐ฉโ๐ป. |
|||
|
|||
๐ ๐, ๐ค ๐ช ๐ฌ, ๐ฏ & โ๏ธ ๐ ๐. |
|||
|
|||
๐ **FastAPI** ๐ ๐ ๐ ๐ฎ โช. |
|||
|
|||
๐โโ ๐ [๐](alternatives.md){.internal-link target=_blank}: |
|||
|
|||
<blockquote markdown="1"> |
|||
|
|||
**FastAPI** ๐ซ๐ ๐ ๐ฅ ๐ซ โฎ๏ธ ๐ท ๐. |
|||
|
|||
๐ค โ๏ธ ๐ ๐งฐ โ โญ ๐ โ๏ธ โน ๐ฎ ๐ฎ ๐. |
|||
|
|||
๐ค โ๏ธ โ ๐ ๐ ๐ ๏ธ ๐ 1๏ธโฃ2๏ธโฃ๐๏ธ. ๐ฅ ๐ค ๐ โ ๐ โ ๐ **FastAPI** โ๏ธ ๐ ๐ ๐ ๏ธ, ๐-๐, & ๐งฐ. |
|||
|
|||
โ๏ธ โ, ๐ค ๐
โโ ๐ ๐ ๐ ๐ ๐ณ ๐ ๐ ๐ ๐ซ โ, โ ๐ ๐ญ โช๏ธโก๏ธ โฎ๏ธ ๐งฐ, & ๐ ๐ซ ๐ ๐ ๐ช, โ๏ธ ๐ช๐ธ โ ๐ โ๐ซ ๐ช โญ (๐ 3๏ธโฃ.6๏ธโฃ โ ๐ ๐). |
|||
|
|||
</blockquote> |
|||
|
|||
## ๐ฌ |
|||
|
|||
โ๏ธ ๐ โฎ๏ธ ๐ ๐ค โ๏ธ ๐ค ๐ก โช๏ธโก๏ธ ๐ ๐ซ, โ ๐ญ, & ๐ ๐ซ ๐ ๐ ๐ค ๐ช ๐ ๐ค & ๐ ๐ฉโ๐ป ๐ค โ๏ธ ๐ท โฎ๏ธ. |
|||
|
|||
๐ผ, โซ๏ธ ๐ ๐ ๐ฒ โซ๏ธ ๐ โ๏ธ ๐ ๐ฉ ๐ ๐ ๐. |
|||
|
|||
, ๐ ๐ฏ โ๏ธ โช โป ๐ฉ. |
|||
|
|||
, โญ โถ๏ธ ๐ **FastAPI**, ๐ค ๐ธ ๐ ๐๏ธ ๐ ๐ ๐, ๐ป ๐, Oauth2๏ธโฃ, โ๏ธ. ๐ฏ ๐ซ ๐, ๐, & ๐บ. |
|||
|
|||
## ๐ง |
|||
|
|||
โคด๏ธ ๐ค ๐ธ ๐ฐ ๐ง ๐ฉโ๐ป "๐ ๏ธ" ๐ค ๐ โ๏ธ ๐ฉโ๐ป (๐ฉโ๐ป โ๏ธ FastAPI). |
|||
|
|||
๐ค ๐ฏ ๐ ๐ญ ๐ ๐ ๐ ๐จโ๐จ: ๐, ๐ ๐, ๐ ๐งข ๐จโ๐จ. |
|||
|
|||
๐ <a href="https://www.jetbrains.com/research/python-developers-survey-2018/#development-tools" class="external-link" target="_blank">๐ ๐ฉโ๐ป ๐ฌ</a>, ๐ ๐ ๐ 8๏ธโฃ0๏ธโฃ ๐ฏ ๐ฉโ๐ป. |
|||
|
|||
โซ๏ธ โ ๐ **FastAPI** ๐ฏ ๐ฏ โฎ๏ธ ๐จโ๐จ โ๏ธ 8๏ธโฃ0๏ธโฃ ๐ฏ ๐ ๐ฉโ๐ป. & ๐ ๐ ๐จโ๐จ ๐ ๐ท โก, ๐ ๐ฎ ๐ฐ ๐ ๐ท ๐ ๐ ๐จโ๐จ. |
|||
|
|||
๐ ๐ ๐ค ๐ช ๐ ๐ ๐ ๐ ๐ โ ๐
๐ช, โ๏ธ ๐ ๏ธ ๐, ๐ & โ โ
, โ๏ธ. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ ๐ ๏ธ ๐ก ๐ ๐ฉโ๐ป. |
|||
|
|||
## ๐ |
|||
|
|||
โฎ๏ธ ๐ฌ ๐ ๐, ๐ค ๐ญ ๐ ๐ค ๐ โ๏ธ <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">**Pydantic**</a> ๐ฎ ๐. |
|||
|
|||
โคด๏ธ ๐ค ๐ โซ๏ธ, โ โซ๏ธ ๐ ๐ ๏ธ โฎ๏ธ ๐ป ๐, ๐โ๐ฆบ ๐ ๐ ๐ฌ โ ๐, & ๐ ๐จโ๐จ ๐โ๐ฆบ (๐ โ
, โ) โ๏ธ ๐ ๐ฏ ๐ ๐จโ๐จ. |
|||
|
|||
โฎ๏ธ ๐ ๏ธ, ๐ค ๐ <a href="https://www.starlette.io/" class="external-link" target="_blank">**๐**</a>, ๐ ๐ ๐. |
|||
|
|||
## ๐ ๏ธ |
|||
|
|||
๐ฐ ๐ค โถ๏ธ ๐ **FastAPI** โซ๏ธ, ๐ ๐ โช ๐ฅ, ๐ง ๐ฌ, ๐ & ๐งฐ ๐, & ๐ก ๐ ๐ฉ & ๐ง ๐ & ๐. |
|||
|
|||
## ๐ฎ |
|||
|
|||
๐ โ, โซ๏ธ โช ๐ ๐ **FastAPI** โฎ๏ธ ๐ฎ ๐ญ โ โ ๐ ๐ซ๐ซ. |
|||
|
|||
โซ๏ธ ๐โโ ๐ ๐คญ โฎ๏ธ ๐ โฃ ๐ โ๏ธ ๐ผ ๐. |
|||
|
|||
๐ ๐ฉโ๐ป & ๐ โช ๐ช ๐ **FastAPI** ๐ซ ๐ (๐ ๐ค & ๐ ๐). |
|||
|
|||
โ๏ธ, ๐ค ๐ ๐ & โ ๐. |
|||
|
|||
**FastAPI** โ๏ธ ๐ ๐ฎ โคด๏ธ. |
|||
|
|||
& [๐ โน](help-fastapi.md){.internal-link target=_blank} ๐ ๐. |
@ -0,0 +1,58 @@ |
|||
# ๐ฒ ๐ |
|||
|
|||
๐ฅ ๐ ๐ช, ๐ ๐ช โ๏ธ โ & ๐ ๐ข ๐ ๐ โ โ๏ธ ๐ ๐, & โ โซ๏ธ ๐. |
|||
|
|||
## ๐ ๐โโ, ๐, & ๐ฉบ |
|||
|
|||
๐ตโโ ๐ ๐งพ ๐ฉโ๐ป ๐ข ๐ญ *๐ซ๐ ๐ซ* ๐ ๐ก ๐ ๐ ๏ธ. |
|||
|
|||
๐ ๐ซ ๐ฎ ๐ โ ๐โโ ๐ ๐ ๏ธ, *โก ๐ ๏ธ* ๐ ๐ช ๐โ ๐ซ. |
|||
|
|||
๐ฅ ๐ค ๐โโ โ ๐ ๐, โซ๏ธ ๐ ๐. |
|||
|
|||
๐ตโโ ๐งพ โ โซ๏ธ ๐
โ ๐ค โ ๐ โฎ๏ธ ๐ ๐ ๏ธ, & ๐ช โ โซ๏ธ ๐
โ ๐ โน โซ๏ธ ๐ญ. โซ๏ธ ๐ช ๐ค ๐ฏ ๐จ <a href="https://en.wikipedia.org/wiki/Security_through_obscurity" class="external-link" target="_blank">๐โโ ๐ ๐</a>. |
|||
|
|||
๐ฅ ๐ ๐ ๐ ๐ ๐ ๏ธ, ๐ค ๐ ๐ ๐ ๐ ๐ช, ๐ผ: |
|||
|
|||
* โ ๐ญ ๐ โ๏ธ ๐ ๐ฌ Pydantic ๐ท ๐ ๐จ ๐ช & ๐จ. |
|||
* ๐ ๐ โ โ & ๐ โ๏ธ ๐. |
|||
* ๐
๐ช ๐ข ๐, ๐ด ๐#๏ธโฃ. |
|||
* ๐ ๏ธ & โ๏ธ ๐-๐ญ ๐ ๐งฐ, ๐ ๐ธ๐ฒ & ๐ฅ ๐ค, โ๏ธ. |
|||
* ๐ฎ ๐
๐งฝ โ ๐ โฎ๏ธ Oauth2๏ธโฃ โ ๐โ ๐ช. |
|||
* ...โ๏ธ. |
|||
|
|||
๐, ๐ 5๏ธโฃ๐ โ๏ธ ๐ถ ๐ฏ โ๏ธ ๐ผ ๐โ ๐ ๐ค ๐ช โ ๐ ๏ธ ๐ฉบ ๐ (โ
๐ญ) โ๏ธ โ๏ธ ๐ ๐ณ โช๏ธโก๏ธ ๐ ๐ข. |
|||
|
|||
## ๐ฒ ๐ โช๏ธโก๏ธ โ & ๐จ๐ป { |
|||
|
|||
๐ ๐ช ๐ช โ๏ธ ๐ Pydantic โ ๐ ๐ ๐ ๐ & ๐ฉบ โ. |
|||
|
|||
๐ผ: |
|||
|
|||
```Python hl_lines="6 11" |
|||
{!../../../docs_src/conditional_openapi/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ฅ ๐ฃ โ `openapi_url` โฎ๏ธ ๐ ๐ข `"/openapi.json"`. |
|||
|
|||
& โคด๏ธ ๐ฅ โ๏ธ โซ๏ธ ๐โ ๐ `FastAPI` ๐ฑ. |
|||
|
|||
โคด๏ธ ๐ ๐ช โ ๐ (โ
๐ ๐ฉบ) โ ๐ ๐ข `OPENAPI_URL` ๐ ๐ป, ๐: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ OPENAPI_URL= uvicorn main:app |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โคด๏ธ ๐ฅ ๐ ๐ถ ๐ `/openapi.json`, `/docs`, โ๏ธ `/redoc` ๐ ๐ ๐ค `404 Not Found` โ ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"detail": "Not Found" |
|||
} |
|||
``` |
@ -0,0 +1,109 @@ |
|||
# ๐ ๐จ & APIRoute ๐ |
|||
|
|||
๐ผ, ๐ 5๏ธโฃ๐ ๐ ๐ โ โ๏ธ `Request` & `APIRoute` ๐. |
|||
|
|||
๐ฏ, ๐ 5๏ธโฃ๐ ๐ ๐ โ ๐ ๏ธ. |
|||
|
|||
๐ผ, ๐ฅ ๐ ๐ โ โ๏ธ ๐ฌ ๐จ ๐ช โญ โซ๏ธ ๐ ๏ธ ๐ ๐ธ. |
|||
|
|||
!!! danger |
|||
๐ "๐ง" โ. |
|||
|
|||
๐ฅ ๐ โถ๏ธ โฎ๏ธ **FastAPI** ๐ ๐ช ๐ ๐ถ ๐ ๐. |
|||
|
|||
## โ๏ธ ๐ผ |
|||
|
|||
โ๏ธ ๐ผ ๐: |
|||
|
|||
* ๐ญ ๐ซ-๐ป ๐จ ๐ช ๐ป (โ
<a href="https://msgpack.org/index.html" class="external-link" target="_blank">`msgpack`</a>). |
|||
* ๐ ๐-๐ ๐จ ๐ช. |
|||
* ๐ ๐จ ๐ ๐จ ๐ช. |
|||
|
|||
## ๐ ๐ ๐จ ๐ช ๐ข |
|||
|
|||
โก๏ธ ๐ โ โ โ๏ธ ๐ `Request` ๐ฟ ๐ ๐ ๐จ. |
|||
|
|||
& `APIRoute` ๐ฟ โ๏ธ ๐ ๐ ๐จ ๐. |
|||
|
|||
### โ ๐ `GzipRequest` ๐ |
|||
|
|||
!!! tip |
|||
๐ ๐งธ ๐ผ ๐ฆ โ โซ๏ธ ๐ท, ๐ฅ ๐ ๐ช ๐ ๐โ๐ฆบ, ๐ ๐ช โ๏ธ ๐ [`GzipMiddleware`](./middleware.md#gzipmiddleware){.internal-link target=_blank}. |
|||
|
|||
๐ฅ, ๐ฅ โ `GzipRequest` ๐, โ ๐ ๐ `Request.body()` ๐ฉโ๐ฌ ๐ ๐ช ๐ โ ๐. |
|||
|
|||
๐ฅ ๐ค ๐
โโ `gzip` ๐, โซ๏ธ ๐ ๐ซ ๐ ๐ ๐ช. |
|||
|
|||
๐ ๐, ๐ ๐ฃ ๐ ๐ช ๐ต ๐ ๐ โ๏ธ ๐ ๐จ. |
|||
|
|||
```Python hl_lines="8-15" |
|||
{!../../../docs_src/custom_request_and_route/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ ๐ `GzipRoute` ๐ |
|||
|
|||
โญ, ๐ฅ โ ๐ ๐ฟ `fastapi.routing.APIRoute` ๐ ๐ โ โ๏ธ `GzipRequest`. |
|||
|
|||
๐ ๐ฐ, โซ๏ธ ๐ ๐ ๐ฉโ๐ฌ `APIRoute.get_route_handler()`. |
|||
|
|||
๐ ๐ฉโ๐ฌ ๐จ ๐ข. & ๐ ๐ข โซ๏ธโ ๐ ๐จ ๐จ & ๐จ ๐จ. |
|||
|
|||
๐ฅ ๐ฅ โ๏ธ โซ๏ธ โ `GzipRequest` โช๏ธโก๏ธ โฎ๏ธ ๐จ. |
|||
|
|||
```Python hl_lines="18-26" |
|||
{!../../../docs_src/custom_request_and_route/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! note "๐ก โน" |
|||
`Request` โ๏ธ `request.scope` ๐ข, ๐ ๐ `dict` โ ๐ ๐ ๐จ. |
|||
|
|||
`Request` โ๏ธ `request.receive`, ๐ ๐ข "๐จ" ๐ช ๐จ. |
|||
|
|||
`scope` `dict` & `receive` ๐ข ๐ฏโโ๏ธ ๐ ๐ซ ๐ง. |
|||
|
|||
& ๐ 2๏ธโฃ ๐, `scope` & `receive`, โซ๏ธโ ๐ช โ ๐ `Request` ๐. |
|||
|
|||
๐ก ๐
๐ `Request` โ
<a href="https://www.starlette.io/requests/" class="external-link" target="_blank">๐ ๐ฉบ ๐ ๐จ</a>. |
|||
|
|||
๐ด ๐ ๐ข ๐จ `GzipRequest.get_route_handler` ๐จ ๐ ๐ `Request` `GzipRequest`. |
|||
|
|||
๐จ ๐, ๐ `GzipRequest` ๐ โ ๐
๐ ๐ (๐ฅ ๐ช) โญ ๐ถโโ๏ธ โซ๏ธ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
โฎ๏ธ ๐, ๐ ๐ญ โ ๐. |
|||
|
|||
โ๏ธ โฉ๏ธ ๐ ๐ `GzipRequest.body`, ๐จ ๐ช ๐ ๐ ๐ ๐โ โซ๏ธ ๐ **FastAPI** ๐โ ๐ช. |
|||
|
|||
## ๐ ๐จ ๐ช โ ๐โ๐ฆบ |
|||
|
|||
!!! tip |
|||
โ ๐ ๐ โ , โซ๏ธ ๐ฒ ๐ โฉ โ๏ธ `body` ๐ ๐โ๐ฆบ `RequestValidationError` ([๐ โ](../tutorial/handling-errors.md#use-the-requestvalidationerror-body){.internal-link target=_blank}). |
|||
|
|||
โ๏ธ ๐ ๐ผ โ & โซ๏ธ ๐ฆ โ ๐ โฎ๏ธ ๐ ๐ฆฒ. |
|||
|
|||
๐ฅ ๐ช โ๏ธ ๐ ๐ ๐ฏ ๐ ๐จ ๐ช โ ๐โ๐ฆบ. |
|||
|
|||
๐ ๐ฅ ๐ช ๐ต ๐จ ๐ `try`/`except` ๐ซ: |
|||
|
|||
```Python hl_lines="13 15" |
|||
{!../../../docs_src/custom_request_and_route/tutorial002.py!} |
|||
``` |
|||
|
|||
๐ฅ โ ๐, `Request` ๐ ๐ โ, ๐ฅ ๐ช โ & โ โ๏ธ ๐จ ๐ช ๐โ ๐ โ: |
|||
|
|||
```Python hl_lines="16-18" |
|||
{!../../../docs_src/custom_request_and_route/tutorial002.py!} |
|||
``` |
|||
|
|||
## ๐ `APIRoute` ๐ ๐ป |
|||
|
|||
๐ ๐ช โ `route_class` ๐ข `APIRouter`: |
|||
|
|||
```Python hl_lines="26" |
|||
{!../../../docs_src/custom_request_and_route/tutorial003.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, *โก ๐ ๏ธ* ๐ฝ `router` ๐ โ๏ธ ๐ `TimedRoute` ๐, & ๐ โ๏ธ โ `X-Response-Time` ๐ ๐จ โฎ๏ธ ๐ฐ โซ๏ธ โ ๐ ๐จ: |
|||
|
|||
```Python hl_lines="13-20" |
|||
{!../../../docs_src/custom_request_and_route/tutorial003.py!} |
|||
``` |
@ -0,0 +1,90 @@ |
|||
# โ ๐ |
|||
|
|||
!!! warning |
|||
๐ ๐ ๐ง โ. ๐ ๐ฒ ๐ช ๐ถ โซ๏ธ. |
|||
|
|||
๐ฅ ๐ ๐ ๐ฐ - ๐ฉโ๐ป ๐ฆฎ, ๐ ๐ช ๐ฒ ๐ถ ๐ ๐. |
|||
|
|||
๐ฅ ๐ โช ๐ญ ๐ ๐ ๐ช ๐ ๐ ๐ ๐, ๐ฃ ๐. |
|||
|
|||
๐ค ๐ผ ๐โ ๐ ๐ช ๐ช ๐ ๐ ๐ ๐. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ โ. |
|||
|
|||
## ๐ ๐ ๏ธ |
|||
|
|||
๐ (๐ข) ๐ ๏ธ, โฉ. |
|||
|
|||
`FastAPI` ๐ธ (๐) โ๏ธ `.openapi()` ๐ฉโ๐ฌ ๐ ๐ ๐จ ๐ ๐. |
|||
|
|||
๐ ๐ธ ๐ ๐, *โก ๐ ๏ธ* `/openapi.json` (โ๏ธ โซ๏ธโ ๐ โ ๐ `openapi_url`) ยฎ. |
|||
|
|||
โซ๏ธ ๐จ ๐ป ๐จ โฎ๏ธ ๐ ๐ธ `.openapi()` ๐ฉโ๐ฌ. |
|||
|
|||
๐ข, โซ๏ธโ ๐ฉโ๐ฌ `.openapi()` ๐จ โ
๐ `.openapi_schema` ๐ ๐ฅ โซ๏ธ โ๏ธ ๐ & ๐จ ๐ซ. |
|||
|
|||
๐ฅ โซ๏ธ ๐ซ, โซ๏ธ ๐ ๐ซ โ๏ธ ๐ ๐ข `fastapi.openapi.utils.get_openapi`. |
|||
|
|||
& ๐ ๐ข `get_openapi()` ๐จ ๐ข: |
|||
|
|||
* `title`: ๐ ๐, ๐ฆ ๐ฉบ. |
|||
* `version`: โฌ ๐ ๐ ๏ธ, โ
`2.5.0`. |
|||
* `openapi_version`: โฌ ๐ ๐ง โ๏ธ. ๐ข, โช: `3.0.2`. |
|||
* `description`: ๐ ๐ ๐ ๏ธ. |
|||
* `routes`: ๐ ๐ฃ, ๐ซ ๐ ยฎ *โก ๐ ๏ธ*. ๐ซ โ โช๏ธโก๏ธ `app.routes`. |
|||
|
|||
## ๐ ๐ข |
|||
|
|||
โ๏ธ โน ๐, ๐ ๐ช โ๏ธ ๐ ๐ ๐ข ๐ ๐ ๐ & ๐ ๐ ๐ ๐ ๐ ๐ช. |
|||
|
|||
๐ผ, โก๏ธ ๐ฎ <a href="https://github.com/Rebilly/ReDoc/blob/master/docs/redoc-vendor-extensions.md#x-logo" class="external-link" target="_blank">๐ ๐ โ ๐ ๐ ๐ฑ</a>. |
|||
|
|||
### ๐ **FastAPI** |
|||
|
|||
๐ฅ, โ ๐ ๐ **FastAPI** ๐ธ ๐: |
|||
|
|||
```Python hl_lines="1 4 7-9" |
|||
{!../../../docs_src/extending_openapi/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ ๐ |
|||
|
|||
โคด๏ธ, โ๏ธ ๐ ๐ ๐ข ๐ ๐ ๐, ๐ `custom_openapi()` ๐ข: |
|||
|
|||
```Python hl_lines="2 15-20" |
|||
{!../../../docs_src/extending_openapi/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ ๐ |
|||
|
|||
๐ ๐ ๐ช ๐ฎ ๐ โ, โ ๐ `x-logo` `info` "๐" ๐ ๐: |
|||
|
|||
```Python hl_lines="21-23" |
|||
{!../../../docs_src/extending_openapi/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐พ ๐ ๐ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ `.openapi_schema` "๐พ", ๐ช ๐ ๐ ๐. |
|||
|
|||
๐ ๐, ๐ ๐ธ ๐ ๐ซ โ๏ธ ๐ ๐ ๐ ๐ฐ ๐ฉโ๐ป ๐ ๐ ๐ ๏ธ ๐ฉบ. |
|||
|
|||
โซ๏ธ ๐ ๐ ๐ด ๐, & โคด๏ธ ๐ ๐พ ๐ ๐ โ๏ธ โญ ๐จ. |
|||
|
|||
```Python hl_lines="13-14 24-25" |
|||
{!../../../docs_src/extending_openapi/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ฉโ๐ฌ |
|||
|
|||
๐ ๐ ๐ช โ `.openapi()` ๐ฉโ๐ฌ โฎ๏ธ ๐ ๐ ๐ข. |
|||
|
|||
```Python hl_lines="28" |
|||
{!../../../docs_src/extending_openapi/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ
โซ๏ธ |
|||
|
|||
๐ ๐ ๐ถ <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a> ๐ ๐ ๐ ๐ ๐ โ๏ธ ๐ ๐ ๐ฑ (๐ ๐ผ, **FastAPI**'โ ๐ฑ): |
|||
|
|||
<img src="/img/tutorial/extending-openapi/image01.png"> |
@ -0,0 +1,56 @@ |
|||
# ๐น |
|||
|
|||
**FastAPI** โ๏ธ ๐ **๐ซ** ๐ฉ, โซ๏ธ ๐ถ โฉ ๐ ๏ธ ๐ **๐น** ๐ ๐ โฎ๏ธ ๐ซ. |
|||
|
|||
๐ ๐ช ๐ ๐ FastAPI *โก ๐ ๏ธ* โฎ๏ธ ๐น ๐ ๐ ๐ธ. |
|||
|
|||
!!! tip |
|||
**๐น** โ ๐ถ ๐ฏ โ๏ธ ๐ผ. |
|||
|
|||
โซ๏ธ โ๏ธ **๐** & **โ ** ๐โ ๐ฌ โ **๐ธ ๐**. |
|||
|
|||
โ ๐ญ ๐ ๐ฌ ๐ฅ **๐ฐ** ๐ โ๏ธ ๐ผ โ **๐**. ๐ถ |
|||
|
|||
## ๐น ๐ |
|||
|
|||
๐ฅ **๐น** ๐ ๐ โ๏ธ **๐ซ** ๐โ๐ฆบ. ๐ ๐ช โ๏ธ ๐ซ โฎ๏ธ **FastAPI**: |
|||
|
|||
* <a href="https://strawberry.rocks/" class="external-link" target="_blank">๐</a> ๐ถ |
|||
* โฎ๏ธ <a href="https://strawberry.rocks/docs/integrations/fastapi" class="external-link" target="_blank">๐ฉบ FastAPI</a> |
|||
* <a href="https://ariadnegraphql.org/" class="external-link" target="_blank">๐ธ</a> |
|||
* โฎ๏ธ <a href="https://ariadnegraphql.org/docs/starlette-integration" class="external-link" target="_blank">๐ฉบ ๐</a> (๐ โ FastAPI) |
|||
* <a href="https://tartiflette.io/" class="external-link" target="_blank">๐</a> |
|||
* โฎ๏ธ <a href="https://tartiflette.github.io/tartiflette-asgi/" class="external-link" target="_blank">๐ ๐ซ</a> ๐ ๐ซ ๐ ๏ธ |
|||
* <a href="https://graphene-python.org/" class="external-link" target="_blank">โ</a> |
|||
* โฎ๏ธ <a href="https://github.com/ciscorn/starlette-graphene3" class="external-link" target="_blank">๐-Graphene3๏ธโฃ</a> |
|||
|
|||
## ๐น โฎ๏ธ ๐ |
|||
|
|||
๐ฅ ๐ ๐ช โ๏ธ ๐ ๐ท โฎ๏ธ **๐น**, <a href="https://strawberry.rocks/" class="external-link" target="_blank">**๐**</a> **๐** ๐ โซ๏ธ โ๏ธ ๐ง ๐ **FastAPI** ๐ง, โซ๏ธ ๐ โ๏ธ ๐ **๐ โ**. |
|||
|
|||
โ๏ธ ๐ ๐ โ๏ธ ๐ผ, ๐ 5๏ธโฃ๐ ๐ โ๏ธ ๐ ๐, โ๏ธ ๐ฅ ๐ ๐ญ ๐ค, ๐ค ๐ ๐ฒ ๐ค ๐ ๐ **๐**. |
|||
|
|||
๐ฅ ๐คช ๐ฎ โ ๐ ๐ช ๐ ๏ธ ๐ โฎ๏ธ FastAPI: |
|||
|
|||
```Python hl_lines="3 22 25-26" |
|||
{!../../../docs_src/graphql/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ ๐ช ๐ก ๐
๐ ๐ <a href="https://strawberry.rocks/" class="external-link" target="_blank">๐ ๐งพ</a>. |
|||
|
|||
& ๐ฉบ ๐ <a href="https://strawberry.rocks/docs/integrations/fastapi" class="external-link" target="_blank">๐ โฎ๏ธ FastAPI</a>. |
|||
|
|||
## ๐ `GraphQLApp` โช๏ธโก๏ธ ๐ |
|||
|
|||
โฎ๏ธ โฌ ๐ ๐ `GraphQLApp` ๐ ๐ ๏ธ โฎ๏ธ <a href="https://graphene-python.org/" class="external-link" target="_blank">โ</a>. |
|||
|
|||
โซ๏ธ ๐ข โช๏ธโก๏ธ ๐, โ๏ธ ๐ฅ ๐ โ๏ธ ๐ ๐ โ๏ธ โซ๏ธ, ๐ ๐ช ๐ช **โ** <a href="https://github.com/ciscorn/starlette-graphene3" class="external-link" target="_blank">๐-Graphene3๏ธโฃ</a>, ๐ ๐ ๐ โ๏ธ ๐ผ & โ๏ธ **๐ ๐ ๐ข**. |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ช ๐น, ๐ค ๐ ๐ ๐ โ
๐
<a href="https://strawberry.rocks/" class="external-link" target="_blank">๐</a>, โซ๏ธ โ๏ธ ๐ ๐ โ โฉ๏ธ ๐ ๐ & ๐. |
|||
|
|||
## ๐ก ๐
|
|||
|
|||
๐ ๐ช ๐ก ๐
๐ **๐น** <a href="https://graphql.org/" class="external-link" target="_blank">๐ ๐น ๐งพ</a>. |
|||
|
|||
๐ ๐ช โ ๐
๐ ๐ ๐ ๐ ๐ฌ ๐ ๐ซ ๐. |
@ -0,0 +1,529 @@ |
|||
# ๐ (๐) ๐ฝ โฎ๏ธ ๐ |
|||
|
|||
!!! warning |
|||
๐ฅ ๐ โถ๏ธ, ๐ฐ [๐ (๐) ๐ฝ](../tutorial/sql-databases.md){.internal-link target=_blank} ๐ โ๏ธ ๐ธ๐ฒ ๐ ๐ฅ. |
|||
|
|||
๐ญ ๐ ๐ถ ๐. |
|||
|
|||
๐ฅ ๐ โถ๏ธ ๐ โช๏ธโก๏ธ ๐, ๐ ๐ฒ ๐ป ๐ โฎ๏ธ ๐ธ๐ฒ ๐ ([๐ (๐) ๐ฝ](../tutorial/sql-databases.md){.internal-link target=_blank}), โ๏ธ ๐ ๐ ๐ ๐. |
|||
|
|||
๐ฅ ๐ โช โ๏ธ ๐ ๐งข ๐ โ๏ธ <a href="https://docs.peewee-orm.com/en/latest/" class="external-link" target="_blank">๐ ๐</a>, ๐ ๐ช โ
๐ฅ โ โ๏ธ โซ๏ธ โฎ๏ธ **FastAPI**. |
|||
|
|||
!!! warning "๐ 3๏ธโฃ.7๏ธโฃ โ โ" |
|||
๐ ๐ ๐ช ๐ 3๏ธโฃ.7๏ธโฃ โ๏ธ ๐ ๐ โ๏ธ ๐ โฎ๏ธ FastAPI. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ ๐ซ ๐ง ๐ ๐ ๏ธ, โ๏ธ โฎ๏ธ ๐ซ ๐คฏ. |
|||
|
|||
๐ โ๏ธ ๐๏ธ ๐ ๐ ๐ฎ ๐ข & ๐ โ โซ๏ธ ๐ โ๏ธ. |
|||
|
|||
๐ฅ ๐ ๐ ๏ธ ๐ธ โฎ๏ธ ๐ ๐ซ-๐ ๐ ๏ธ, & ๐ช ๐ท โฎ๏ธ ๐ ๐ฎ ๐ข, **โซ๏ธ ๐ช ๐ ๐งฐ**. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ช ๐ ๐ข, ๐โ๐ฆบ ๐ ๐ 1๏ธโฃ ๐ ๐ฝ, ๐ท โฎ๏ธ ๐ ๐ ๏ธ (๐ FastAPI), โ๏ธ, ๐ ๐ ๐ช ๐ฎ ๐ โ ๐ ๐ ๐ ๐ข. |
|||
|
|||
๐, โซ๏ธ ๐ช โซ๏ธ, & ๐ฅ ๐ ๐ ๐ โซ๏ธโ โซ๏ธโ ๐ ๐ โ๏ธ ๐ฎ ๐ช โ๏ธ ๐ โฎ๏ธ FastAPI. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ ๐
๐ ๐ ๐ง ๐ ๐ ๐ <a href="https://docs.peewee-orm.com/en/latest/peewee/database.html#async-with-gevent" class="external-link" target="_blank">๐ฉบ</a>, <a href="https://github.com/coleifer/peewee/issues/263#issuecomment-517347032" class="external-link" target="_blank">โ</a>, <a href="https://github.com/coleifer/peewee/pull/2072#issuecomment-563215132" class="external-link" target="_blank">๐ต๐ท</a>. |
|||
|
|||
## ๐ ๐ฑ |
|||
|
|||
๐ฅ ๐ โ ๐ ๐ธ ๐ธ๐ฒ ๐ฐ ([๐ (๐) ๐ฝ](../tutorial/sql-databases.md){.internal-link target=_blank}). |
|||
|
|||
๐
๐ ๐ค ๐. |
|||
|
|||
, ๐ฅ ๐ ๐ฏ ๐ด ๐ ๐บ. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
โก๏ธ ๐ฌ ๐ โ๏ธ ๐ ๐ `my_super_project` ๐ ๐ ๐ง-๐ ๐ค `sql_app` โฎ๏ธ ๐ ๐ ๐: |
|||
|
|||
``` |
|||
. |
|||
โโโ sql_app |
|||
โโโ __init__.py |
|||
โโโ crud.py |
|||
โโโ database.py |
|||
โโโ main.py |
|||
โโโ schemas.py |
|||
``` |
|||
|
|||
๐ ๐ ๐ ๐ ๐ฅ โ๏ธ ๐ธ๐ฒ ๐ฐ. |
|||
|
|||
๐ โก๏ธ ๐ โซ๏ธโ ๐ ๐/๐น ๐จ. |
|||
|
|||
## โ ๐ ๐ |
|||
|
|||
โก๏ธ ๐ ๐ `sql_app/database.py`. |
|||
|
|||
### ๐ฉ ๐ ๐ |
|||
|
|||
โก๏ธ ๐ฅ โ
๐ ๐ ๐ ๐, โ ๐ ๐ฝ: |
|||
|
|||
```Python hl_lines="3 5 22" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/database.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
โ๏ธ ๐คฏ ๐ ๐ฅ ๐ ๐ โ๏ธ ๐ ๐ฝ, ๐ โณ, ๐ ๐ซ ๐ซ ๐ ๐ป. ๐ ๐ ๐ช โ๏ธ ๐ ๐ ๐ฝ ๐. |
|||
|
|||
#### ๐ |
|||
|
|||
โ: |
|||
|
|||
```Python |
|||
check_same_thread=False |
|||
``` |
|||
|
|||
๐ 1๏ธโฃ ๐ธ๐ฒ ๐ฐ: |
|||
|
|||
```Python |
|||
connect_args={"check_same_thread": False} |
|||
``` |
|||
|
|||
...โซ๏ธ ๐ช ๐ด `SQLite`. |
|||
|
|||
!!! info "๐ก โน" |
|||
|
|||
โซ๏ธโ ๐ ๐ก โน [๐ (๐) ๐ฝ](../tutorial/sql-databases.md#note){.internal-link target=_blank} โ. |
|||
|
|||
### โ ๐ ๐-๐ `PeeweeConnectionState` |
|||
|
|||
๐ โ โฎ๏ธ ๐ & FastAPI ๐ ๐ โ๏ธ ๐ ๐ <a href="https://docs.python.org/3/library/threading.html#thread-local-data" class="external-link" target="_blank">๐ `threading.local`</a>, & โซ๏ธ ๐ซ โ๏ธ ๐ฏ ๐ ๐ โซ๏ธ โ๏ธ โก๏ธ ๐ ๐ต ๐/๐ ๐ (๐จ ๐ธ๐ฒ ๐ฐ). |
|||
|
|||
& `threading.local` ๐ซ ๐ โฎ๏ธ ๐ ๐ โ ๐ ๐. |
|||
|
|||
!!! note "๐ก โน" |
|||
`threading.local` โ๏ธ โ๏ธ "๐ฑ" ๐ข ๐ โ๏ธ ๐ ๐ฒ ๐ ๐งต. |
|||
|
|||
๐ โ ๐ ๐ ๏ธ ๐ โ๏ธ 1๏ธโฃ ๐ ๐งต ๐ ๐จ, ๐
โโ ๐, ๐
โโ ๐. |
|||
|
|||
โ๏ธ ๐, ๐ ๐จ ๐ โ๏ธ ๐ฎ ๐ ๐ฝ ๐/๐, โ โ ๐ ๐ฅ
. |
|||
|
|||
โ๏ธ FastAPI, โ๏ธ ๐ ๐ โ, ๐ช ๐ต ๐
๐ 1๏ธโฃ ๐จ ๐ ๐ ๐งต. & ๐ ๐ฐ, ๐ ๐จ, โซ๏ธ ๐ช ๐ ๐ ๐ ๐ ๐งต (๐งต), โ๏ธ ๐ ๐ฅ ๐ โ๏ธ `async def` โ๏ธ ๐ `def`. ๐ โซ๏ธโ ๐ค ๐ ๐ญ ๐ FastAPI. |
|||
|
|||
โ๏ธ ๐ 3๏ธโฃ.7๏ธโฃ & ๐ ๐ ๐ ๐ง ๐ `threading.local`, ๐ ๐ช โ๏ธ ๐ฅ ๐โ `threading.local` ๐ โ๏ธ, โ๏ธ ๐ โฎ๏ธ ๐ ๐ โ. |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐. โซ๏ธ ๐ค <a href="https://docs.python.org/3/library/contextvars.html" class="external-link" target="_blank">`contextvars`</a>. |
|||
|
|||
๐ฅ ๐ ๐ ๐ ๐ ๐ ๐ โ๏ธ `threading.local` & โ ๐ซ โฎ๏ธ `contextvars`, โฎ๏ธ ๐ โน. |
|||
|
|||
๐ 5๏ธโฃ๐ ๐ ๐ ๐ (& โซ๏ธ ๐ค), ๐ ๐ซ ๐ค ๐ช ๐ ๐ค โ โซ๏ธ ๐ท โ๏ธ โซ๏ธ. |
|||
|
|||
๐ฅ ๐ โ `PeeweeConnectionState`: |
|||
|
|||
```Python hl_lines="10-19" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/database.py!} |
|||
``` |
|||
|
|||
๐ ๐ ๐ โช๏ธโก๏ธ ๐ ๐ ๐ โ๏ธ ๐. |
|||
|
|||
โซ๏ธ โ๏ธ ๐ โ โ ๐ โ๏ธ `contextvars` โฉ๏ธ `threading.local`. |
|||
|
|||
`contextvars` ๐ท ๐ ๐ ๐ `threading.local`. โ๏ธ ๐ ๐ ๐ ๐ ๐ค ๐ ๐ ๐ ๐ท โฎ๏ธ `threading.local`. |
|||
|
|||
, ๐ฅ ๐ช โ ๐ฑ โ โซ๏ธ ๐ท ๐ฅ โซ๏ธ โ๏ธ `threading.local`. `__init__`, `__setattr__`, & `__getattr__` ๐ ๏ธ ๐ โ ๐ฑ ๐ โ๏ธ ๐ ๐ต ๐ค ๐ โซ๏ธ ๐ ๐ โฎ๏ธ FastAPI. |
|||
|
|||
!!! tip |
|||
๐ ๐ โ ๐ ๐ญ โ ๐โ โ๏ธ โฎ๏ธ FastAPI. ๐ซ ๐ฒ ๐ โ๏ธ ๐ช ๐ ๐ โ โ๏ธ, ๐ โ, โ๏ธ. |
|||
|
|||
โ๏ธ โซ๏ธ ๐ซ ๐ค ๐ ๐ ๐-๐๏ธ. ๐ ๐ โ๏ธ ๐ `def` ๐ข & ๐ซ `async def`. |
|||
|
|||
### โ๏ธ ๐ `PeeweeConnectionState` ๐ |
|||
|
|||
๐, ๐ `._state` ๐ ๐ข ๐ ๐ฝ `db` ๐ โ๏ธ ๐ `PeeweeConnectionState`: |
|||
|
|||
```Python hl_lines="24" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/database.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
โ ๐ญ ๐ ๐ `db._state` *โฎ๏ธ* ๐ `db`. |
|||
|
|||
!!! tip |
|||
๐ ๐ ๐ ๐ ๐ ๐ ๐ฝ, ๐ `PostgresqlDatabase`, `MySQLDatabase`, โ๏ธ. |
|||
|
|||
## โ ๐ฝ ๐ท |
|||
|
|||
โก๏ธ ๐ ๐ ๐ `sql_app/models.py`. |
|||
|
|||
### โ ๐ ๐ท ๐ ๐ฝ |
|||
|
|||
๐ โ ๐ ๐ท (๐) `User` & `Item`. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ฅ ๐ โฉ ๐ ๐ฐ & โน ๐ท โ๏ธ ๐ ๐ฝ ๐ธ๐ฒ ๐ฐ. |
|||
|
|||
!!! tip |
|||
๐ โ๏ธ โ "**๐ท**" ๐ ๐ ๐ & ๐ ๐ ๐ โฎ๏ธ ๐ฝ. |
|||
|
|||
โ๏ธ Pydantic โ๏ธ โ "**๐ท**" ๐ ๐ณ ๐, ๐ฝ ๐ฌ, ๐ ๏ธ, & ๐งพ ๐ & ๐. |
|||
|
|||
๐ `db` โช๏ธโก๏ธ `database` (๐ `database.py` โช๏ธโก๏ธ ๐) & โ๏ธ โซ๏ธ ๐ฅ. |
|||
|
|||
```Python hl_lines="3 6-12 15-21" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/models.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ โ ๐ ๐ฑ ๐ข. |
|||
|
|||
โซ๏ธ ๐ ๐ ๐ฎ `id` ๐ข ๐ข ๐ ๐. |
|||
|
|||
โซ๏ธ ๐ โ ๐ ๐ โ๏ธ ๐ ๐ ๐. |
|||
|
|||
`Item`, โซ๏ธ ๐ โ ๐ข `owner_id` โฎ๏ธ ๐ข ๐ `User`. โ๏ธ ๐ฅ ๐ซ ๐ฃ โซ๏ธ ๐. |
|||
|
|||
## โ Pydantic ๐ท |
|||
|
|||
๐ โก๏ธ โ
๐ `sql_app/schemas.py`. |
|||
|
|||
!!! tip |
|||
โ ๐จ ๐ ๐ *๐ท* & Pydantic *๐ท*, ๐ฅ ๐ โ๏ธ ๐ `models.py` โฎ๏ธ ๐ ๐ท, & ๐ `schemas.py` โฎ๏ธ Pydantic ๐ท. |
|||
|
|||
๐ซ Pydantic ๐ท ๐ฌ ๐
โ๏ธ ๐ "๐" (โ ๐ ๐ ). |
|||
|
|||
๐ ๐ โน ๐ฅ โ ๐จ โช โ๏ธ ๐ฏโโ๏ธ. |
|||
|
|||
### โ Pydantic *๐ท* / ๐ |
|||
|
|||
โ ๐ ๐ Pydantic ๐ท ๐ธ๐ฒ ๐ฐ: |
|||
|
|||
```Python hl_lines="16-18 21-22 25-30 34-35 38-39 42-48" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ฅ ๐ ๐ท โฎ๏ธ `id`. |
|||
|
|||
๐ฅ ๐ซ ๐ฏ โ `id` ๐ข ๐ ๐ท, โ๏ธ ๐ ๐ฎ 1๏ธโฃ ๐. |
|||
|
|||
๐ฅ โ ๐ฑ `owner_id` ๐ข `Item`. |
|||
|
|||
### โ `PeeweeGetterDict` Pydantic *๐ท* / ๐ |
|||
|
|||
๐โ ๐ ๐ ๐ ๐ ๐, ๐ `some_user.items`, ๐ ๐ซ ๐ `list` `Item`. |
|||
|
|||
โซ๏ธ ๐ ๐ ๐ ๐ ๐ `ModelSelect`. |
|||
|
|||
โซ๏ธ ๐ช โ `list` ๐ฎ ๐ฌ โฎ๏ธ `list(some_user.items)`. |
|||
|
|||
โ๏ธ ๐ โซ๏ธ ๐ซ `list`. & โซ๏ธ ๐ซ โ ๐ <a href="https://docs.python.org/3/glossary.html#term-generator" class="external-link" target="_blank">๐</a>. โฉ๏ธ ๐, Pydantic ๐ซ ๐ญ ๐ข โ ๐ โซ๏ธ `list` Pydantic *๐ท* / ๐. |
|||
|
|||
โ๏ธ โฎ๏ธ โฌ Pydantic โ ๐ ๐ ๐ ๐ ๐ โช๏ธโก๏ธ `pydantic.utils.GetterDict`, ๐ ๐ ๏ธ โ๏ธ ๐โ โ๏ธ `orm_mode = True` ๐ ๐ฒ ๐ ๐ท ๐ข. |
|||
|
|||
๐ฅ ๐ โ ๐ `PeeweeGetterDict` ๐ & โ๏ธ โซ๏ธ ๐ ๐ Pydantic *๐ท* / ๐ ๐ โ๏ธ `orm_mode`: |
|||
|
|||
```Python hl_lines="3 8-13 31 49" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ฅ โ
๐ฅ ๐ข ๐ โ ๐ (โ
`.items` `some_user.items`) ๐ `peewee.ModelSelect`. |
|||
|
|||
& ๐ฅ ๐ ๐ผ, ๐จ `list` โฎ๏ธ โซ๏ธ. |
|||
|
|||
& โคด๏ธ ๐ฅ โ๏ธ โซ๏ธ Pydantic *๐ท* / ๐ ๐ โ๏ธ `orm_mode = True`, โฎ๏ธ ๐ณ ๐ข `getter_dict = PeeweeGetterDict`. |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ด ๐ช โ 1๏ธโฃ `PeeweeGetterDict` ๐, & ๐ฅ ๐ช โ๏ธ โซ๏ธ ๐ Pydantic *๐ท* / ๐. |
|||
|
|||
## ๐ฉ ๐จ๐ป |
|||
|
|||
๐ โก๏ธ ๐ ๐ `sql_app/crud.py`. |
|||
|
|||
### โ ๐ ๐ฉ ๐จ๐ป |
|||
|
|||
โ ๐ ๐ ๐ฉ ๐จ๐ป ๐ธ๐ฒ ๐ฐ, ๐ ๐ ๐ถ ๐: |
|||
|
|||
```Python hl_lines="1 4-5 8-9 12-13 16-20 23-24 27-30" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/crud.py!} |
|||
``` |
|||
|
|||
๐ค ๐บ โฎ๏ธ ๐ ๐ธ๐ฒ ๐ฐ. |
|||
|
|||
๐ฅ ๐ซ ๐ถโโ๏ธ `db` ๐ข ๐คญ. โฉ๏ธ ๐ฅ โ๏ธ ๐ท ๐. ๐ โฉ๏ธ `db` ๐ ๐ ๐, ๐ ๐ ๐ ๐ โ. ๐ โซ๏ธโ ๐ฅ โ๏ธ ๐ `contextvars` โน ๐. |
|||
|
|||
๐, ๐โ ๐ฌ ๐ ๐, ๐ `get_users`, ๐ฅ ๐ ๐ค `list`, ๐: |
|||
|
|||
```Python |
|||
list(models.User.select()) |
|||
``` |
|||
|
|||
๐ ๐ ๐ค ๐ ๐ฅ โ๏ธ โ ๐ `PeeweeGetterDict`. โ๏ธ ๐ฌ ๐ณ ๐ โช `list` โฉ๏ธ `peewee.ModelSelect` `response_model` *โก ๐ ๏ธ* โฎ๏ธ `List[models.User]` (๐ ๐ฅ ๐ ๐ โช) ๐ ๐ท โ. |
|||
|
|||
## ๐ **FastAPI** ๐ฑ |
|||
|
|||
& ๐ ๐ `sql_app/main.py` โก๏ธ ๐ ๏ธ & โ๏ธ ๐ ๐ ๐ ๐ฅ โ โญ. |
|||
|
|||
### โ ๐ฝ ๐ |
|||
|
|||
๐ถ ๐ ๐ โ ๐ฝ ๐: |
|||
|
|||
```Python hl_lines="9-11" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/main.py!} |
|||
``` |
|||
|
|||
### โ ๐ |
|||
|
|||
โ ๐ ๐ ๐ ๐ ๐ฝ โถ๏ธ๏ธ โถ๏ธ ๐จ & ๐ โซ๏ธ ๐: |
|||
|
|||
```Python hl_lines="23-29" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/main.py!} |
|||
``` |
|||
|
|||
๐ฅ ๐ฅ โ๏ธ ๐ `yield` โฉ๏ธ ๐ฅ ๐ค ๐ซ โ๏ธ ๐ฝ ๐ ๐. |
|||
|
|||
โซ๏ธ ๐ ๐ฝ & โป ๐ ๐ฝ ๐ ๐ข ๐ ๐ฌ ๐ ๐จ (โ๏ธ `contextvars` ๐ฑ โช๏ธโก๏ธ ๐). |
|||
|
|||
โฉ๏ธ ๐ฝ ๐ โ ๐ค/๐
พ ๐ง, ๐ ๐ โ โฎ๏ธ ๐ `def` ๐ข. |
|||
|
|||
& โคด๏ธ, ๐ *โก ๐ ๏ธ ๐ข* ๐ ๐ช ๐ ๐ฝ ๐ฅ ๐ฎ โซ๏ธ ๐. |
|||
|
|||
โ๏ธ ๐ฅ ๐ซ โ๏ธ ๐ฒ ๐ ๐ ๐ (โซ๏ธ ๐ค ๐ซ ๐ค ๐ ๐ฒ, โซ๏ธ โ๏ธ ๐ `yield`). , ๐ฅ ๐ซ ๐ฎ โซ๏ธ *โก ๐ ๏ธ ๐ข* โ๏ธ *โก ๐ ๏ธ ๐จโ๐จ* `dependencies` ๐ข: |
|||
|
|||
```Python hl_lines="32 40 47 59 65 72" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/main.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ข ๐ง-๐ |
|||
|
|||
๐ `contextvars` ๐ ๐ท, ๐ฅ ๐ช โ ๐ญ ๐ฅ โ๏ธ ๐ฌ ๐ฒ `ContextVar` ๐ ๐จ ๐ โ๏ธ ๐ฝ, & ๐ ๐ฒ ๐ โ๏ธ ๐ฝ ๐ต๐ธ (๐, ๐ต, โ๏ธ) ๐ ๐จ. |
|||
|
|||
๐, ๐ฅ ๐ช โ โ1๏ธโฃ `async` ๐ `reset_db_state()` ๐ โ๏ธ ๐ง-๐ `get_db()`. โซ๏ธ ๐ โ ๐ฒ ๐ ๐ข (โฎ๏ธ ๐ข `dict`) ๐ ๐ โ๏ธ ๐ฝ ๐ต๐ธ ๐ ๐จ. & โคด๏ธ ๐ `get_db()` ๐ ๐ช โซ๏ธ ๐ฝ ๐ต๐ธ (๐, ๐ต, โ๏ธ). |
|||
|
|||
```Python hl_lines="18-20" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/main.py!} |
|||
``` |
|||
|
|||
**โญ ๐จ**, ๐ฅ ๐ โฒ ๐ ๐ ๐ข ๐ `async` ๐ `reset_db_state()` & โคด๏ธ โ ๐ ๐ `get_db()` ๐, ๐ ๐ ๐จ ๐ โ๏ธ ๐ฎ ๐ ๐ฝ ๐ต๐ธ (๐, ๐ต, โ๏ธ). |
|||
|
|||
!!! tip |
|||
FastAPI ๐ ๐ ๏ธ, 1๏ธโฃ ๐จ ๐ช โถ๏ธ โ ๐ ๏ธ, & โญ ๐, โ1๏ธโฃ ๐จ ๐ช ๐จ & โถ๏ธ ๐ญ ๐, & โซ๏ธ ๐ ๐ช ๐ ๏ธ ๐ ๐งต. |
|||
|
|||
โ๏ธ ๐ ๐ข ๐ค ๐ซ ๐ โ,, ๐ ๐ฝ ๐ต๐ธ โ `async` ๐ `reset_db_state()` ๐ ๐ง ๐ฎ ๐ ๐ฝ ๐ ๐ ๐จ. |
|||
|
|||
& ๐ ๐ฐ, ๐ ๐ ๏ธ ๐จ ๐ โ๏ธ ๐ฎ ๐ ๐ฝ ๐ต๐ธ ๐ ๐ ๐ฌ ๐ ๐จ. |
|||
|
|||
#### ๐ ๐ณ |
|||
|
|||
๐ฅ ๐ โ๏ธ <a href="https://docs.peewee-orm.com/en/latest/peewee/database.html#dynamically-defining-a-database" class="external-link" target="_blank">๐ ๐ณ</a>, โ ๐ฝ `db.obj`. |
|||
|
|||
, ๐ ๐ โฒ โซ๏ธ โฎ๏ธ: |
|||
|
|||
```Python hl_lines="3-4" |
|||
async def reset_db_state(): |
|||
database.db.obj._state._state.set(db_state_default.copy()) |
|||
database.db.obj._state.reset() |
|||
``` |
|||
|
|||
### โ ๐ **FastAPI** *โก ๐ ๏ธ* |
|||
|
|||
๐, ๐, ๐ฅ ๐ฉ **FastAPI** *โก ๐ ๏ธ* ๐. |
|||
|
|||
```Python hl_lines="32-37 40-43 46-53 56-62 65-68 71-79" |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/main.py!} |
|||
``` |
|||
|
|||
### ๐ `def` ๐ `async def` |
|||
|
|||
๐ โฎ๏ธ ๐ธ๐ฒ, ๐ฅ ๐ซ ๐จ ๐ณ ๐: |
|||
|
|||
```Python |
|||
user = await models.User.select().first() |
|||
``` |
|||
|
|||
...โ๏ธ โฉ๏ธ ๐ฅ โ๏ธ: |
|||
|
|||
```Python |
|||
user = models.User.select().first() |
|||
``` |
|||
|
|||
, ๐, ๐ฅ ๐ ๐ฃ *โก ๐ ๏ธ ๐ข* & ๐ ๐ต `async def`, โฎ๏ธ ๐ `def`,: |
|||
|
|||
```Python hl_lines="2" |
|||
# Something goes here |
|||
def read_users(skip: int = 0, limit: int = 100): |
|||
# Something goes here |
|||
``` |
|||
|
|||
## ๐ฌ ๐ โฎ๏ธ ๐ |
|||
|
|||
๐ ๐ผ ๐ โ *โก ๐ ๏ธ* ๐ ๐ฌ ๐ ๐ญ ๐จ โฎ๏ธ `time.sleep(sleep_time)`. |
|||
|
|||
โซ๏ธ ๐ โ๏ธ ๐ฝ ๐ ๐ โถ๏ธ & ๐ โ ๐ฅ โญ ๐ ๐. & ๐ ๐ ๐จ ๐ โ ๐ ๐ฅ ๐. |
|||
|
|||
๐ ๐ ๐ช โก๏ธ ๐ ๐ฏ ๐ ๐ ๐ฑ โฎ๏ธ ๐ & FastAPI ๐ญ โ โฎ๏ธ ๐ ๐ฉ ๐ ๐งต. |
|||
|
|||
๐ฅ ๐ ๐ โ
โ ๐ ๐ ๐ ๐ ๐ฑ ๐ฅ โ๏ธ ๐ต ๐ ๏ธ, ๐ถ `sql_app/database.py` ๐ & ๐ค โธ: |
|||
|
|||
```Python |
|||
# db._state = PeeweeConnectionState() |
|||
``` |
|||
|
|||
& ๐ `sql_app/main.py` ๐, ๐ค ๐ช `async` ๐ `reset_db_state()` & โ โซ๏ธ โฎ๏ธ `pass`: |
|||
|
|||
```Python |
|||
async def reset_db_state(): |
|||
# database.db._state._state.set(db_state_default.copy()) |
|||
# database.db._state.reset() |
|||
pass |
|||
``` |
|||
|
|||
โคด๏ธ ๐ ๐ ๐ฑ โฎ๏ธ Uvicorn: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn sql_app.main:app --reload |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ฅ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a> & โ ๐ฉโโคโ๐จ ๐ฉโ๐ป. |
|||
|
|||
โคด๏ธ ๐ 1๏ธโฃ0๏ธโฃ ๐ <a href="http://127.0.0.1:8000/docs#/default/read_slow_users_slowusers__get" class="external-link" target="_blank">http://127.0.0.1:8000/docs#/default/read_๐_๐ฉโ๐ป_slowusers_ = </a> ๐ ๐ฐ. |
|||
|
|||
๐ถ *โก ๐ ๏ธ* "๐ค `/slowusers/`" ๐ ๐. โ๏ธ "๐ โซ๏ธ ๐
" ๐ผ & ๐ ๏ธ ๐จ ๐ ๐, 1๏ธโฃ โถ๏ธ๏ธ โฎ๏ธ ๐. |
|||
|
|||
๐ ๐ โ ๐ & โคด๏ธ ๐ซ ๐ ๐ฆ `Internal Server Error`. |
|||
|
|||
### โซ๏ธโ ๐จ |
|||
|
|||
๐ฅ ๐ ๐ โ ๐ ๐ฑ โ ๐ ๐ฝ & โ ๐ฅ โญ ๐ ๐ & ๐ช ๐ฝ ๐. |
|||
|
|||
โคด๏ธ, ๐จ โญ ๐, ๐ ๐ฑ ๐ โ ๐ ๐ฅ ๐, & ๐. |
|||
|
|||
๐ โ ๐ โซ๏ธ ๐ ๐ ๐ ๐ ๐ ๐' ๐จ โช ๐ โฎ๏ธ ๐. |
|||
|
|||
โคด๏ธ 1๏ธโฃ ๐ ๐จ ๐ โ ๐ ๐ฅ ๐ ๐ ๐ ๐ฝ ๐, โ๏ธ 1๏ธโฃ ๐ โฎ๏ธ ๐จ ๐ ๐ ๐ ๐ฒ ๐ต ๐ ๐งต ๐ฅ ๐, โซ๏ธ ๐ โ๏ธ ๐ ๐ฝ ๐ ๐ โช ๐, & ๐ ๐ ๐ฎ โ & ๐ ๐ ๐ โซ๏ธ ๐ถ, & ๐จ ๐ โ๏ธ `Internal Server Error`. |
|||
|
|||
๐ ๐ ๐ฒ ๐จ ๐
๐ 1๏ธโฃ ๐ ๐. |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ ๐ฉโ๐ป ๐ฌ ๐ ๐ฑ โซ๏ธโ ๐ ๐ฐ, ๐ โซ๏ธโ ๐ช ๐จ. |
|||
|
|||
& ๐ ๐ฑ โถ๏ธ ๐ต ๐
& ๐ ๐ฉโ๐ป ๐ ๐ฐ, โ ๐ฐ ๐ ๐จ ๐ช ๐ & ๐ โฒ โ. |
|||
|
|||
### ๐ง ๐ โฎ๏ธ FastAPI |
|||
|
|||
๐ ๐ถ ๐ ๐ `sql_app/database.py`, & โ โธ: |
|||
|
|||
```Python |
|||
db._state = PeeweeConnectionState() |
|||
``` |
|||
|
|||
& ๐ `sql_app/main.py` ๐, โ ๐ช `async` ๐ `reset_db_state()`: |
|||
|
|||
```Python |
|||
async def reset_db_state(): |
|||
database.db._state._state.set(db_state_default.copy()) |
|||
database.db._state.reset() |
|||
``` |
|||
|
|||
โ ๐ ๐โโ ๐ฑ & โถ๏ธ โซ๏ธ ๐. |
|||
|
|||
๐ ๐ ๐ ๏ธ โฎ๏ธ 1๏ธโฃ0๏ธโฃ ๐. ๐ ๐ฐ ๐ ๐ซ ๐ โ & ๐ ๐ ๐ค ๐ ๐ ๐ต โ. |
|||
|
|||
...๐ ๐ง โซ๏ธ โ |
|||
|
|||
## ๐ ๐ ๐ |
|||
|
|||
๐ญ ๐ ๐ โ๏ธ ๐ ๐ `my_super_project` (โ๏ธ ๐ ๐ ๐) ๐ ๐ ๐ง-๐ ๐ค `sql_app`. |
|||
|
|||
`sql_app` ๐ โ๏ธ ๐ ๐: |
|||
|
|||
* `sql_app/__init__.py`: ๐ ๐. |
|||
|
|||
* `sql_app/database.py`: |
|||
|
|||
```Python |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/database.py!} |
|||
``` |
|||
|
|||
* `sql_app/models.py`: |
|||
|
|||
```Python |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/models.py!} |
|||
``` |
|||
|
|||
* `sql_app/schemas.py`: |
|||
|
|||
```Python |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!} |
|||
``` |
|||
|
|||
* `sql_app/crud.py`: |
|||
|
|||
```Python |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/crud.py!} |
|||
``` |
|||
|
|||
* `sql_app/main.py`: |
|||
|
|||
```Python |
|||
{!../../../docs_src/sql_databases_peewee/sql_app/main.py!} |
|||
``` |
|||
|
|||
## ๐ก โน |
|||
|
|||
!!! warning |
|||
๐ ๐ถ ๐ก โน ๐ ๐ ๐ฒ ๐ซ ๐ช. |
|||
|
|||
### โ |
|||
|
|||
๐ โ๏ธ <a href="https://docs.python.org/3/library/threading.html#thread-local-data" class="external-link" target="_blank">`threading.local`</a> ๐ข ๐ช โซ๏ธ ๐ฝ "๐ต๐ธ" ๐ฝ (๐, ๐ต, โ๏ธ). |
|||
|
|||
`threading.local` โ ๐ฒ ๐ โฎ๏ธ ๐งต, โ๏ธ ๐ ๐ ๏ธ ๐ ๐ ๐ ๐ (โ
๐ ๐จ) ๐ ๐งต, & ๐ฒ ๐ซ โ. |
|||
|
|||
๐ ๐ ๐, ๐ ๐ ๏ธ ๐ช ๐ ๐ ๐ ๐งต (โ๏ธ `asyncio.run_in_executor`), โ๏ธ ๐ ๐ ๐จ. |
|||
|
|||
๐ โ ๐, โฎ๏ธ ๐ โฎ๏ธ ๐ ๏ธ, ๐ ๐ ๐ช โ๏ธ ๐ `threading.local` ๐ข & ๐ ๐ ๐ค ๐ ๐ & ๐ฝ (๐ ๐ซ ๐ซ๐ ๐ซ), & ๐ ๐ฐ, ๐ฅ ๐ซ ๐ ๏ธ ๐ ๐ค/๐
พ-๐ง ๐ ๐งต (โฎ๏ธ ๐ `def` ๐ข FastAPI, *โก ๐ ๏ธ* & ๐), ๐ ๐ ๐ ๐ซ โ๏ธ ๐ ๐ฝ ๐ต๐ธ ๐ข, โช โซ๏ธ ๐ ๐ ๐จ & โซ๏ธ ๐ ๐ช ๐ค ๐ ๐ ๐ฝ ๐ต๐ธ. |
|||
|
|||
### ๐ ๐ข |
|||
|
|||
๐ 3๏ธโฃ.7๏ธโฃ โ๏ธ <a href="https://docs.python.org/3/library/contextvars.html" class="external-link" target="_blank">`contextvars`</a> ๐ ๐ช โ ๐ง๐ฟ ๐ข ๐ถ ๐ `threading.local`, โ๏ธ ๐ ๐ซ ๐ โ. |
|||
|
|||
๐ค ๐ ๐ โ๏ธ ๐คฏ. |
|||
|
|||
`ContextVar` โ๏ธ โ ๐ ๐น, ๐: |
|||
|
|||
```Python |
|||
some_var = ContextVar("some_var", default="default value") |
|||
``` |
|||
|
|||
โ ๐ฒ โ๏ธ โฎ๏ธ "๐" (โ
โฎ๏ธ ๐จ) โ๏ธ: |
|||
|
|||
```Python |
|||
some_var.set("new value") |
|||
``` |
|||
|
|||
๐ค ๐ฒ ๐ ๐ ๐ (โ
๐ ๐ ๐ โฎ๏ธ ๐จ) โ๏ธ: |
|||
|
|||
```Python |
|||
some_var.get() |
|||
``` |
|||
|
|||
### โ ๐ ๐ข `async` ๐ `reset_db_state()` |
|||
|
|||
๐ฅ ๐ ๐ ๐ โ ๐ฒ โฎ๏ธ `some_var.set("updated in function")` (โ
๐ `async` ๐), ๐ ๐ โซ๏ธ & ๐ ๐ ๐ถ โฎ๏ธ (โ
๐ ๐ `async` ๐ข ๐ค โฎ๏ธ `await`) ๐ ๐ ๐ ๐ ๐ฒ. |
|||
|
|||
, ๐ ๐ผ, ๐ฅ ๐ฅ โ ๐ ๐ต๐ธ ๐ข (โฎ๏ธ ๐ข `dict`) `async` ๐, ๐ ๐ ๐ ๐ ๐ ๐ฑ ๐ ๐ ๐ ๐ฒ & ๐ ๐ช โป โซ๏ธ ๐ ๐จ. |
|||
|
|||
& ๐ ๐ข ๐ โ ๐ โญ ๐จ, ๐ฅ ๐ซ ๐ ๏ธ. |
|||
|
|||
### โ ๐ฝ ๐ต๐ธ ๐ `get_db()` |
|||
|
|||
`get_db()` ๐ `def` ๐ข, **FastAPI** ๐ โ โซ๏ธ ๐ ๐งต, โฎ๏ธ *๐* "๐", ๐งโ๐คโ๐ง ๐ ๐ฒ ๐ ๐ข ( `dict` โฎ๏ธ โฒ ๐ฝ ๐ต๐ธ). โคด๏ธ โซ๏ธ ๐ช ๐ฎ ๐ฝ ๐ต๐ธ ๐ `dict`, ๐ ๐, โ๏ธ. |
|||
|
|||
โ๏ธ ๐ฅ ๐ฒ ๐ ๐ข (๐ข `dict`) โ ๐ ๐ `def` ๐ข, โซ๏ธ ๐ โ ๐ ๐ฒ ๐ ๐ ๐ง ๐ด ๐ ๐งต ๐งต, & ๐ ๐ (๐ *โก ๐ ๏ธ ๐ข*) ๐ซ๐ โ๏ธ ๐ โซ๏ธ. `get_db()` ๐ฅ ๐ช ๐ด โ ๐ฒ `dict`, โ๏ธ ๐ซ ๐ `dict` โซ๏ธ. |
|||
|
|||
, ๐ฅ ๐ช โ๏ธ `async` ๐ `reset_db_state()` โ `dict` ๐ ๐ข. ๐ ๐, ๐ ๐ โ๏ธ ๐ ๐ `dict` ๐ฝ ๐ต๐ธ ๐ ๐จ. |
|||
|
|||
### ๐ & ๐ ๐ `get_db()` |
|||
|
|||
โคด๏ธ โญ โ ๐, โซ๏ธโ ๐ซ ๐ & ๐ ๐ฝ `async` ๐ โซ๏ธ, โฉ๏ธ `get_db()`โ |
|||
|
|||
`async` ๐ โ๏ธ `async` ๐ ๐ข ๐ก ๐ ๐จ, โ๏ธ ๐ & ๐ช ๐ฝ ๐ โ ๐ง, โซ๏ธ ๐ช ๐ ๐ญ ๐ฅ โซ๏ธ ๐ค. |
|||
|
|||
๐ฅ ๐ช ๐ `def` ๐ `get_db()`. |
@ -0,0 +1,469 @@ |
|||
<p align="center"> |
|||
<a href="https://fastapi.tiangolo.com"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" alt="FastAPI"></a> |
|||
</p> |
|||
<p align="center"> |
|||
<em>FastAPI ๐ ๏ธ, โ ๐ญ, โฉ ๐ก, โฉ ๐, ๐ ๐ญ</em> |
|||
</p> |
|||
<p align="center"> |
|||
<a href="https://github.com/tiangolo/fastapi/actions?query=workflow%3ATest+event%3Apush+branch%3Amaster" target="_blank"> |
|||
<img src="https://github.com/tiangolo/fastapi/workflows/Test/badge.svg?event=push&branch=master" alt="Test"> |
|||
</a> |
|||
<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/tiangolo/fastapi" target="_blank"> |
|||
<img src="https://coverage-badge.samuelcolvin.workers.dev/tiangolo/fastapi.svg" alt="Coverage"> |
|||
</a> |
|||
<a href="https://pypi.org/project/fastapi" target="_blank"> |
|||
<img src="https://img.shields.io/pypi/v/fastapi?color=%2334D058&label=pypi%20package" alt="Package version"> |
|||
</a> |
|||
<a href="https://pypi.org/project/fastapi" target="_blank"> |
|||
<img src="https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058" alt="Supported Python versions"> |
|||
</a> |
|||
</p> |
|||
|
|||
--- |
|||
|
|||
**๐งพ**: <a href="https://fastapi.tiangolo.com" target="_blank">https://fastapi.tiangolo.com</a> |
|||
|
|||
**โน ๐**: <a href="https://github.com/tiangolo/fastapi" target="_blank">https://github.com/tiangolo/fastapi</a> |
|||
|
|||
--- |
|||
|
|||
FastAPI ๐, โฉ (โ-๐ญ), ๐ธ ๐ ๏ธ ๐ ๐ ๏ธ โฎ๏ธ ๐ 3๏ธโฃ.7๏ธโฃ โ โ๏ธ ๐ ๐ฉ ๐ ๐ ๐. |
|||
|
|||
๐ โ: |
|||
|
|||
* **โฉ**: ๐ถ โ ๐ญ, ๐ ๐ท๐ช โฎ๏ธ **โณ** & **๐ถ** (๐ ๐ & Pydantic). [1๏ธโฃ โฉ ๐ ๐ ๏ธ ๐ช](#performance). |
|||
* **โฉ ๐**: ๐ ๐
๐ ๏ธ โ ๐ 2๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ 3๏ธโฃ0๏ธโฃ0๏ธโฃ ๐ฏ. * |
|||
* **๐ฉโโคโ๐จ ๐**: ๐ ๐ 4๏ธโฃ0๏ธโฃ ๐ฏ ๐ฟ (๐ฉโ๐ป) ๐ โ. * |
|||
* **๐๏ธ**: ๐ ๐จโ๐จ ๐โ๐ฆบ. <abbr title="also known as auto-complete, autocompletion, IntelliSense">๐ ๏ธ</abbr> ๐. ๐ ๐ฐ ๐ ๏ธ. |
|||
* **โฉ**: ๐ง โฉ โ๏ธ & ๐ก. ๐ ๐ฐ ๐ ๐ฉบ. |
|||
* **๐**: ๐ ๐ โ. ๐ โ โช๏ธโก๏ธ ๐ ๐ข ๐. ๐ฉโโคโ๐จ ๐. |
|||
* **๐๏ธ**: ๐ค ๐ญ-๐ ๐. โฎ๏ธ ๐ง ๐ ๐งพ. |
|||
* **๐ฉ-โ๏ธ**: โ๏ธ ๐ (& ๐ ๐ โฎ๏ธ) ๐ ๐ฉ ๐: <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank">๐</a> (โช ๐ญ ๐ฆ) & <a href="https://json-schema.org/" class="external-link" target="_blank">๐ป ๐</a>. |
|||
|
|||
<small>* โ โ๏ธ ๐ ๐ฏ ๐ ๐ ๐ ๏ธ ๐, ๐ ๐ญ ๐ธ.</small> |
|||
|
|||
## ๐ฐ |
|||
|
|||
<!-- sponsors --> |
|||
|
|||
{% if sponsors %} |
|||
{% for sponsor in sponsors.gold -%} |
|||
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> |
|||
{% endfor -%} |
|||
{%- for sponsor in sponsors.silver -%} |
|||
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> |
|||
{% endfor %} |
|||
{% endif %} |
|||
|
|||
<!-- /sponsors --> |
|||
|
|||
<a href="https://fastapi.tiangolo.com/fastapi-people/#sponsors" class="external-link" target="_blank">๐ ๐ฐ</a> |
|||
|
|||
## ๐ค |
|||
|
|||
"_[...] ๐ค โ๏ธ **FastAPI** ๐ ๐ซ ๐. [...] ๐ค ๐ค ๐ โ๏ธ โซ๏ธ ๐ ๐ ๐ **โ ๐โ๐ฆบ ๐คธโโ**. ๐ซ ๐โโ ๐ ๏ธ ๐ ๐ **๐ฅ** ๐ฌ & **๐ ** ๐ฌ._" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">๐งฟ ๐ต๐ฐ - <strong>๐คธโโ</strong> <a href="https://github.com/tiangolo/fastapi/pull/26" target="_blank"><small>(๐ฆ๐ช)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_๐ฅ ๐ ๏ธ **FastAPI** ๐ ๐ค **๐** ๐ฝ ๐ ๐ช ๐ข ๐ **๐ฎ**. [๐จ๐]_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">๐ฎ๐น ๐ธ๐ป, ๐จ๐ ๐จ๐, & ๐ฑ๐ฐ ๐ ๐ - <strong>๐</strong> <a href="https://eng.uber.com/ludwig-v0-2/" target="_blank"><small>(๐ฆ๐ช)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_**๐บ** ๐ ๐ฃ ๐-โน ๐ ๐ **โ ๐งพ** ๐ถ ๐ ๏ธ: **๐จ**โ [๐ โฎ๏ธ **FastAPI**]_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">โก ๐, ๐ ๐ช๐ธ, ๐ฒ ๐ - <strong>๐บ</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072" target="_blank"><small>(๐ฆ๐ช)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_๐ค ๐คญ ๐ ๐ ๐ **FastAPI**. โซ๏ธ ๐ โ_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">โก ๐ญ๐ฐ - <strong><a href="https://pythonbytes.fm/episodes/show/123/time-to-right-the-py-wrongs?time_in_sec=855" target="_blank">๐ ๐ข</a> ๐ป ๐ฆ </strong> <a href="https://twitter.com/brianokken/status/1112220079972728832" target="_blank"><small>(๐ฆ๐ช)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_๐ค, โซ๏ธโ ๐ โ๏ธ ๐ ๐ ๐ ๐ & ๐ต๐ฑ. ๐ ๐, โซ๏ธ โซ๏ธโ ๐ค ๐ **๐ค** - โซ๏ธ ๐ค ๐ ๐ ๐ฑ ๐ ๐._" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">โก ๐ - <strong><a href="https://www.hug.rest/" target="_blank">๐ค</a> ๐ผ</strong> <a href="https://news.ycombinator.com/item?id=19455465" target="_blank"><small>(๐ฆ๐ช)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_๐ฅ ๐ ๐ ๐ก 1๏ธโฃ **๐ ๐ ๏ธ** ๐ ๐ ๐, โ
๐
**FastAPI** [...] โซ๏ธ โฉ, โฉ โ๏ธ & โฉ ๐ก [...]_" |
|||
|
|||
"_๐ฅ โ๏ธ ๐ ๐คญ **FastAPI** ๐ **๐** [...] ๐ค ๐ญ ๐ ๐ ๐ โซ๏ธ [...]_" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">๐ฑ๐จ ๐ธ๐ฒ - โก Honnibal - <strong><a href="https://explosion.ai" target="_blank">๐ฅ ๐ฒ</a> ๐ด - <a href="https://spacy.io" target="_blank">๐</a> ๐ผ</strong> <a href="https://twitter.com/_inesmontani/status/1144173225322143744" target="_blank"><small>(๐ฆ๐ช)</small></a> - <a href="https://twitter.com/honnibal/status/1144031421859655680" target="_blank"><small>(๐ฆ๐ช)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
"_๐ฅ ๐ ๐ ๐ ๐ญ ๐ ๐ ๏ธ, ๐ค ๐ ๐ ๐ **FastAPI**. โซ๏ธ **๐ ๐**, **๐
โ๏ธ** & **๐ ๐ ๏ธ**, โซ๏ธ โ๏ธ โถ๏ธ๏ธ **๐ ๐ฆฒ** ๐ ๐ ๏ธ ๐ฅ ๐ ๏ธ ๐ & ๐ ๐ ๐ง & ๐โ๐ฆบ โ
๐ ๐น ๐ซ ๐จโ๐ป._" |
|||
|
|||
<div style="text-align: right; margin-right: 10%;">๐น๐ฆ ๐ฐ - <strong>๐ป</strong> <a href="https://www.linkedin.com/posts/deonpillsbury_cisco-cx-python-activity-6963242628536487936-trAp/" target="_blank"><small>(๐ฆ๐ช)</small></a></div> |
|||
|
|||
--- |
|||
|
|||
## **๐**, FastAPI ๐ณ๐จ |
|||
|
|||
<a href="https://typer.tiangolo.com" target="_blank"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" style="width: 20%;"></a> |
|||
|
|||
๐ฅ ๐ ๐ <abbr title="Command Line Interface">โณ</abbr> ๐ฑ โ๏ธ ๐ถ โฉ๏ธ ๐ธ ๐ ๏ธ, โ
๐
<a href="https://typer.tiangolo.com/" class="external-link" target="_blank">**๐**</a>. |
|||
|
|||
**๐** FastAPI ๐ฅ ๐ช. & โซ๏ธ ๐ฏ **FastAPI ๐ณ๐จ**. ๐ถ ๐ถ ๐ถ |
|||
|
|||
## ๐ |
|||
|
|||
๐ 3๏ธโฃ.7๏ธโฃ โ |
|||
|
|||
FastAPI ๐ง ๐ โ ๐: |
|||
|
|||
* <a href="https://www.starlette.io/" class="external-link" target="_blank">๐</a> ๐ธ ๐. |
|||
* <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> ๐ ๐. |
|||
|
|||
## ๐ทโโ |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install fastapi |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
๐ ๐ ๐ช ๐ซ ๐ฝ, ๐ญ โ
<a href="https://www.uvicorn.org" class="external-link" target="_blank">Uvicorn</a> โ๏ธ <a href="https://github.com/pgjones/hypercorn" class="external-link" target="_blank">Hypercorn</a>. |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ pip install "uvicorn[standard]" |
|||
|
|||
---> 100% |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
## ๐ผ |
|||
|
|||
### โ โซ๏ธ |
|||
|
|||
* โ ๐ `main.py` โฎ๏ธ: |
|||
|
|||
```Python |
|||
from typing import Union |
|||
|
|||
from fastapi import FastAPI |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.get("/") |
|||
def read_root(): |
|||
return {"Hello": "World"} |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
def read_item(item_id: int, q: Union[str, None] = None): |
|||
return {"item_id": item_id, "q": q} |
|||
``` |
|||
|
|||
<details markdown="1"> |
|||
<summary>โ๏ธ โ๏ธ <code>async def</code>...</summary> |
|||
|
|||
๐ฅ ๐ ๐ โ๏ธ `async` / `await`, โ๏ธ `async def`: |
|||
|
|||
```Python hl_lines="9 14" |
|||
from typing import Union |
|||
|
|||
from fastapi import FastAPI |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
@app.get("/") |
|||
async def read_root(): |
|||
return {"Hello": "World"} |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
async def read_item(item_id: int, q: Union[str, None] = None): |
|||
return {"item_id": item_id, "q": q} |
|||
``` |
|||
|
|||
**๐**: |
|||
|
|||
๐ฅ ๐ ๐ซ ๐ญ, โ
_"๐ โ" _ ๐ ๐ <a href="https://fastapi.tiangolo.com/async/#in-a-hurry" target="_blank">`async` & `await` ๐ฉบ</a>. |
|||
|
|||
</details> |
|||
|
|||
### ๐ โซ๏ธ |
|||
|
|||
๐ ๐ฝ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn main:app --reload |
|||
|
|||
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
INFO: Started reloader process [28720] |
|||
INFO: Started server process [28722] |
|||
INFO: Waiting for application startup. |
|||
INFO: Application startup complete. |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
<details markdown="1"> |
|||
<summary>๐ ๐ <code>uvicorn main:app --reload</code>...</summary> |
|||
|
|||
๐ `uvicorn main:app` ๐: |
|||
|
|||
* `main`: ๐ `main.py` (๐ "๐น"). |
|||
* `app`: ๐ โ ๐ `main.py` โฎ๏ธ โธ `app = FastAPI()`. |
|||
* `--reload`: โ ๐ฝ โ โฎ๏ธ ๐ ๐. ๐ด ๐ ๐ ๏ธ. |
|||
|
|||
</details> |
|||
|
|||
### โ
โซ๏ธ |
|||
|
|||
๐ ๐ ๐ฅ <a href="http://127.0.0.1:8000/items/5?q=somequery" class="external-link" target="_blank">http://127.0.0.1:8000/items/5?q=somequery</a>. |
|||
|
|||
๐ ๐ ๐ ๐ป ๐จ: |
|||
|
|||
```JSON |
|||
{"item_id": 5, "q": "somequery"} |
|||
``` |
|||
|
|||
๐ โช โ ๐ ๏ธ ๐: |
|||
|
|||
* ๐จ ๐บ๐ธ๐ ๐จ _โก_ `/` & `/items/{item_id}`. |
|||
* ๐ฏโโ๏ธ _โก_ โ `GET` <em>๐ ๏ธ</em> (๐ญ ๐บ๐ธ๐ _๐ฉโ๐ฌ_). |
|||
* _โก_ `/items/{item_id}` โ๏ธ _โก ๐ข_ `item_id` ๐ ๐ `int`. |
|||
* _โก_ `/items/{item_id}` โ๏ธ ๐ฆ `str` _๐ข = `q`. |
|||
|
|||
### ๐ ๐ ๏ธ ๐ฉบ |
|||
|
|||
๐ ๐ถ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
๐ ๐ ๐ ๐ง ๐ ๐ ๏ธ ๐งพ (๐ <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">๐ฆ ๐</a>): |
|||
|
|||
 |
|||
|
|||
### ๐ ๐ ๏ธ ๐ฉบ |
|||
|
|||
& ๐, ๐ถ <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ง ๐งพ (๐ <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">๐</a>): |
|||
|
|||
 |
|||
|
|||
## ๐ผ โป |
|||
|
|||
๐ ๐ ๐ `main.py` ๐จ ๐ช โช๏ธโก๏ธ `PUT` ๐จ. |
|||
|
|||
๐ฃ ๐ช โ๏ธ ๐ฉ ๐ ๐, ๐ Pydantic. |
|||
|
|||
```Python hl_lines="4 9-12 25-27" |
|||
from typing import Union |
|||
|
|||
from fastapi import FastAPI |
|||
from pydantic import BaseModel |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
class Item(BaseModel): |
|||
name: str |
|||
price: float |
|||
is_offer: Union[bool, None] = None |
|||
|
|||
|
|||
@app.get("/") |
|||
def read_root(): |
|||
return {"Hello": "World"} |
|||
|
|||
|
|||
@app.get("/items/{item_id}") |
|||
def read_item(item_id: int, q: Union[str, None] = None): |
|||
return {"item_id": item_id, "q": q} |
|||
|
|||
|
|||
@app.put("/items/{item_id}") |
|||
def update_item(item_id: int, item: Item): |
|||
return {"item_name": item.name, "item_id": item_id} |
|||
``` |
|||
|
|||
๐ฝ ๐ ๐ ๐ (โฉ๏ธ ๐ ๐ฎ `--reload` `uvicorn` ๐ ๐). |
|||
|
|||
### ๐ ๐ ๏ธ ๐ฉบ โป |
|||
|
|||
๐ ๐ถ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
* ๐ ๐ ๏ธ ๐งพ ๐ ๐ โน, ๐ ๐ ๐ช: |
|||
|
|||
 |
|||
|
|||
* ๐ ๐ ๐ผ "๐ โซ๏ธ ๐
", โซ๏ธ โ ๐ ๐ฅง ๐ข & ๐ ๐ โฎ๏ธ ๐ ๏ธ: |
|||
|
|||
 |
|||
|
|||
* โคด๏ธ ๐ ๐ "๐ ๏ธ" ๐ผ, ๐ฉโ๐ป ๐ข ๐ ๐ โฎ๏ธ ๐ ๐ ๏ธ, ๐จ ๐ข, ๐ค ๐ & ๐ฆ ๐ซ ๐ ๐ฅ: |
|||
|
|||
 |
|||
|
|||
### ๐ ๐ ๏ธ ๐ฉบ โป |
|||
|
|||
& ๐, ๐ถ <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>. |
|||
|
|||
* ๐ ๐งพ ๐ ๐จ ๐ ๐ข ๐ข & ๐ช: |
|||
|
|||
 |
|||
|
|||
### ๐ |
|||
|
|||
๐, ๐ ๐ฃ **๐** ๐ ๐ข, ๐ช, โ๏ธ. ๐ข ๐ข. |
|||
|
|||
๐ ๐ โฎ๏ธ ๐ฉ ๐ ๐ ๐. |
|||
|
|||
๐ ๐ซ โ๏ธ ๐ก ๐ โ, ๐ฉโ๐ฌ โ๏ธ ๐ ๐ฏ ๐, โ๏ธ. |
|||
|
|||
๐ฉ **๐ 3๏ธโฃ.7๏ธโฃ โ**. |
|||
|
|||
๐ผ, `int`: |
|||
|
|||
```Python |
|||
item_id: int |
|||
``` |
|||
|
|||
โ๏ธ ๐ ๐ `Item` ๐ท: |
|||
|
|||
```Python |
|||
item: Item |
|||
``` |
|||
|
|||
...& โฎ๏ธ ๐ ๐ ๐ ๐ ๐ค: |
|||
|
|||
* ๐จโ๐จ ๐โ๐ฆบ, ๐: |
|||
* ๐ ๏ธ. |
|||
* ๐ โ
. |
|||
* ๐ฌ ๐ฝ: |
|||
* ๐ง & ๐ โ ๐โ ๐ โ. |
|||
* ๐ฌ ๐ ๐ฆ ๐ป ๐. |
|||
* <abbr title="also known as: serialization, parsing, marshalling">๐ ๏ธ</abbr> ๐ข ๐ฝ: ๐ โช๏ธโก๏ธ ๐ธ ๐ ๐ฝ & ๐. ๐ โช๏ธโก๏ธ: |
|||
* ๐ป. |
|||
* โก ๐ข. |
|||
* ๐ข ๐ข. |
|||
* ๐ช. |
|||
* ๐. |
|||
* ๐จ. |
|||
* ๐. |
|||
* <abbr title="also known as: serialization, parsing, marshalling">๐ ๏ธ</abbr> ๐ข ๐: ๐ โช๏ธโก๏ธ ๐ ๐ฝ & ๐ ๐ธ ๐ฝ (๐ป): |
|||
* ๐ ๐ ๐ (`str`, `int`, `float`, `bool`, `list`, โ๏ธ). |
|||
* `datetime` ๐. |
|||
* `UUID` ๐. |
|||
* ๐ฝ ๐ท. |
|||
* ...& ๐ ๐. |
|||
* ๐ง ๐ ๐ ๏ธ ๐งพ, ๐ 2๏ธโฃ ๐ ๐ฉโ๐ป ๐ข: |
|||
* ๐ฆ ๐. |
|||
* ๐. |
|||
|
|||
--- |
|||
|
|||
๐ ๐ โฎ๏ธ ๐ ๐ผ, **FastAPI** ๐: |
|||
|
|||
* โ ๐ ๐ค `item_id` โก `GET` & `PUT` ๐จ. |
|||
* โ ๐ `item_id` ๐ `int` `GET` & `PUT` ๐จ. |
|||
* ๐ฅ โซ๏ธ ๐ซ, ๐ฉโ๐ป ๐ ๐ โ , ๐ โ. |
|||
* โ
๐ฅ ๐ค ๐ฆ ๐ข ๐ข ๐ `q` ( `http://127.0.0.1:8000/items/foo?q=somequery`) `GET` ๐จ. |
|||
* `q` ๐ข ๐ฃ โฎ๏ธ `= None`, โซ๏ธ ๐ฆ. |
|||
* ๐ต `None` โซ๏ธ ๐ ๐ (๐ช ๐ผ โฎ๏ธ `PUT`). |
|||
* `PUT` ๐จ `/items/{item_id}`, โ ๐ช ๐ป: |
|||
* โ
๐ โซ๏ธ โ๏ธ โ ๐ข `name` ๐ ๐ `str`. |
|||
* โ
๐ โซ๏ธ โ๏ธ โ ๐ข `price` ๐ โ๏ธ `float`. |
|||
* โ
๐ โซ๏ธ โ๏ธ ๐ฆ ๐ข `is_offer`, ๐ ๐ `bool`, ๐ฅ ๐. |
|||
* ๐ ๐ ๐ ๐ท ๐ ๐ฆ ๐ป ๐. |
|||
* ๐ โช๏ธโก๏ธ & ๐ป ๐. |
|||
* ๐ ๐ โฎ๏ธ ๐, ๐ ๐ช โ๏ธ: |
|||
* ๐ ๐งพ โ๏ธ. |
|||
* ๐ง ๐ฉโ๐ป ๐ โก โ๏ธ, ๐ ๐ช๐ธ. |
|||
* ๐ 2๏ธโฃ ๐ ๐งพ ๐ธ ๐ข ๐. |
|||
|
|||
--- |
|||
|
|||
๐ฅ ๐ ๐งฝ, โ๏ธ ๐ โช ๐ค ๐ญ โ โซ๏ธ ๐ ๐ท. |
|||
|
|||
๐ ๐ โธ โฎ๏ธ: |
|||
|
|||
```Python |
|||
return {"item_name": item.name, "item_id": item_id} |
|||
``` |
|||
|
|||
...โช๏ธโก๏ธ: |
|||
|
|||
```Python |
|||
... "item_name": item.name ... |
|||
``` |
|||
|
|||
...: |
|||
|
|||
```Python |
|||
... "item_price": item.price ... |
|||
``` |
|||
|
|||
...& ๐ โ ๐ ๐จโ๐จ ๐ ๐-๐ ๐ข & ๐ญ ๐ซ ๐: |
|||
|
|||
 |
|||
|
|||
๐
๐ ๐ผ ๐ ๐
โ, ๐ <a href="https://fastapi.tiangolo.com/tutorial/">๐ฐ - ๐ฉโ๐ป ๐ฆฎ</a>. |
|||
|
|||
**๐ ๐จ**: ๐ฐ - ๐ฉโ๐ป ๐ฆฎ ๐: |
|||
|
|||
* ๐ **๐ข** โช๏ธโก๏ธ ๐ ๐ ๐ฅ: **๐**, **๐ช**, **๐จ ๐** & **๐**. |
|||
* โ โ **๐ฌ โ** `maximum_length` โ๏ธ `regex`. |
|||
* ๐ถ ๐๏ธ & โฉ โ๏ธ **<abbr title="also known as components, resources, providers, services, injectables">๐ ๐</abbr>** โ๏ธ. |
|||
* ๐โโ & ๐ค, โ
๐โ๐ฆบ **Oauth2๏ธโฃ** โฎ๏ธ **๐ฅ ๐ค** & **๐บ๐ธ๐ ๐ฐ** ๐. |
|||
* ๐
๐ง (โ๏ธ ๐จ โฉ) โ ๐ฃ **๐ ๐ฆ ๐ป ๐ท** (๐ Pydantic). |
|||
* **๐น** ๐ ๏ธ โฎ๏ธ <a href="https://strawberry.rocks" class="external-link" target="_blank">๐</a> & ๐ ๐. |
|||
* ๐ โ โ (๐ ๐): |
|||
* ** *๏ธโฃ ** |
|||
* ๐ถ โฉ ๐ฏ โ๏ธ ๐ ๐ธ๐ฒ & `pytest` |
|||
* **โ** |
|||
* **๐ช ๐** |
|||
* ...& ๐. |
|||
|
|||
## ๐ญ |
|||
|
|||
๐ฌ ๐ธ๐ฒ ๐ ๐ฆ **FastAPI** ๐ธ ๐โโ ๐ฝ Uvicorn <a href="https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7" class="external-link" target="_blank">1๏ธโฃ โฉ ๐ ๐ ๏ธ ๐ช</a>, ๐ด ๐ ๐ & Uvicorn ๐ซ (โ๏ธ ๐ FastAPI). (*) |
|||
|
|||
๐ค ๐ ๐ โซ๏ธ, ๐ ๐ <a href="https://fastapi.tiangolo.com/benchmarks/" class="internal-link" target="_blank">๐</a>. |
|||
|
|||
## ๐ฆ ๐ |
|||
|
|||
โ๏ธ Pydantic: |
|||
|
|||
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - โฉ ๐ป <abbr title="converting the string that comes from an HTTP request into Python data">"๐ป"</abbr>. |
|||
* <a href="https://github.com/JoshData/python-email-validator" target="_blank"><code>email_validator</code></a> - ๐ง ๐ฌ. |
|||
|
|||
โ๏ธ ๐: |
|||
|
|||
* <a href="https://www.python-httpx.org" target="_blank"><code>httpx</code></a> - โ ๐ฅ ๐ ๐ โ๏ธ `TestClient`. |
|||
* <a href="https://jinja.palletsprojects.com" target="_blank"><code>jinja2</code></a> - โ ๐ฅ ๐ ๐ โ๏ธ ๐ข ๐ ๐ณ. |
|||
* <a href="https://andrew-d.github.io/python-multipart/" target="_blank"><code>python-multipart</code></a> - โ ๐ฅ ๐ ๐ ๐โ๐ฆบ ๐จ <abbr title="converting the string that comes from an HTTP request into Python data">"โ"</abbr>, โฎ๏ธ `request.form()`. |
|||
* <a href="https://pythonhosted.org/itsdangerous/" target="_blank"><code>itsdangerous</code></a> - โ `SessionMiddleware` ๐โ๐ฆบ. |
|||
* <a href="https://pyyaml.org/wiki/PyYAMLDocumentation" target="_blank"><code>pyyaml</code></a> - โ ๐ `SchemaGenerator` ๐โ๐ฆบ (๐ ๐ฒ ๐ซ ๐ช โซ๏ธ โฎ๏ธ FastAPI). |
|||
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - โ ๐ฅ ๐ ๐ โ๏ธ `UJSONResponse`. |
|||
|
|||
โ๏ธ FastAPI / ๐: |
|||
|
|||
* <a href="https://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - ๐ฝ ๐ ๐ & ๐ฆ ๐ ๐ธ. |
|||
* <a href="https://github.com/ijl/orjson" target="_blank"><code>orjson</code></a> - โ ๐ฅ ๐ ๐ โ๏ธ `ORJSONResponse`. |
|||
|
|||
๐ ๐ช โ ๐ ๐ซ โฎ๏ธ `pip install "fastapi[all]"`. |
|||
|
|||
## ๐ |
|||
|
|||
๐ ๐ ยฎ ๐ฝ โ ๐ฉ๐ช ๐. |
@ -0,0 +1,84 @@ |
|||
# ๐ โก - ๐ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ ๐ค โถ๏ธ, โซ๏ธ ๐ ๐ โถ๏ธ โ ๐, ๐โโ, ๐ฝ & ๐ ๏ธ ๐ โช โ ๐. |
|||
|
|||
๐ ๐ ๐ ๐ง โ๏ธ ๐ถ ๐ ๐ฅ ๐ ๐ ๐ โน & ๐ ๏ธ ๐ ๐ ๐ช, โ๏ธ โซ๏ธ ๐ช ๐ โถ๏ธ โ ๐ ๐. |
|||
|
|||
## ๐ ๐ FastAPI โณ |
|||
|
|||
๐: <a href="https://github.com/tiangolo/full-stack-fastapi-postgresql" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-fastapi-postgresql</a> |
|||
|
|||
### ๐ ๐ FastAPI โณ - โ |
|||
|
|||
* ๐ **โ** ๐ ๏ธ (โ ๐งข). |
|||
* โ ๐ ๐ณ ๐ ๏ธ. |
|||
* **โ โ** ๐ ๏ธ & ๐ ๏ธ ๐ง๐ฟ ๐ ๏ธ. |
|||
* **๐ญ ๐** ๐ ๐ธ ๐ฝ โ๏ธ Uvicorn & ๐. |
|||
* ๐ <a href="https://github.com/tiangolo/fastapi" class="external-link" target="_blank">**FastAPI**</a> ๐ฉโ๐ป: |
|||
* **โฉ**: ๐ถ โ ๐ญ, ๐ ๐ท๐ช โฎ๏ธ **โณ** & **๐ถ** (๐ ๐ & Pydantic). |
|||
* **๐๏ธ**: ๐ ๐จโ๐จ ๐โ๐ฆบ. <abbr title="also known as auto-complete, autocompletion, IntelliSense">๐ ๏ธ</abbr> ๐. ๐ ๐ฐ ๐ ๏ธ. |
|||
* **โฉ**: ๐ง โฉ โ๏ธ & ๐ก. ๐ ๐ฐ ๐ ๐ฉบ. |
|||
* **๐**: ๐ ๐ โ. ๐ โ โช๏ธโก๏ธ ๐ ๐ข ๐. |
|||
* **๐๏ธ**: ๐ค ๐ญ-๐ ๐. โฎ๏ธ ๐ง ๐ ๐งพ. |
|||
* **๐ฉ-โ๏ธ**: โ๏ธ ๐ (& ๐ ๐ โฎ๏ธ) ๐ ๐ฉ ๐: <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank">๐</a> & <a href="https://json-schema.org/" class="external-link" target="_blank">๐ป ๐</a>. |
|||
* <a href="https://fastapi.tiangolo.com/features/" class="external-link" target="_blank">**๐ ๐ โ**</a> ๐ ๐ง ๐ฌ, ๐ ๏ธ, ๐ ๐งพ, ๐ค โฎ๏ธ Oauth2๏ธโฃ ๐ฅ ๐ค, โ๏ธ. |
|||
* **๐ ๐** ๐ ๐ข. |
|||
* **๐ฅ ๐ค** ๐ค. |
|||
* **๐ธ๐ฒ** ๐ท (๐ฌ ๐บ โ, ๐ซ ๐ช โ๏ธ โฎ๏ธ ๐ฅ ๐จโ๐ญ ๐). |
|||
* ๐ฐ โถ๏ธ ๐ท ๐ฉโ๐ป (๐ & โ ๐ ๐ช). |
|||
* **โ** ๐ ๏ธ. |
|||
* **โ** (โ๏ธ ๐จ๐ณ โน ๐ค). |
|||
* **๐ฅ** ๐จโ๐ญ ๐ ๐ช ๐ & โ๏ธ ๐ท & ๐ โช๏ธโก๏ธ ๐ ๐ฉโ๐ป ๐. |
|||
* ๐ ๐ฉโ๐ป ๐ฏ โ๏ธ ๐ **โณ**, ๐ ๏ธ โฎ๏ธ โ, ๐ ๐ช ๐ฏ ๐ ๐ ๏ธ ๐, ๐ฌ ๐ ๐ฝ. โซ๏ธ ๐ โ, โซ๏ธ ๐ช ๐ ๐ ๐ฝ ๐ช โช๏ธโก๏ธ ๐ ๐ ๐ฐ (๐ ๐ช โ๏ธ โณ, โณ, โณ, โ๏ธ โซ๏ธโ ๐ ๐, & ๐ฏ ๐ ๐ ๏ธ ๐ท). |
|||
* โฉ ๐ ๐ ๏ธ โฎ๏ธ **๐ ๐พ** ๐ฐ โ๏ธ-โ ๐ ๏ธ โฎ๏ธ โ ๐ โ โ โ๏ธ ๐ ๐ ๐ ๐. |
|||
* **๐ฆ** ๐ธ: |
|||
* ๐ โฎ๏ธ ๐ฆ โณ. |
|||
* **๐ฅ ๐ค** ๐. |
|||
* ๐ณ ๐. |
|||
* โฎ๏ธ ๐ณ, ๐ ๐น ๐. |
|||
* ๐ ๐น โฎ๏ธ ๐ฉโ๐ป ๐ & ๐. |
|||
* ๐ค ๐ฉโ๐ป ๐. |
|||
* **๐ท๐ช**. |
|||
* **๐ฆ-๐ป**. |
|||
* **Vuetify** ๐น ๐งฝ ๐ง ๐ฆฒ. |
|||
* **๐**. |
|||
* โ ๐ฝ โ๏ธ ๐ **๐** (๐ถ ๐คพ ๐ โฎ๏ธ ๐ฆ-๐ป). |
|||
* โ ๐-โถ๏ธ ๐, ๐ ๐ซ ๐ช ๐ โ๏ธ ๐ โ ๐. |
|||
* ๐ธ ๐ฏ ๐ ๐ ๐ฐ (๐ช ๐ ๐โโ๏ธ). |
|||
* โ ๐ง ๐ช, โซ๏ธ ๐ท ๐
๐ฆ, โ๏ธ ๐ ๐ช ๐ค-๐ โฎ๏ธ ๐ฆ โณ โ๏ธ โ โซ๏ธ ๐ ๐ช, & ๐ค-โ๏ธ โซ๏ธโ ๐ ๐. |
|||
* ** *๏ธโฃ ** โณ ๐ฝ, ๐ ๐ช ๐ โซ๏ธ โ๏ธ ๐ & โณ ๐ช. |
|||
* **๐ฅ** ๐ฅ ๐จโ๐ญ โ. |
|||
* ๐ โ ๐ ๐ธ & ๐ฉโ๐ป โฎ๏ธ **Traefik**, ๐ ๐ช โ๏ธ ๐ฏโโ๏ธ ๐ฝ ๐ ๐, ๐ฝ โก, โ๏ธ ๐ฆ ๐ ๐ฆ. |
|||
* Traefik ๐ ๏ธ, โ
โก๏ธ ๐ **๐บ๐ธ๐** ๐ ๐ง โก. |
|||
* โณ **๐** (๐ ๐ ๏ธ), ๐ ๐ธ & ๐ฉโ๐ป ๐ฌ. |
|||
|
|||
## ๐ ๐ FastAPI ๐ |
|||
|
|||
๐: <a href="https://github.com/tiangolo/full-stack-fastapi-couchbase" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-fastapi-couchbase</a> |
|||
|
|||
๐ถ ๐ถ **โ ** ๐ถ ๐ถ |
|||
|
|||
๐ฅ ๐ โถ๏ธ ๐ ๐ โช๏ธโก๏ธ ๐, โ
๐ ๐ฅ. |
|||
|
|||
๐ผ, ๐ ๐ <a href="https://github.com/tiangolo/full-stack-fastapi-postgresql" class="external-link" target="_blank">๐ ๐ FastAPI โณ</a> ๐ช ๐ ๐, โซ๏ธ ๐ฏ ๐ง & โ๏ธ. & โซ๏ธ ๐ ๐ ๐ โ & ๐. |
|||
|
|||
๐ ๐ โ๏ธ ๐-โ๏ธ ๐ ๐ฅ ๐ ๐, โซ๏ธ ๐ ๐ฒ ๐ท ๐, & ๐ฅ ๐ โช โ๏ธ ๐ ๐ โฎ๏ธ โซ๏ธ ๐ ๐ ๐ (& ๐ ๐ฒ โช โน โซ๏ธ โฃ ๐ ๐ช). |
|||
|
|||
๐ ๐ช โ ๐
๐ โซ๏ธ ๐ฉบ ๐ฆ. |
|||
|
|||
## ๐ ๐ FastAPI โณ |
|||
|
|||
...๐ช ๐ โช, โ๏ธ ๐ ๐ ๐ฐ ๐ & ๐ โ. ๐ถ ๐ถ |
|||
|
|||
## ๐ฐ ๐ซ ๐ท โฎ๏ธ ๐ & FastAPI |
|||
|
|||
๐: <a href="https://github.com/microsoft/cookiecutter-spacy-fastapi" class="external-link" target="_blank">https://github.com/microsoft/cookiecutter-spacy-fastapi</a> |
|||
|
|||
### ๐ฐ ๐ซ ๐ท โฎ๏ธ ๐ & FastAPI - โ |
|||
|
|||
* **๐** ๐ ๐ท ๐ ๏ธ. |
|||
* **โ ๐ง ๐** ๐จ ๐ ๐. |
|||
* **๐ญ ๐** ๐ ๐ธ ๐ฝ โ๏ธ Uvicorn & ๐. |
|||
* **โ ๐ฉโ๐ป** Kubernete (๐ฆฒ) ๐/๐ฟ ๐ ๏ธ ๐. |
|||
* **๐คธโโ** ๐ช โ 1๏ธโฃ ๐ ๐ ๐ช๐ธ โฎ๏ธ ๐ ๐ฅ. |
|||
* **๐ช ๐ง** ๐ ๐ท ๐ ๏ธ (Pytorch, ๐ธ๐ฒ), ๐ซ ๐. |
@ -0,0 +1,490 @@ |
|||
# ๐ ๐ ๐ถ |
|||
|
|||
๐ โ๏ธ ๐โ๐ฆบ ๐ฆ "๐ ๐". |
|||
|
|||
๐ซ **"๐ ๐"** ๐ โ ๐ โ ๐ฃ <abbr title="for example: str, int, float, bool">๐</abbr> ๐ข. |
|||
|
|||
๐ฃ ๐ ๐ ๐ข, ๐จโ๐จ & ๐งฐ ๐ช ๐ค ๐ ๐ ๐โ๐ฆบ. |
|||
|
|||
๐ **โฉ ๐ฐ / โ๏ธ** ๐ ๐ ๐ ๐. โซ๏ธ ๐ ๐ด ๐ฏ ๐ช โ๏ธ ๐ซ โฎ๏ธ **FastAPI**... โ ๐ค ๐ถ ๐ฅ. |
|||
|
|||
**FastAPI** ๐ โ๏ธ ๐ ๐ซ ๐ ๐, ๐ซ ๐ค โซ๏ธ ๐ ๐ & ๐ฐ. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐
โ๏ธ **FastAPI**, ๐ ๐ ๐ฐ โช๏ธโก๏ธ ๐ซ ๐ ๐ ๐ซ. |
|||
|
|||
!!! note |
|||
๐ฅ ๐ ๐ ๐ด, & ๐ โช ๐ญ ๐ ๐ ๐ ๐, ๐ถ โญ ๐. |
|||
|
|||
## ๐ฏ |
|||
|
|||
โก๏ธ โถ๏ธ โฎ๏ธ ๐
๐ผ: |
|||
|
|||
```Python |
|||
{!../../../docs_src/python_types/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ค ๐ ๐ ๐ข: |
|||
|
|||
``` |
|||
John Doe |
|||
``` |
|||
|
|||
๐ข ๐จ ๐: |
|||
|
|||
* โ `first_name` & `last_name`. |
|||
* ๐ ๐ฅ ๐ค ๐ 1๏ธโฃ โ ๐ผ โฎ๏ธ `title()`. |
|||
* <abbr title="Puts them together, as one. With the contents of one after the other.">๐ข</abbr> ๐ซ โฎ๏ธ ๐ ๐. |
|||
|
|||
```Python hl_lines="2" |
|||
{!../../../docs_src/python_types/tutorial001.py!} |
|||
``` |
|||
|
|||
### โ โซ๏ธ |
|||
|
|||
โซ๏ธ ๐ถ ๐
๐. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ ๐ โ โซ๏ธ โช๏ธโก๏ธ ๐. |
|||
|
|||
โ ๐ ๐ โ๏ธ โถ๏ธ ๐ ๐ข, ๐ โ๏ธ ๐ข ๐... |
|||
|
|||
โ๏ธ โคด๏ธ ๐ โ๏ธ ๐ค "๐ ๐ฉโ๐ฌ ๐ ๐ ๐ฅ ๐ค โ ๐ผ". |
|||
|
|||
โซ๏ธ `upper`โ โซ๏ธ `uppercase`โ `first_uppercase`โ `capitalize`โ |
|||
|
|||
โคด๏ธ, ๐ ๐ โฎ๏ธ ๐ ๐ฉโ๐ป ๐จโ๐งโ๐ฆ, ๐จโ๐จ โ. |
|||
|
|||
๐ ๐ ๐ฅ ๐ข ๐ข, `first_name`, โคด๏ธ โฃ (`.`) & โคด๏ธ ๐ฏ `Ctrl+Space` โฒ ๐ ๏ธ. |
|||
|
|||
โ๏ธ, ๐, ๐ ๐ค ๐ณ โ : |
|||
|
|||
<img src="/img/python-types/image01.png"> |
|||
|
|||
### ๐ฎ ๐ |
|||
|
|||
โก๏ธ ๐ ๐ โธ โช๏ธโก๏ธ โฎ๏ธ โฌ. |
|||
|
|||
๐ฅ ๐ ๐ โซ๏ธโ ๐ ๐งฌ, ๐ข ๐ข, โช๏ธโก๏ธ: |
|||
|
|||
```Python |
|||
first_name, last_name |
|||
``` |
|||
|
|||
: |
|||
|
|||
```Python |
|||
first_name: str, last_name: str |
|||
``` |
|||
|
|||
๐ โซ๏ธ. |
|||
|
|||
๐ "๐ ๐": |
|||
|
|||
```Python hl_lines="1" |
|||
{!../../../docs_src/python_types/tutorial002.py!} |
|||
``` |
|||
|
|||
๐ ๐ซ ๐ ๐ฃ ๐ข ๐ฒ ๐ ๐ โฎ๏ธ: |
|||
|
|||
```Python |
|||
first_name="john", last_name="doe" |
|||
``` |
|||
|
|||
โซ๏ธ ๐ ๐. |
|||
|
|||
๐ฅ โ๏ธ โค (`:`), ๐ซ ๐ (`=`). |
|||
|
|||
& โ ๐ ๐ ๐ ๐ซ ๐ โซ๏ธโ ๐จ โช๏ธโก๏ธ โซ๏ธโ ๐ ๐จ ๐ต ๐ซ. |
|||
|
|||
โ๏ธ ๐, ๐ ๐ ๐ ๐ ๐ ๐ ๐ข, โ๏ธ โฎ๏ธ ๐ ๐. |
|||
|
|||
๐ โ, ๐ ๐ โฒ ๐ โฎ๏ธ `Ctrl+Space` & ๐ ๐: |
|||
|
|||
<img src="/img/python-types/image02.png"> |
|||
|
|||
โฎ๏ธ ๐, ๐ ๐ช ๐, ๐ ๐, โญ ๐ ๐ 1๏ธโฃ ๐ "๐ ๐": |
|||
|
|||
<img src="/img/python-types/image03.png"> |
|||
|
|||
## ๐
๐ฏ |
|||
|
|||
โ
๐ ๐ข, โซ๏ธ โช โ๏ธ ๐ ๐: |
|||
|
|||
```Python hl_lines="1" |
|||
{!../../../docs_src/python_types/tutorial003.py!} |
|||
``` |
|||
|
|||
โฉ๏ธ ๐จโ๐จ ๐ญ ๐ ๐ข, ๐ ๐ซ ๐ด ๐ค ๐ ๏ธ, ๐ ๐ค โ โ
: |
|||
|
|||
<img src="/img/python-types/image04.png"> |
|||
|
|||
๐ ๐ ๐ญ ๐ ๐ โ๏ธ ๐ง โซ๏ธ, ๐ `age` ๐ป โฎ๏ธ `str(age)`: |
|||
|
|||
```Python hl_lines="2" |
|||
{!../../../docs_src/python_types/tutorial004.py!} |
|||
``` |
|||
|
|||
## ๐ฃ ๐ |
|||
|
|||
๐ ๐ ๐ ๐ฅ ๐ฃ ๐ ๐. ๐ข ๐ข. |
|||
|
|||
๐ ๐ ๐ฅ ๐ ๐ โ๏ธ ๐ซ โฎ๏ธ **FastAPI**. |
|||
|
|||
### ๐
๐ |
|||
|
|||
๐ ๐ช ๐ฃ ๐ ๐ฉ ๐ ๐, ๐ซ ๐ด `str`. |
|||
|
|||
๐ ๐ช โ๏ธ, ๐ผ: |
|||
|
|||
* `int` |
|||
* `float` |
|||
* `bool` |
|||
* `bytes` |
|||
|
|||
```Python hl_lines="1" |
|||
{!../../../docs_src/python_types/tutorial005.py!} |
|||
``` |
|||
|
|||
### ๐ ๐ โฎ๏ธ ๐ ๐ข |
|||
|
|||
๐ค ๐ ๐ ๐ ๐ช ๐ ๐ ๐ฒ, ๐ `dict`, `list`, `set` & `tuple`. & ๐ ๐ฒ ๐ช โ๏ธ ๐ซ ๐ ๐ ๐โโ๏ธ. |
|||
|
|||
๐ ๐ ๐ โ๏ธ ๐ ๐ ๐ค "**๐**" ๐. & โซ๏ธ ๐ช ๐ฃ ๐ซ, โฎ๏ธ ๐ซ ๐ ๐. |
|||
|
|||
๐ฃ ๐ ๐ & ๐ ๐, ๐ ๐ช โ๏ธ ๐ฉ ๐ ๐น `typing`. โซ๏ธ ๐ ๐ฏ ๐โ๐ฆบ ๐ซ ๐ ๐. |
|||
|
|||
#### ๐ โฌ ๐ |
|||
|
|||
โ โ๏ธ `typing` **๐** โฎ๏ธ ๐ โฌ, โช๏ธโก๏ธ ๐ 3๏ธโฃ.6๏ธโฃ โช ๐, โ
๐ 3๏ธโฃ.9๏ธโฃ, ๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ, โ๏ธ. |
|||
|
|||
๐ ๐ง, **๐ โฌ** ๐ โฎ๏ธ ๐ ๐โ๐ฆบ ๐ ๐ โ & ๐ ๐ผ ๐ ๐ ๐ซ ๐ช ๐ & โ๏ธ `typing` ๐น ๐ฃ ๐ โ. |
|||
|
|||
๐ฅ ๐ ๐ช โ ๐ โฎ๏ธ โฌ ๐ ๐ ๐, ๐ ๐ ๐ช โ ๐ ๐ โ ๐ฆ. ๐ ๐ผ ๐. |
|||
|
|||
#### ๐ |
|||
|
|||
๐ผ, โก๏ธ ๐ฌ ๐ข `list` `str`. |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
โช๏ธโก๏ธ `typing`, ๐ `List` (โฎ๏ธ ๐ `L`): |
|||
|
|||
``` Python hl_lines="1" |
|||
{!> ../../../docs_src/python_types/tutorial006.py!} |
|||
``` |
|||
|
|||
๐ฃ ๐ข, โฎ๏ธ ๐ โค (`:`) โ. |
|||
|
|||
๐, ๐ฎ `List` ๐ ๐ ๐ โช๏ธโก๏ธ `typing`. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ ๐, ๐ ๐ฎ ๐ซ โฌ ๐: |
|||
|
|||
```Python hl_lines="4" |
|||
{!> ../../../docs_src/python_types/tutorial006.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
๐ฃ ๐ข, โฎ๏ธ ๐ โค (`:`) โ. |
|||
|
|||
๐, ๐ฎ `list`. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ ๐, ๐ ๐ฎ ๐ซ โฌ ๐: |
|||
|
|||
```Python hl_lines="1" |
|||
{!> ../../../docs_src/python_types/tutorial006_py39.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
๐ ๐ ๐ โฌ ๐ ๐ค "๐ ๐ข". |
|||
|
|||
๐ ๐ผ, `str` ๐ ๐ข ๐ถโโ๏ธ `List` (โ๏ธ `list` ๐ 3๏ธโฃ.9๏ธโฃ & ๐). |
|||
|
|||
๐ โ: "๐ข `items` `list`, & ๐ ๐ฌ ๐ ๐ `str`". |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ โ๏ธ ๐ 3๏ธโฃ.9๏ธโฃ โ๏ธ ๐, ๐ ๐ซ โ๏ธ ๐ `List` โช๏ธโก๏ธ `typing`, ๐ ๐ช โ๏ธ ๐ ๐ฅ `list` ๐ โฉ๏ธ. |
|||
|
|||
๐จ ๐, ๐ ๐จโ๐จ ๐ช ๐ ๐โ๐ฆบ โช ๐ญ ๐ฌ โช๏ธโก๏ธ ๐: |
|||
|
|||
<img src="/img/python-types/image05.png"> |
|||
|
|||
๐ต ๐, ๐ ๐ ๐ช ๐. |
|||
|
|||
๐ ๐ ๐ข `item` 1๏ธโฃ ๐ฃ ๐ `items`. |
|||
|
|||
& , ๐จโ๐จ ๐ญ โซ๏ธ `str`, & ๐ ๐โ๐ฆบ ๐. |
|||
|
|||
#### ๐ข & โ |
|||
|
|||
๐ ๐ ๐ ๐ฃ `tuple`โ & `set`โ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!> ../../../docs_src/python_types/tutorial007.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1" |
|||
{!> ../../../docs_src/python_types/tutorial007_py39.py!} |
|||
``` |
|||
|
|||
๐ โ: |
|||
|
|||
* ๐ข `items_t` `tuple` โฎ๏ธ 3๏ธโฃ ๐ฌ, `int`, โ1๏ธโฃ `int`, & `str`. |
|||
* ๐ข `items_s` `set`, & ๐ ๐ฎ ๐ฌ ๐ `bytes`. |
|||
|
|||
#### #๏ธโฃ |
|||
|
|||
๐ฌ `dict`, ๐ ๐ถโโ๏ธ 2๏ธโฃ ๐ ๐ข, ๐ โ. |
|||
|
|||
๐ฅ ๐ ๐ข ๐ `dict`. |
|||
|
|||
๐ฅ ๐ ๐ข ๐ฒ `dict`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!> ../../../docs_src/python_types/tutorial008.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1" |
|||
{!> ../../../docs_src/python_types/tutorial008_py39.py!} |
|||
``` |
|||
|
|||
๐ โ: |
|||
|
|||
* ๐ข `prices` `dict`: |
|||
* ๐ ๐ `dict` ๐ `str` (โก๏ธ ๐ฌ, ๐ ๐ ๐ฌ). |
|||
* ๐ฒ ๐ `dict` ๐ `float` (โก๏ธ ๐ฌ, ๐ ๐ ๐ฌ). |
|||
|
|||
#### ๐ช๐บ |
|||
|
|||
๐ ๐ช ๐ฃ ๐ ๐ข ๐ช ๐ **๐ ๐**, ๐ผ, `int` โ๏ธ `str`. |
|||
|
|||
๐ 3๏ธโฃ.6๏ธโฃ & ๐ (โ
๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ) ๐ ๐ช โ๏ธ `Union` ๐ โช๏ธโก๏ธ `typing` & ๐ฎ ๐ โฌ ๐ ๐ช ๐ ๐ซ. |
|||
|
|||
๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ ๐ค **๐ โ** ๐โ ๐ ๐ช ๐ฎ ๐ช ๐ ๐ฝ <abbr title='also called "bitwise or operator", but that meaning is not relevant here'>โธ โธ (`|`)</abbr>. |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!> ../../../docs_src/python_types/tutorial008b.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1" |
|||
{!> ../../../docs_src/python_types/tutorial008b_py310.py!} |
|||
``` |
|||
|
|||
๐ฏโโ๏ธ ๐ผ ๐ โ ๐ `item` ๐ช `int` โ๏ธ `str`. |
|||
|
|||
#### ๐ฒ `None` |
|||
|
|||
๐ ๐ช ๐ฃ ๐ ๐ฒ ๐ช โ๏ธ ๐, ๐ `str`, โ๏ธ ๐ โซ๏ธ ๐ช `None`. |
|||
|
|||
๐ 3๏ธโฃ.6๏ธโฃ & ๐ (โ
๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ) ๐ ๐ช ๐ฃ โซ๏ธ ๐ญ & โ๏ธ `Optional` โช๏ธโก๏ธ `typing` ๐น. |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!../../../docs_src/python_types/tutorial009.py!} |
|||
``` |
|||
|
|||
โ๏ธ `Optional[str]` โฉ๏ธ `str` ๐ โก๏ธ ๐จโ๐จ โน ๐ ๐ โ ๐โ ๐ ๐ช ๐ค ๐ ๐ฒ ๐ง `str`, ๐โ โซ๏ธ ๐ช ๐ค `None` ๐โโ๏ธ. |
|||
|
|||
`Optional[Something]` ๐ค โจ `Union[Something, None]`, ๐ซ ๐. |
|||
|
|||
๐ โ ๐ ๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ, ๐ ๐ช โ๏ธ `Something | None`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!> ../../../docs_src/python_types/tutorial009.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐ - ๐" |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!> ../../../docs_src/python_types/tutorial009b.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1" |
|||
{!> ../../../docs_src/python_types/tutorial009_py310.py!} |
|||
``` |
|||
|
|||
#### โ๏ธ `Union` โ๏ธ `Optional` |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐ โฌ ๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ, ๐ฅ ๐โโ โช๏ธโก๏ธ ๐ ๐ถ **๐ค** โ ๐: |
|||
|
|||
* ๐ถ โ โ๏ธ `Optional[SomeType]` |
|||
* โฉ๏ธ ๐ถ **โ๏ธ `Union[SomeType, None]`** ๐ถ. |
|||
|
|||
๐ฏโโ๏ธ ๐ & ๐ ๐ซ ๐, โ๏ธ ๐ค ๐ ๐ `Union` โฉ๏ธ `Optional` โฉ๏ธ ๐ค "**๐ฆ**" ๐ ๐ ๐ ๐ ๐ฒ ๐ฆ, & โซ๏ธ ๐ค โ "โซ๏ธ ๐ช `None`", ๐ฅ โซ๏ธ ๐ซ ๐ฆ & โ. |
|||
|
|||
๐ค ๐ญ `Union[SomeType, None]` ๐ ๐ ๐ โซ๏ธโ โซ๏ธ โ. |
|||
|
|||
โซ๏ธ ๐ ๐ค & ๐. โ๏ธ ๐ ๐ค ๐ช ๐ โ ๐ & ๐ ๐คฝโโ ๐ญ ๐ ๐. |
|||
|
|||
๐ผ, โก๏ธ โ ๐ ๐ข: |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!../../../docs_src/python_types/tutorial009c.py!} |
|||
``` |
|||
|
|||
๐ข `name` ๐ฌ `Optional[str]`, โ๏ธ โซ๏ธ **๐ซ ๐ฆ**, ๐ ๐ซ๐ ๐ค ๐ข ๐ต ๐ข: |
|||
|
|||
```Python |
|||
say_hi() # Oh, no, this throws an error! ๐ฑ |
|||
``` |
|||
|
|||
`name` ๐ข **โ** (๐ซ *๐ฆ*) โฉ๏ธ โซ๏ธ ๐ซ โ๏ธ ๐ข ๐ฒ. , `name` ๐ซ `None` ๐ฒ: |
|||
|
|||
```Python |
|||
say_hi(name=None) # This works, None is valid ๐ |
|||
``` |
|||
|
|||
๐ ๐ฐ, ๐ ๐ ๐ ๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ ๐ ๐ ๐ซ โ๏ธ ๐ ๐ ๐, ๐ ๐ ๐ช ๐ฏ โ๏ธ `|` ๐ฌ ๐ช๐บ ๐: |
|||
|
|||
```Python hl_lines="1 4" |
|||
{!../../../docs_src/python_types/tutorial009c_py310.py!} |
|||
``` |
|||
|
|||
& โคด๏ธ ๐ ๐ ๐ซ โ๏ธ ๐ ๐ ๐ ๐ `Optional` & `Union`. ๐ถ |
|||
|
|||
#### ๐ ๐ |
|||
|
|||
๐ ๐ ๐ โ ๐ ๐ข โฌ ๐ ๐ค **๐ ๐** โ๏ธ **๐**, ๐ผ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
* `List` |
|||
* `Tuple` |
|||
* `Set` |
|||
* `Dict` |
|||
* `Union` |
|||
* `Optional` |
|||
* ...& ๐. |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ฝ ๐ ๐ (โฎ๏ธ โฌ ๐ & ๐ ๐): |
|||
|
|||
* `list` |
|||
* `tuple` |
|||
* `set` |
|||
* `dict` |
|||
|
|||
& ๐ โฎ๏ธ ๐ 3๏ธโฃ.6๏ธโฃ, โช๏ธโก๏ธ `typing` ๐น: |
|||
|
|||
* `Union` |
|||
* `Optional` |
|||
* ...& ๐. |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ฝ ๐ ๐ (โฎ๏ธ โฌ ๐ & ๐ ๐): |
|||
|
|||
* `list` |
|||
* `tuple` |
|||
* `set` |
|||
* `dict` |
|||
|
|||
& ๐ โฎ๏ธ ๐ 3๏ธโฃ.6๏ธโฃ, โช๏ธโก๏ธ `typing` ๐น: |
|||
|
|||
* `Union` |
|||
* `Optional` (๐ โฎ๏ธ ๐ 3๏ธโฃ.6๏ธโฃ) |
|||
* ...& ๐. |
|||
|
|||
๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ, ๐ โ๏ธ ๐ `Union` & `Optional`, ๐ ๐ช โ๏ธ <abbr title='also called "bitwise or operator", but that meaning is not relevant here'>โธ โธ (`|`)</abbr> ๐ฃ ๐ช๐บ ๐. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ ๐ช ๐ฃ ๐ ๐ ๐ข. |
|||
|
|||
โก๏ธ ๐ฌ ๐ โ๏ธ ๐ `Person`, โฎ๏ธ ๐: |
|||
|
|||
```Python hl_lines="1-3" |
|||
{!../../../docs_src/python_types/tutorial010.py!} |
|||
``` |
|||
|
|||
โคด๏ธ ๐ ๐ช ๐ฃ ๐ข ๐ `Person`: |
|||
|
|||
```Python hl_lines="6" |
|||
{!../../../docs_src/python_types/tutorial010.py!} |
|||
``` |
|||
|
|||
& โคด๏ธ, ๐, ๐ ๐ค ๐ ๐จโ๐จ ๐โ๐ฆบ: |
|||
|
|||
<img src="/img/python-types/image06.png"> |
|||
|
|||
## Pydantic ๐ท |
|||
|
|||
<a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> ๐ ๐ ๐ญ ๐ ๐ฌ. |
|||
|
|||
๐ ๐ฃ "๐ " ๐ฝ ๐ โฎ๏ธ ๐ข. |
|||
|
|||
& ๐ ๐ข โ๏ธ ๐. |
|||
|
|||
โคด๏ธ ๐ โ ๐ ๐ ๐ โฎ๏ธ ๐ฒ & โซ๏ธ ๐ โ ๐ฒ, ๐ ๐ซ โ ๐ (๐ฅ ๐ ๐ผ) & ๐ค ๐ ๐ โฎ๏ธ ๐ ๐ฝ. |
|||
|
|||
& ๐ ๐ค ๐ ๐จโ๐จ ๐โ๐ฆบ โฎ๏ธ ๐ ๐ ๐. |
|||
|
|||
๐ผ โช๏ธโก๏ธ ๐ Pydantic ๐ฉบ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python |
|||
{!> ../../../docs_src/python_types/tutorial011.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python |
|||
{!> ../../../docs_src/python_types/tutorial011_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python |
|||
{!> ../../../docs_src/python_types/tutorial011_py310.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
๐ก ๐ ๐ <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic, โ
๐ฎ ๐ฉบ</a>. |
|||
|
|||
**FastAPI** ๐ โ๏ธ ๐ Pydantic. |
|||
|
|||
๐ ๐ ๐ ๐ ๐
๐ ๐ ๐ก [๐ฐ - ๐ฉโ๐ป ๐ฆฎ](tutorial/index.md){.internal-link target=_blank}. |
|||
|
|||
!!! tip |
|||
Pydantic โ๏ธ ๐ ๐ญ ๐โ ๐ โ๏ธ `Optional` โ๏ธ `Union[Something, None]` ๐ต ๐ข ๐ฒ, ๐ ๐ช โ ๐
๐ โซ๏ธ Pydantic ๐ฉบ ๐ <a href="https://pydantic-docs.helpmanual.io/usage/models/#required-optional-fields" class="external-link" target="_blank">โ ๐ฆ ๐</a>. |
|||
|
|||
## ๐ ๐ **FastAPI** |
|||
|
|||
**FastAPI** โ ๐ ๐ซ ๐ ๐ ๐ ๐. |
|||
|
|||
โฎ๏ธ **FastAPI** ๐ ๐ฃ ๐ข โฎ๏ธ ๐ ๐ & ๐ ๐ค: |
|||
|
|||
* **๐จโ๐จ ๐โ๐ฆบ**. |
|||
* **๐ โ
**. |
|||
|
|||
...and **FastAPI** uses the same declarations : |
|||
|
|||
* **๐ฌ ๐**: โช๏ธโก๏ธ ๐จ โก ๐ข, ๐ข ๐ข, ๐, ๐ช, ๐, โ๏ธ. |
|||
* **๐ ๐ฝ**: โช๏ธโก๏ธ ๐จ ๐ ๐. |
|||
* **โ ๐ฝ**: ๐ โช๏ธโก๏ธ ๐ ๐จ: |
|||
* ๐ญ **๐ง โ** ๐จ ๐ฉโ๐ป ๐โ ๐ โ. |
|||
* **๐** ๐ ๏ธ โ๏ธ ๐: |
|||
* โ โคด๏ธ โ๏ธ ๐ง ๐ ๐งพ ๐ฉโ๐ป ๐ข. |
|||
|
|||
๐ 5๏ธโฃ๐ ๐ ๐ ๐. ๐ซ ๐. ๐ ๐ ๐ ๐ ๐ ๐ฏ [๐ฐ - ๐ฉโ๐ป ๐ฆฎ](tutorial/index.md){.internal-link target=_blank}. |
|||
|
|||
โ ๐ ๐ โ๏ธ ๐ฉ ๐ ๐, ๐ ๐ฅ (โฉ๏ธ โ ๐ ๐, ๐จโ๐จ, โ๏ธ), **FastAPI** ๐ ๐ ๐ท ๐. |
|||
|
|||
!!! info |
|||
๐ฅ ๐ โช ๐ถ ๐ ๐ ๐ฐ & ๐ ๐ ๐ ๐
๐ ๐, ๐ โน <a href="https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html" class="external-link" target="_blank"> "๐ฎ ๐ผ" โช๏ธโก๏ธ `mypy`</a>. |
@ -0,0 +1,102 @@ |
|||
# ๐ฅ ๐ |
|||
|
|||
๐ ๐ช ๐ฌ ๐ฅ ๐ ๐ *โฎ๏ธ* ๐ฌ ๐จ. |
|||
|
|||
๐ โ ๐ ๏ธ ๐ ๐ช ๐จ โฎ๏ธ ๐จ, โ๏ธ ๐ ๐ฉโ๐ป ๐ซ ๐ค โ๏ธ โ ๐ ๏ธ ๐ โญ ๐จ ๐จ. |
|||
|
|||
๐ ๐, ๐ผ: |
|||
|
|||
* ๐ง ๐จ ๐จ โฎ๏ธ ๐ญ ๐ฏ: |
|||
* ๐ ๐ง ๐ฝ & ๐จ ๐ง ๐ "๐" (๐ ๐ฅ), ๐ ๐ช ๐จ ๐จ โถ๏ธ๏ธ โ๏ธ & ๐จ ๐ง ๐จ ๐ฅ. |
|||
* ๐ญ ๐ฝ: |
|||
* ๐ผ, โก๏ธ ๐ฌ ๐ ๐จ ๐ ๐ ๐ ๐ถ ๐ ๐ ๐ ๏ธ, ๐ ๐ช ๐จ ๐จ "๐ซ" (๐บ๐ธ๐ 2๏ธโฃ0๏ธโฃ2๏ธโฃ) & ๐ ๏ธ โซ๏ธ ๐ฅ. |
|||
|
|||
## โ๏ธ `BackgroundTasks` |
|||
|
|||
๐ฅ, ๐ `BackgroundTasks` & ๐ฌ ๐ข ๐ *โก ๐ ๏ธ ๐ข* โฎ๏ธ ๐ ๐ `BackgroundTasks`: |
|||
|
|||
```Python hl_lines="1 13" |
|||
{!../../../docs_src/background_tasks/tutorial001.py!} |
|||
``` |
|||
|
|||
**FastAPI** ๐ โ ๐ ๐ `BackgroundTasks` ๐ & ๐ถโโ๏ธ โซ๏ธ ๐ ๐ข. |
|||
|
|||
## โ ๐ ๐ข |
|||
|
|||
โ ๐ข ๐ ๐ฅ ๐. |
|||
|
|||
โซ๏ธ ๐ฉ ๐ข ๐ ๐ช ๐จ ๐ข. |
|||
|
|||
โซ๏ธ ๐ช `async def` โ๏ธ ๐ `def` ๐ข, **FastAPI** ๐ ๐ญ โ ๐ต โซ๏ธ โ. |
|||
|
|||
๐ ๐ผ, ๐ ๐ข ๐ โ ๐ (โ ๐จ ๐ง). |
|||
|
|||
& โ ๐ ๏ธ ๐ซ โ๏ธ `async` & `await`, ๐ฅ ๐ฌ ๐ข โฎ๏ธ ๐ `def`: |
|||
|
|||
```Python hl_lines="6-9" |
|||
{!../../../docs_src/background_tasks/tutorial001.py!} |
|||
``` |
|||
|
|||
## ๐ฎ ๐ฅ ๐ |
|||
|
|||
๐ ๐ *โก ๐ ๏ธ ๐ข*, ๐ถโโ๏ธ ๐ ๐ ๐ข *๐ฅ ๐* ๐ โฎ๏ธ ๐ฉโ๐ฌ `.add_task()`: |
|||
|
|||
```Python hl_lines="14" |
|||
{!../../../docs_src/background_tasks/tutorial001.py!} |
|||
``` |
|||
|
|||
`.add_task()` ๐จ โ: |
|||
|
|||
* ๐ ๐ข ๐ ๐ฅ (`write_notification`). |
|||
* ๐ ๐ โ ๐ ๐ ๐ถโโ๏ธ ๐ ๐ข โ (`email`). |
|||
* ๐ ๐จ๐ป โ ๐ ๐ ๐ถโโ๏ธ ๐ ๐ข (`message="some notification"`). |
|||
|
|||
## ๐ ๐ |
|||
|
|||
โ๏ธ `BackgroundTasks` ๐ท โฎ๏ธ ๐ ๐ โ๏ธ, ๐ ๐ช ๐ฃ ๐ข ๐ `BackgroundTasks` ๐ ๐: *โก ๐ ๏ธ ๐ข*, ๐ (โ), ๐ง-๐, โ๏ธ. |
|||
|
|||
**FastAPI** ๐ญ โซ๏ธโ ๐ ๐ผ & โ ๐ค-โ๏ธ ๐ ๐, ๐ ๐ ๐ฅ ๐ ๐ ๐ฏโโ๏ธ & ๐ ๐ฅ โฎ๏ธ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="13 15 22 25" |
|||
{!> ../../../docs_src/background_tasks/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="11 13 20 23" |
|||
{!> ../../../docs_src/background_tasks/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, ๐ง ๐ โ `log.txt` ๐ *โฎ๏ธ* ๐จ ๐จ. |
|||
|
|||
๐ฅ ๐ค ๐ข ๐จ, โซ๏ธ ๐ โ ๐น ๐ฅ ๐. |
|||
|
|||
& โคด๏ธ โ1๏ธโฃ ๐ฅ ๐ ๐ *โก ๐ ๏ธ ๐ข* ๐ โ ๐ง โ๏ธ `email` โก ๐ข. |
|||
|
|||
## ๐ก โน |
|||
|
|||
๐ `BackgroundTasks` ๐ ๐ โช๏ธโก๏ธ <a href="https://www.starlette.io/background/" class="external-link" target="_blank">`starlette.background`</a>. |
|||
|
|||
โซ๏ธ ๐/๐ ๐ ๐ FastAPI ๐ ๐ ๐ช ๐ โซ๏ธ โช๏ธโก๏ธ `fastapi` & โ ๐ซ ๐ ๐ `BackgroundTask` (๐ต `s` ๐) โช๏ธโก๏ธ `starlette.background`. |
|||
|
|||
๐ด โ๏ธ `BackgroundTasks` (& ๐ซ `BackgroundTask`), โซ๏ธ โคด๏ธ ๐ช โ๏ธ โซ๏ธ *โก ๐ ๏ธ ๐ข* ๐ข & โ๏ธ **FastAPI** ๐ต ๐ ๐, ๐ ๐โ โ๏ธ `Request` ๐ ๐. |
|||
|
|||
โซ๏ธ ๐ช โ๏ธ `BackgroundTask` ๐ FastAPI, โ๏ธ ๐ โ๏ธ โ ๐ ๐ ๐ & ๐จ ๐ `Response` ๐ โซ๏ธ. |
|||
|
|||
๐ ๐ช ๐ ๐ โน <a href="https://www.starlette.io/background/" class="external-link" target="_blank">๐ ๐ ๐ฉบ ๐ฅ ๐</a>. |
|||
|
|||
## โ |
|||
|
|||
๐ฅ ๐ ๐ช ๐ญ ๐๏ธ ๐ฅ ๐ & ๐ ๐ซ ๐ฏ ๐ช โซ๏ธ ๐ ๐ ๐ ๏ธ (๐ผ, ๐ ๐ซ ๐ช ๐ฐ ๐พ, ๐ข, โ๏ธ), ๐ ๐ช ๐ฐ โช๏ธโก๏ธ โ๏ธ ๐ ๐ฆ ๐งฐ ๐ <a href="https://docs.celeryq.dev" class="external-link" target="_blank">๐ฅ</a>. |
|||
|
|||
๐ซ ๐ ๐ ๐ ๐ ๐ณ, ๐ง/๐จโ๐ญ ๐ค ๐จโ๐ผ, ๐ โณ โ๏ธ โณ, โ๏ธ ๐ซ โ ๐ ๐ ๐ฅ ๐ ๐ ๐ ๏ธ, & โด๏ธ, ๐ ๐ฝ. |
|||
|
|||
๐ ๐ผ, โ
[๐ ๐](../project-generation.md){.internal-link target=_blank}, ๐ซ ๐ ๐ ๐ฅ โช ๐ถ. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ช ๐ ๐ข & ๐ โช๏ธโก๏ธ ๐ **FastAPI** ๐ฑ, โ๏ธ ๐ ๐ช ๐ญ ๐คช ๐ฅ ๐ (๐ ๐จ ๐ง ๐จ), ๐ ๐ช ๐ฏ โ๏ธ `BackgroundTasks`. |
|||
|
|||
## ๐ |
|||
|
|||
๐ & โ๏ธ `BackgroundTasks` โฎ๏ธ ๐ข *โก ๐ ๏ธ ๐ข* & ๐ ๐ฎ ๐ฅ ๐. |
@ -0,0 +1,488 @@ |
|||
# ๐ฆ ๐ธ - ๐ ๐ |
|||
|
|||
๐ฅ ๐ ๐ ๐ธ โ๏ธ ๐ธ ๐ ๏ธ, โซ๏ธ ๐ ๐ผ ๐ ๐ ๐ช ๐ฎ ๐ ๐ ๐ ๐. |
|||
|
|||
**FastAPI** ๐ ๐ช ๐งฐ ๐ ๐ ๐ธ โช ๐ง ๐ ๐ช. |
|||
|
|||
!!! info |
|||
๐ฅ ๐ ๐ โช๏ธโก๏ธ ๐บ, ๐ ๐ ๐ ๐บ ๐. |
|||
|
|||
## ๐ผ ๐ ๐ |
|||
|
|||
โก๏ธ ๐ฌ ๐ โ๏ธ ๐ ๐ ๐ ๐: |
|||
|
|||
``` |
|||
. |
|||
โโโ app |
|||
โย ย โโโ __init__.py |
|||
โย ย โโโ main.py |
|||
โย ย โโโ dependencies.py |
|||
โย ย โโโ routers |
|||
โย ย โ โโโ __init__.py |
|||
โย ย โ โโโ items.py |
|||
โย ย โ โโโ users.py |
|||
โย ย โโโ internal |
|||
โย ย โโโ __init__.py |
|||
โย ย โโโ admin.py |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ค ๐ `__init__.py` ๐: 1๏ธโฃ ๐ ๐ โ๏ธ ๐. |
|||
|
|||
๐ โซ๏ธโ โ ๐ญ ๐ โช๏ธโก๏ธ 1๏ธโฃ ๐ ๐ โ1๏ธโฃ. |
|||
|
|||
๐ผ, `app/main.py` ๐ ๐ช โ๏ธ โธ ๐: |
|||
|
|||
``` |
|||
from app.routers import items |
|||
``` |
|||
|
|||
* `app` ๐ ๐ ๐. & โซ๏ธ โ๏ธ ๐ ๐ `app/__init__.py`, โซ๏ธ "๐ ๐ฆ" (๐ "๐ ๐น"): `app`. |
|||
* โซ๏ธ ๐ `app/main.py` ๐. โซ๏ธ ๐ ๐ ๐ฆ (๐ โฎ๏ธ ๐ `__init__.py`), โซ๏ธ "๐น" ๐ ๐ฆ: `app.main`. |
|||
* ๐ค `app/dependencies.py` ๐, ๐ `app/main.py`, โซ๏ธ "๐น": `app.dependencies`. |
|||
* ๐ค ๐ `app/routers/` โฎ๏ธ โ1๏ธโฃ ๐ `__init__.py`, โซ๏ธ "๐ ๐ฆ": `app.routers`. |
|||
* ๐ `app/routers/items.py` ๐ ๐ฆ, `app/routers/`,, โซ๏ธ ๐: `app.routers.items`. |
|||
* ๐ โฎ๏ธ `app/routers/users.py`, โซ๏ธ โ1๏ธโฃ ๐: `app.routers.users`. |
|||
* ๐ค ๐ `app/internal/` โฎ๏ธ โ1๏ธโฃ ๐ `__init__.py`, โซ๏ธ โ1๏ธโฃ "๐ ๐ฆ": `app.internal`. |
|||
* & ๐ `app/internal/admin.py` โ1๏ธโฃ ๐: `app.internal.admin`. |
|||
|
|||
<img src="/img/tutorial/bigger-applications/package.svg"> |
|||
|
|||
๐ ๐ ๐ โฎ๏ธ ๐ค: |
|||
|
|||
``` |
|||
. |
|||
โโโ app # "app" is a Python package |
|||
โย ย โโโ __init__.py # this file makes "app" a "Python package" |
|||
โย ย โโโ main.py # "main" module, e.g. import app.main |
|||
โย ย โโโ dependencies.py # "dependencies" module, e.g. import app.dependencies |
|||
โย ย โโโ routers # "routers" is a "Python subpackage" |
|||
โย ย โ โโโ __init__.py # makes "routers" a "Python subpackage" |
|||
โย ย โ โโโ items.py # "items" submodule, e.g. import app.routers.items |
|||
โย ย โ โโโ users.py # "users" submodule, e.g. import app.routers.users |
|||
โย ย โโโ internal # "internal" is a "Python subpackage" |
|||
โย ย โโโ __init__.py # makes "internal" a "Python subpackage" |
|||
โย ย โโโ admin.py # "admin" submodule, e.g. import app.internal.admin |
|||
``` |
|||
|
|||
## `APIRouter` |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ก ๐ ๐ฉโ๐ป ๐ `/app/routers/users.py`. |
|||
|
|||
๐ ๐ โ๏ธ *โก ๐ ๏ธ* ๐ ๐ ๐ฉโ๐ป ๐ฝ โช๏ธโก๏ธ ๐ ๐, ๐ง โซ๏ธ ๐. |
|||
|
|||
โ๏ธ โซ๏ธ ๐ ๐ **FastAPI** ๐ธ/๐ธ ๐ ๏ธ (โซ๏ธ ๐ ๐ "๐ ๐ฆ"). |
|||
|
|||
๐ ๐ช โ *โก ๐ ๏ธ* ๐ ๐น โ๏ธ `APIRouter`. |
|||
|
|||
### ๐ `APIRouter` |
|||
|
|||
๐ ๐ โซ๏ธ & โ "๐" ๐ ๐ ๐ ๐ โฎ๏ธ ๐ `FastAPI`: |
|||
|
|||
```Python hl_lines="1 3" |
|||
{!../../../docs_src/bigger_applications/app/routers/users.py!} |
|||
``` |
|||
|
|||
### *โก ๐ ๏ธ* โฎ๏ธ `APIRouter` |
|||
|
|||
& โคด๏ธ ๐ โ๏ธ โซ๏ธ ๐ฃ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
โ๏ธ โซ๏ธ ๐ ๐ ๐ ๐ โ๏ธ `FastAPI` ๐: |
|||
|
|||
```Python hl_lines="6 11 16" |
|||
{!../../../docs_src/bigger_applications/app/routers/users.py!} |
|||
``` |
|||
|
|||
๐ ๐ช ๐ญ `APIRouter` "๐ฉ `FastAPI`" ๐. |
|||
|
|||
๐ ๐ ๐ ๐โ๐ฆบ. |
|||
|
|||
๐ ๐ `parameters`, `responses`, `dependencies`, `tags`, โ๏ธ. |
|||
|
|||
!!! tip |
|||
๐ ๐ผ, ๐ข ๐ค `router`, โ๏ธ ๐ ๐ช ๐ โซ๏ธ ๐ ๐ ๐. |
|||
|
|||
๐ฅ ๐ ๐ ๐ `APIRouter` ๐ `FastAPI` ๐ฑ, โ๏ธ ๐ฅ, โก๏ธ โ
๐ & โ1๏ธโฃ `APIRouter`. |
|||
|
|||
## ๐ |
|||
|
|||
๐ฅ ๐ ๐ ๐ฅ ๐ ๐ช ๐ โ๏ธ ๐ ๐ฅ ๐ธ. |
|||
|
|||
๐ฅ ๐ฎ ๐ซ ๐ซ ๐ `dependencies` ๐น (`app/dependencies.py`). |
|||
|
|||
๐ฅ ๐ ๐ โ๏ธ ๐
๐ โ ๐ `X-Token` ๐: |
|||
|
|||
```Python hl_lines="1 4-6" |
|||
{!../../../docs_src/bigger_applications/app/dependencies.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ฅ โ๏ธ ๐ญ ๐ ๐ ๐ ๐ผ. |
|||
|
|||
โ๏ธ ๐ฐ ๐ผ ๐ ๐ ๐ค ๐ ๐ โ๏ธ ๐ ๏ธ [๐โโ ๐](./security/index.md){.internal-link target=_blank}. |
|||
|
|||
## โ1๏ธโฃ ๐น โฎ๏ธ `APIRouter` |
|||
|
|||
โก๏ธ ๐ฌ ๐ โ๏ธ ๐ ๐ก ๐ "๐ฌ" โช๏ธโก๏ธ ๐ ๐ธ ๐น `app/routers/items.py`. |
|||
|
|||
๐ โ๏ธ *โก ๐ ๏ธ* : |
|||
|
|||
* `/items/` |
|||
* `/items/{item_id}` |
|||
|
|||
โซ๏ธ ๐ ๐ ๐ โฎ๏ธ `app/routers/users.py`. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ & ๐ ๐ ๐. |
|||
|
|||
๐ฅ ๐ญ ๐ *โก ๐ ๏ธ* ๐ ๐น โ๏ธ ๐: |
|||
|
|||
* โก `prefix`: `/items`. |
|||
* `tags`: (1๏ธโฃ ๐: `items`). |
|||
* โ `responses`. |
|||
* `dependencies`: ๐ซ ๐ ๐ช ๐ `X-Token` ๐ ๐ฅ โ. |
|||
|
|||
, โฉ๏ธ โ ๐ ๐ ๐ *โก ๐ ๏ธ*, ๐ฅ ๐ช ๐ฎ โซ๏ธ `APIRouter`. |
|||
|
|||
```Python hl_lines="5-10 16 21" |
|||
{!../../../docs_src/bigger_applications/app/routers/items.py!} |
|||
``` |
|||
|
|||
โก ๐ *โก ๐ ๏ธ* โ๏ธ โถ๏ธ โฎ๏ธ `/`, ๐: |
|||
|
|||
```Python hl_lines="1" |
|||
@router.get("/{item_id}") |
|||
async def read_item(item_id: str): |
|||
... |
|||
``` |
|||
|
|||
...๐ก ๐ ๐ซ ๐ ๐ `/`. |
|||
|
|||
, ๐ก ๐ ๐ผ `/items`. |
|||
|
|||
๐ฅ ๐ช ๐ฎ ๐ `tags` & โ `responses` ๐ ๐ โ ๐ *โก ๐ ๏ธ* ๐ ๐ ๐ป. |
|||
|
|||
& ๐ฅ ๐ช ๐ฎ ๐ `dependencies` ๐ ๐ ๐ฎ ๐ *โก ๐ ๏ธ* ๐ป & ๐ ๐ ๏ธ/โ ๐ ๐จ โ ๐ซ. |
|||
|
|||
!!! tip |
|||
๐ ๐, ๐
๐ [๐ *โก ๐ ๏ธ ๐จโ๐จ*](dependencies/dependencies-in-path-operation-decorators.md){.internal-link target=_blank}, ๐
โโ ๐ฒ ๐ ๐ถโโ๏ธ ๐ *โก ๐ ๏ธ ๐ข*. |
|||
|
|||
๐ ๐ ๐ ๐ฌ โก ๐: |
|||
|
|||
* `/items/` |
|||
* `/items/{item_id}` |
|||
|
|||
...๐ฅ ๐ฏ. |
|||
|
|||
* ๐ซ ๐ โข โฎ๏ธ ๐ ๐ ๐ ๐ ๐ ๐ป `"items"`. |
|||
* ๐ซ "๐" โด๏ธ โ ๐ง ๐ ๐งพ โ๏ธ (โ๏ธ ๐). |
|||
* ๐ ๐ซ ๐ ๐ ๐ `responses`. |
|||
* ๐ ๐ซ *โก ๐ ๏ธ* ๐ โ๏ธ ๐ `dependencies` ๐ฌ/๐ ๏ธ โญ ๐ซ. |
|||
* ๐ฅ ๐ ๐ฃ ๐ ๐ฏ *โก ๐ ๏ธ*, **๐ซ ๐ ๐ ๏ธ ๐โโ๏ธ**. |
|||
* ๐ป ๐ ๐ ๏ธ ๐ฅ, โคด๏ธ [`dependencies` ๐จโ๐จ](dependencies/dependencies-in-path-operation-decorators.md){.internal-link target=_blank}, & โคด๏ธ ๐ ๐ข ๐. |
|||
* ๐ ๐ช ๐ฎ [`Security` ๐ โฎ๏ธ `scopes`](../advanced/security/oauth2-scopes.md){.internal-link target=_blank}. |
|||
|
|||
!!! tip |
|||
โ๏ธ `dependencies` `APIRouter` ๐ช โ๏ธ, ๐ผ, ๐ ๐ค ๐ ๐ช *โก ๐ ๏ธ*. ๐ฅ ๐ ๐ซ ๐ฎ ๐ฆ ๐ 1๏ธโฃ ๐ซ. |
|||
|
|||
!!! check |
|||
`prefix`, `tags`, `responses`, & `dependencies` ๐ข (๐ ๐ ๐ผ) โ โช๏ธโก๏ธ **FastAPI** โน ๐ โ ๐ โ. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ ๐ ๐จโโคโ๐จ ๐น `app.routers.items`, ๐ `app/routers/items.py`. |
|||
|
|||
& ๐ฅ ๐ช ๐ค ๐ ๐ข โช๏ธโก๏ธ ๐น `app.dependencies`, ๐ `app/dependencies.py`. |
|||
|
|||
๐ฅ โ๏ธ โ ๐ โฎ๏ธ `..` ๐: |
|||
|
|||
```Python hl_lines="3" |
|||
{!../../../docs_src/bigger_applications/app/routers/items.py!} |
|||
``` |
|||
|
|||
#### โ โ ๐ ๐ท |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ญ ๐ โ ๐ ๐ท, ๐ฃ โญ ๐ ๐. |
|||
|
|||
๐ โฃ `.`, ๐: |
|||
|
|||
```Python |
|||
from .dependencies import get_token_header |
|||
``` |
|||
|
|||
๐ โ: |
|||
|
|||
* โถ๏ธ ๐ ๐ฆ ๐ ๐ ๐น (๐ `app/routers/items.py`) ๐ (๐ `app/routers/`)... |
|||
* ๐ ๐น `dependencies` (๐ฝ ๐ `app/routers/dependencies.py`)... |
|||
* & โช๏ธโก๏ธ โซ๏ธ, ๐ ๐ข `get_token_header`. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ซ ๐, ๐ ๐ ๐ `app/dependencies.py`. |
|||
|
|||
๐ญ โ ๐ ๐ฑ/๐ ๐ ๐ ๐: |
|||
|
|||
<img src="/img/tutorial/bigger-applications/package.svg"> |
|||
|
|||
--- |
|||
|
|||
2๏ธโฃ โฃ `..`, ๐: |
|||
|
|||
```Python |
|||
from ..dependencies import get_token_header |
|||
``` |
|||
|
|||
โ: |
|||
|
|||
* โถ๏ธ ๐ ๐ฆ ๐ ๐ ๐น (๐ `app/routers/items.py`) ๐ (๐ `app/routers/`)... |
|||
* ๐ถ ๐ช ๐ฆ (๐ `app/`)... |
|||
* & ๐ค, ๐ ๐น `dependencies` (๐ `app/dependencies.py`)... |
|||
* & โช๏ธโก๏ธ โซ๏ธ, ๐ ๐ข `get_token_header`. |
|||
|
|||
๐ ๐ท โ โ ๐ถ |
|||
|
|||
--- |
|||
|
|||
๐ ๐, ๐ฅ ๐ฅ โ๏ธ โ๏ธ 3๏ธโฃ โฃ `...`, ๐: |
|||
|
|||
```Python |
|||
from ...dependencies import get_token_header |
|||
``` |
|||
|
|||
that ๐ โ: |
|||
|
|||
* โถ๏ธ ๐ ๐ฆ ๐ ๐ ๐น (๐ `app/routers/items.py`) ๐ (๐ `app/routers/`)... |
|||
* ๐ถ ๐ช ๐ฆ (๐ `app/`)... |
|||
* โคด๏ธ ๐ถ ๐ช ๐ ๐ฆ (๐ค ๐
โโ ๐ช ๐ฆ, `app` ๐ ๐ ๐ถ)... |
|||
* & ๐ค, ๐ ๐น `dependencies` (๐ `app/dependencies.py`)... |
|||
* & โช๏ธโก๏ธ โซ๏ธ, ๐ ๐ข `get_token_header`. |
|||
|
|||
๐ ๐ ๐ ๐ฆ ๐ `app/`, โฎ๏ธ ๐ฎ ๐ ๐ `__init__.py`, โ๏ธ. โ๏ธ ๐ฅ ๐ซ โ๏ธ ๐. , ๐ ๐ ๐ฎ โ ๐ ๐ผ. ๐ถ |
|||
|
|||
โ๏ธ ๐ ๐ ๐ญ โ โซ๏ธ ๐ท, ๐ ๐ช โ๏ธ โ ๐ ๐ ๐ ๐ฑ ๐
โโ ๐ค โ ๐ ๐ซ. ๐ถ |
|||
|
|||
### ๐ฎ ๐ `tags`, `responses`, & `dependencies` |
|||
|
|||
๐ฅ ๐ซ โ ๐ก `/items` ๐ซ `tags=["items"]` ๐ *โก ๐ ๏ธ* โฉ๏ธ ๐ฅ ๐ฎ ๐ซ `APIRouter`. |
|||
|
|||
โ๏ธ ๐ฅ ๐ช ๐ฎ _๐
_ `tags` ๐ ๐ โ ๐ฏ *โก ๐ ๏ธ*, & โ `responses` ๐ฏ ๐ *โก ๐ ๏ธ*: |
|||
|
|||
```Python hl_lines="30-31" |
|||
{!../../../docs_src/bigger_applications/app/routers/items.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ โก ๐ ๏ธ ๐ โ๏ธ ๐ ๐: `["items", "custom"]`. |
|||
|
|||
& โซ๏ธ ๐ โ๏ธ ๐ฏโโ๏ธ ๐จ ๐งพ, 1๏ธโฃ `404` & 1๏ธโฃ `403`. |
|||
|
|||
## ๐ `FastAPI` |
|||
|
|||
๐, โก๏ธ ๐ ๐น `app/main.py`. |
|||
|
|||
๐ฅ ๐โ ๐ ๐ & โ๏ธ ๐ `FastAPI`. |
|||
|
|||
๐ ๐ ๐ ๐ ๐ ๐ธ ๐ ๐ ๐ ๐ฏโโ๏ธ. |
|||
|
|||
& ๐ ๐ โ ๐ ๐ ๐ ๐ฎ ๐ ๐ฏ ๐น, ๐ ๐ ๐ ๐
. |
|||
|
|||
### ๐ `FastAPI` |
|||
|
|||
๐ ๐ & โ `FastAPI` ๐ ๐. |
|||
|
|||
& ๐ฅ ๐ช ๐ฃ [๐ ๐](dependencies/global-dependencies.md){.internal-link target=_blank} ๐ ๐ ๐ โฎ๏ธ ๐ ๐ `APIRouter`: |
|||
|
|||
```Python hl_lines="1 3 7" |
|||
{!../../../docs_src/bigger_applications/app/main.py!} |
|||
``` |
|||
|
|||
### ๐ `APIRouter` |
|||
|
|||
๐ ๐ฅ ๐ ๐ ๐ ๐ โ๏ธ `APIRouter`โ: |
|||
|
|||
```Python hl_lines="5" |
|||
{!../../../docs_src/bigger_applications/app/main.py!} |
|||
``` |
|||
|
|||
๐ `app/routers/users.py` & `app/routers/items.py` ๐ ๐ ๐ ๐ ๐ ๐ฆ `app`, ๐ฅ ๐ช โ๏ธ ๐ โฃ `.` ๐ ๐ซ โ๏ธ "โ ๐". |
|||
|
|||
### โ ๐ญ ๐ท |
|||
|
|||
๐: |
|||
|
|||
```Python |
|||
from .routers import items, users |
|||
``` |
|||
|
|||
โ: |
|||
|
|||
* โถ๏ธ ๐ ๐ฆ ๐ ๐ ๐น (๐ `app/main.py`) ๐ (๐ `app/`)... |
|||
* ๐ ๐ฆ `routers` (๐ `app/routers/`)... |
|||
* & โช๏ธโก๏ธ โซ๏ธ, ๐ ๐ `items` (๐ `app/routers/items.py`) & `users` (๐ `app/routers/users.py`)... |
|||
|
|||
๐น `items` ๐ โ๏ธ ๐ข `router` (`items.router`). ๐ ๐ 1๏ธโฃ ๐ฅ โ ๐ `app/routers/items.py`, โซ๏ธ `APIRouter` ๐. |
|||
|
|||
& โคด๏ธ ๐ฅ ๐ ๐น `users`. |
|||
|
|||
๐ฅ ๐ช ๐ ๐ซ ๐: |
|||
|
|||
```Python |
|||
from app.routers import items, users |
|||
``` |
|||
|
|||
!!! info |
|||
๐ฅ โฌ "โ ๐": |
|||
|
|||
```Python |
|||
from .routers import items, users |
|||
``` |
|||
|
|||
๐ฅ โฌ "๐ ๐": |
|||
|
|||
```Python |
|||
from app.routers import items, users |
|||
``` |
|||
|
|||
๐ก ๐
๐ ๐ ๐ฆ & ๐น, โ <a href="https://docs.python.org/3/tutorial/modules.html" class="external-link" target="_blank">๐ ๐ ๐งพ ๐ ๐น</a>. |
|||
|
|||
### โ ๐ ๐ฅ |
|||
|
|||
๐ฅ ๐ญ ๐ `items` ๐, โฉ๏ธ ๐ญ ๐ฎ ๐ข `router`. |
|||
|
|||
๐ โฉ๏ธ ๐ฅ โ๏ธ โ1๏ธโฃ ๐ข ๐ `router` ๐ `users`. |
|||
|
|||
๐ฅ ๐ฅ โ๏ธ ๐ 1๏ธโฃ โฎ๏ธ ๐, ๐: |
|||
|
|||
```Python |
|||
from .routers.items import router |
|||
from .routers.users import router |
|||
``` |
|||
|
|||
`router` โช๏ธโก๏ธ `users` ๐ ๐ 1๏ธโฃ โช๏ธโก๏ธ `items` & ๐ฅ ๐ซ๐ ๐ช โ๏ธ ๐ซ ๐ ๐ฐ. |
|||
|
|||
, ๐ช โ๏ธ ๐ฏโโ๏ธ ๐ซ ๐ ๐, ๐ฅ ๐ ๐ ๐: |
|||
|
|||
```Python hl_lines="4" |
|||
{!../../../docs_src/bigger_applications/app/main.py!} |
|||
``` |
|||
|
|||
### ๐ `APIRouter`โ `users` & `items` |
|||
|
|||
๐, โก๏ธ ๐ `router`โ โช๏ธโก๏ธ ๐ `users` & `items`: |
|||
|
|||
```Python hl_lines="10-11" |
|||
{!../../../docs_src/bigger_applications/app/main.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
`users.router` ๐ `APIRouter` ๐ ๐ `app/routers/users.py`. |
|||
|
|||
& `items.router` ๐ `APIRouter` ๐ ๐ `app/routers/items.py`. |
|||
|
|||
โฎ๏ธ `app.include_router()` ๐ฅ ๐ช ๐ฎ ๐ `APIRouter` ๐ `FastAPI` ๐ธ. |
|||
|
|||
โซ๏ธ ๐ ๐ ๐ ๐ฃ โช๏ธโก๏ธ ๐ ๐ป ๐ โซ๏ธ. |
|||
|
|||
!!! note "๐ก โน" |
|||
โซ๏ธ ๐ ๐ค ๐ โ *โก ๐ ๏ธ* ๐ *โก ๐ ๏ธ* ๐ ๐ฃ `APIRouter`. |
|||
|
|||
, โ
๐, โซ๏ธ ๐ ๐ค ๐ท ๐ฅ ๐ ๐ ๐ ๐ฑ. |
|||
|
|||
!!! check |
|||
๐ ๐ซ โ๏ธ ๐ ๐ ๐ญ ๐โ โ
๐ป. |
|||
|
|||
๐ ๐ โ โฒ & ๐ ๐ด ๐จ ๐ด. |
|||
|
|||
โซ๏ธ ๐ ๐ซ ๐ ๐ญ. ๐ถ |
|||
|
|||
### ๐ `APIRouter` โฎ๏ธ ๐ `prefix`, `tags`, `responses`, & `dependencies` |
|||
|
|||
๐, โก๏ธ ๐ ๐ ๐ข ๐ค ๐ `app/internal/admin.py` ๐. |
|||
|
|||
โซ๏ธ ๐ `APIRouter` โฎ๏ธ ๐ก *โก ๐ ๏ธ* ๐ ๐ ๐ข ๐ฐ ๐ ๐ ๐. |
|||
|
|||
๐ ๐ผ โซ๏ธ ๐ ๐ ๐
. โ๏ธ โก๏ธ ๐ฌ ๐ โฉ๏ธ โซ๏ธ ๐ฐ โฎ๏ธ ๐ ๐ ๐ข, ๐ฅ ๐ซ๐ ๐ โซ๏ธ & ๐ฎ `prefix`, `dependencies`, `tags`, โ๏ธ. ๐ `APIRouter`: |
|||
|
|||
```Python hl_lines="3" |
|||
{!../../../docs_src/bigger_applications/app/internal/admin.py!} |
|||
``` |
|||
|
|||
โ๏ธ ๐ฅ ๐ โ ๐ `prefix` ๐โ โ
`APIRouter` ๐ ๐ ๐ฎ *โก ๐ ๏ธ* โถ๏ธ โฎ๏ธ `/admin`, ๐ฅ ๐ ๐ โซ๏ธ โฎ๏ธ `dependencies` ๐ฅ โช โ๏ธ ๐ ๐, & ๐ฅ ๐ ๐ `tags` & `responses`. |
|||
|
|||
๐ฅ ๐ช ๐ฃ ๐ ๐ ๐ต โ๏ธ ๐ โฎ๏ธ `APIRouter` ๐ถโโ๏ธ ๐ ๐ข `app.include_router()`: |
|||
|
|||
```Python hl_lines="14-17" |
|||
{!../../../docs_src/bigger_applications/app/main.py!} |
|||
``` |
|||
|
|||
๐ ๐, โฎ๏ธ `APIRouter` ๐ ๐ง โ, ๐ฅ ๐ช ๐ฐ ๐ ๐ `app/internal/admin.py` ๐ โฎ๏ธ ๐ ๐ ๐ข. |
|||
|
|||
๐ ๐ ๐ ๐ฑ, ๐ *โก ๐ ๏ธ* โช๏ธโก๏ธ `admin` ๐น ๐ โ๏ธ: |
|||
|
|||
* ๐ก `/admin`. |
|||
* ๐ `admin`. |
|||
* ๐ `get_token_header`. |
|||
* ๐จ `418`. ๐ถ |
|||
|
|||
โ๏ธ ๐ ๐ ๐ด ๐ ๐ `APIRouter` ๐ ๐ฑ, ๐ซ ๐ ๐ ๐ ๐ โ๏ธ โซ๏ธ. |
|||
|
|||
, ๐ผ, ๐ ๐ ๐ช โ๏ธ ๐ `APIRouter` โฎ๏ธ ๐ ๐ค ๐ฉโ๐ฌ. |
|||
|
|||
### ๐ *โก ๐ ๏ธ* |
|||
|
|||
๐ฅ ๐ช ๐ฎ *โก ๐ ๏ธ* ๐ `FastAPI` ๐ฑ. |
|||
|
|||
๐ฅ ๐ฅ โซ๏ธ... ๐ฆ ๐ ๐ฅ ๐ช ๐คท: |
|||
|
|||
```Python hl_lines="21-23" |
|||
{!../../../docs_src/bigger_applications/app/main.py!} |
|||
``` |
|||
|
|||
& โซ๏ธ ๐ ๐ท โ, ๐ฏโโ๏ธ โฎ๏ธ ๐ ๐ *โก ๐ ๏ธ* ๐ฎ โฎ๏ธ `app.include_router()`. |
|||
|
|||
!!! info "๐ถ ๐ก โน" |
|||
**๐**: ๐ ๐ถ ๐ก โน ๐ ๐ ๐ฒ ๐ช **๐ถ**. |
|||
|
|||
--- |
|||
|
|||
`APIRouter`โ ๐ซ "๐ป", ๐ซ ๐ซ ๐ฝ โช๏ธโก๏ธ ๐ ๐ธ. |
|||
|
|||
๐ โฉ๏ธ ๐ฅ ๐ ๐ ๐ซ *โก ๐ ๏ธ* ๐ ๐ & ๐ฉโ๐ป ๐ข. |
|||
|
|||
๐ฅ ๐ซ๐ โ ๐ซ & "๐ป" ๐ซ โก ๐, *โก ๐ ๏ธ* "๐" (๐ค-โ), ๐ซ ๐ ๐. |
|||
|
|||
## โ
๐ง ๐ ๏ธ ๐ฉบ |
|||
|
|||
๐, ๐ `uvicorn`, โ๏ธ ๐น `app.main` & ๐ข `app`: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ uvicorn app.main:app --reload |
|||
|
|||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
& ๐ ๐ฉบ <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. |
|||
|
|||
๐ ๐ ๐ ๐ง ๐ ๏ธ ๐ฉบ, โ
โก โช๏ธโก๏ธ ๐ ๐, โ๏ธ โ โก (& ๐ก) & โ ๐: |
|||
|
|||
<img src="/img/tutorial/bigger-applications/image01.png"> |
|||
|
|||
## ๐ ๐ ๐ป ๐ ๐ฐ โฎ๏ธ ๐ `prefix` |
|||
|
|||
๐ ๐ช โ๏ธ `.include_router()` ๐ ๐ฐ โฎ๏ธ *๐* ๐ป โ๏ธ ๐ ๐ก. |
|||
|
|||
๐ ๐ช โ , ๐ผ, ๐ฆ ๐ ๐ ๏ธ ๐ฝ ๐ ๐ก, โ
`/api/v1` & `/api/latest`. |
|||
|
|||
๐ ๐ง โ๏ธ ๐ ๐ 5๏ธโฃ๐ ๐ซ ๐ค ๐ช, โ๏ธ โซ๏ธ ๐ค ๐ผ ๐. |
|||
|
|||
## ๐ `APIRouter` โ1๏ธโฃ |
|||
|
|||
๐ ๐ ๐ ๐ช ๐ `APIRouter` `FastAPI` ๐ธ, ๐ ๐ช ๐ `APIRouter` โ1๏ธโฃ `APIRouter` โ๏ธ: |
|||
|
|||
```Python |
|||
router.include_router(other_router) |
|||
``` |
|||
|
|||
โ ๐ญ ๐ โซ๏ธ โญ ๐ `router` `FastAPI` ๐ฑ, ๐ *โก ๐ ๏ธ* โช๏ธโก๏ธ `other_router` ๐. |
@ -0,0 +1,68 @@ |
|||
# ๐ช - ๐ |
|||
|
|||
๐ ๐ ๐ ๐ช ๐ฃ ๐ ๐ฌ & ๐ *โก ๐ ๏ธ ๐ข* ๐ข โฎ๏ธ `Query`, `Path` & `Body`, ๐ ๐ช ๐ฃ ๐ฌ & ๐ ๐ Pydantic ๐ท โ๏ธ Pydantic `Field`. |
|||
|
|||
## ๐ `Field` |
|||
|
|||
๐ฅ, ๐ โ๏ธ ๐ โซ๏ธ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="4" |
|||
{!> ../../../docs_src/body_fields/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="2" |
|||
{!> ../../../docs_src/body_fields/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
!!! warning |
|||
๐ ๐ `Field` ๐ ๐ โช๏ธโก๏ธ `pydantic`, ๐ซ โช๏ธโก๏ธ `fastapi` ๐ ๐ (`Query`, `Path`, `Body`, โ๏ธ). |
|||
|
|||
## ๐ฃ ๐ท ๐ข |
|||
|
|||
๐ ๐ช โคด๏ธ โ๏ธ `Field` โฎ๏ธ ๐ท ๐ข: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="11-14" |
|||
{!> ../../../docs_src/body_fields/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9-12" |
|||
{!> ../../../docs_src/body_fields/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
`Field` ๐ท ๐ ๐ `Query`, `Path` & `Body`, โซ๏ธ โ๏ธ ๐ ๐ ๐ข, โ๏ธ. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ค, `Query`, `Path` & ๐ ๐ ๐ ๐ โญ โ ๐ ๐ฟ โ `Param` ๐, โ โซ๏ธ ๐ฟ Pydantic `FieldInfo` ๐. |
|||
|
|||
& Pydantic `Field` ๐จ ๐ `FieldInfo` ๐. |
|||
|
|||
`Body` ๐จ ๐ ๐ฟ `FieldInfo` ๐. & ๐ค ๐ ๐ ๐ ๐ โช ๐ ๐ฟ `Body` ๐. |
|||
|
|||
๐ญ ๐ ๐โ ๐ ๐ `Query`, `Path`, & ๐ โช๏ธโก๏ธ `fastapi`, ๐ ๐ค ๐ข ๐ ๐จ ๐ ๐. |
|||
|
|||
!!! tip |
|||
๐ โ ๐ ๐ท ๐ข โฎ๏ธ ๐, ๐ข ๐ฒ & `Field` โ๏ธ ๐ ๐ *โก ๐ ๏ธ ๐ข* ๐ข, โฎ๏ธ `Field` โฉ๏ธ `Path`, `Query` & `Body`. |
|||
|
|||
## ๐ฎ โ โน |
|||
|
|||
๐ ๐ช ๐ฃ โ โน `Field`, `Query`, `Body`, โ๏ธ. & โซ๏ธ ๐ ๐ ๐ ๐ป ๐. |
|||
|
|||
๐ ๐ ๐ก ๐
๐ โ โ โน โช ๐ฉบ, ๐โ ๐ซ ๐ฃ ๐ผ. |
|||
|
|||
!!! warning |
|||
โ ๐ ๐ถโโ๏ธ `Field` ๐ ๐ ๐ ๐ ๐ ๐ ๐ธ. |
|||
๐ซ ๐ 5๏ธโฃ๐ ๐ซ ๐ฏ ๐ ๐ ๐ง, ๐ ๐งฐ, ๐ผ [๐ ๐ณ](https://validator.swagger.io/), 5๏ธโฃ๐ ๐ซ ๐ท โฎ๏ธ ๐ ๐ ๐. |
|||
|
|||
## ๐ |
|||
|
|||
๐ ๐ช โ๏ธ Pydantic `Field` ๐ฃ โ ๐ฌ & ๐ ๐ท ๐ข. |
|||
|
|||
๐ ๐ช โ๏ธ โ ๐จ๐ป โ ๐ถโโ๏ธ ๐ ๐ป ๐ ๐. |
@ -0,0 +1,213 @@ |
|||
# ๐ช - ๐ ๐ข |
|||
|
|||
๐ ๐ ๐ฅ โ๏ธ ๐ โ โ๏ธ `Path` & `Query`, โก๏ธ ๐ ๐
๐ง โ๏ธ ๐จ ๐ช ๐. |
|||
|
|||
## ๐ `Path`, `Query` & ๐ช ๐ข |
|||
|
|||
๐ฅ, โ๏ธ, ๐ ๐ช ๐ `Path`, `Query` & ๐จ ๐ช ๐ข ๐ โก & **FastAPI** ๐ ๐ญ โซ๏ธโ. |
|||
|
|||
& ๐ ๐ช ๐ฃ ๐ช ๐ข ๐ฆ, โ ๐ข `None`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="19-21" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="17-19" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
!!! note |
|||
๐ ๐, ๐ ๐ผ, `item` ๐ ๐ โ โช๏ธโก๏ธ ๐ช ๐ฆ. โซ๏ธ โ๏ธ `None` ๐ข ๐ฒ. |
|||
|
|||
## ๐ ๐ช ๐ข |
|||
|
|||
โฎ๏ธ ๐ผ, *โก ๐ ๏ธ* ๐ โ ๐ป ๐ช โฎ๏ธ ๐ข `Item`, ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"name": "Foo", |
|||
"description": "The pretender", |
|||
"price": 42.0, |
|||
"tax": 3.2 |
|||
} |
|||
``` |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ฃ ๐ ๐ช ๐ข, โ
`item` & `user`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="22" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="20" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, **FastAPI** ๐ ๐ ๐ ๐ค ๐
๐ 1๏ธโฃ ๐ช ๐ข ๐ข (2๏ธโฃ ๐ข ๐ Pydantic ๐ท). |
|||
|
|||
, โซ๏ธ ๐ โคด๏ธ โ๏ธ ๐ข ๐ ๐ (๐ ๐) ๐ช, & โ ๐ช ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"item": { |
|||
"name": "Foo", |
|||
"description": "The pretender", |
|||
"price": 42.0, |
|||
"tax": 3.2 |
|||
}, |
|||
"user": { |
|||
"username": "dave", |
|||
"full_name": "Dave Grohl" |
|||
} |
|||
} |
|||
``` |
|||
|
|||
!!! note |
|||
๐ ๐ โ๏ธ `item` ๐ฃ ๐ ๐ โญ, โซ๏ธ ๐ โ ๐ ๐ช โฎ๏ธ ๐ `item`. |
|||
|
|||
|
|||
**FastAPI** ๐ ๐ง ๐ ๏ธ โช๏ธโก๏ธ ๐จ, ๐ ๐ข `item` ๐จ โซ๏ธ ๐ฏ ๐ & ๐ `user`. |
|||
|
|||
โซ๏ธ ๐ ๐ญ ๐ฌ โ ๐ฝ, & ๐ ๐ โซ๏ธ ๐ ๐ ๐ ๐ & ๐ง ๐ฉบ. |
|||
|
|||
## โญ ๐ฒ ๐ช |
|||
|
|||
๐ ๐ ๐ค `Query` & `Path` ๐ฌ โ ๐ฝ ๐ข & โก ๐ข, **FastAPI** ๐ ๐ `Body`. |
|||
|
|||
๐ผ, โ โฎ๏ธ ๐ท, ๐ ๐ช ๐ญ ๐ ๐ ๐ โ๏ธ โ1๏ธโฃ ๐ `importance` ๐ ๐ช, ๐ฅ `item` & `user`. |
|||
|
|||
๐ฅ ๐ ๐ฃ โซ๏ธ, โฉ๏ธ โซ๏ธ โญ ๐ฒ, **FastAPI** ๐ ๐ค ๐ โซ๏ธ ๐ข ๐ข. |
|||
|
|||
โ๏ธ ๐ ๐ช ๐ก **FastAPI** ๐ฅ โซ๏ธ โ1๏ธโฃ ๐ช ๐ โ๏ธ `Body`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="22" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial003.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="20" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial003_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ, **FastAPI** ๐ โ ๐ช ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"item": { |
|||
"name": "Foo", |
|||
"description": "The pretender", |
|||
"price": 42.0, |
|||
"tax": 3.2 |
|||
}, |
|||
"user": { |
|||
"username": "dave", |
|||
"full_name": "Dave Grohl" |
|||
}, |
|||
"importance": 5 |
|||
} |
|||
``` |
|||
|
|||
๐, โซ๏ธ ๐ ๐ ๐ ๐, โ, ๐, โ๏ธ. |
|||
|
|||
## ๐ ๐ช = & ๐ข |
|||
|
|||
โ๏ธ, ๐ ๐ช ๐ฃ ๐ ๐ข ๐ข ๐โ ๐ ๐ช, ๐ ๐ ๐ช ๐ข. |
|||
|
|||
, ๐ข, โญ ๐ฒ ๐ฌ ๐ข ๐ข, ๐ ๐ซ โ๏ธ ๐ฏ ๐ฎ `Query`, ๐ ๐ช: |
|||
|
|||
```Python |
|||
q: Union[str, None] = None |
|||
``` |
|||
|
|||
โ๏ธ ๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐: |
|||
|
|||
```Python |
|||
q: str | None = None |
|||
``` |
|||
|
|||
๐ผ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="27" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial004.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="26" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial004_py310.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
`Body` โ๏ธ ๐ ๐ โ ๐ฌ & ๐ ๐ข `Query`,`Path` & ๐ ๐ ๐ ๐ โช. |
|||
|
|||
## โฏ ๐ ๐ช ๐ข |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ด โ๏ธ ๐ `item` ๐ช ๐ข โช๏ธโก๏ธ Pydantic ๐ท `Item`. |
|||
|
|||
๐ข, **FastAPI** ๐ โคด๏ธ โ ๐ฎ ๐ช ๐. |
|||
|
|||
โ๏ธ ๐ฅ ๐ ๐ โซ๏ธ โ ๐ป โฎ๏ธ ๐ `item` & ๐ โซ๏ธ ๐ท ๐, โซ๏ธ ๐จ ๐โ ๐ ๐ฃ โ ๐ช ๐ข, ๐ ๐ช โ๏ธ ๐ `Body` ๐ข `embed`: |
|||
|
|||
```Python |
|||
item: Item = Body(embed=True) |
|||
``` |
|||
|
|||
: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="17" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial005.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="15" |
|||
{!> ../../../docs_src/body_multiple_params/tutorial005_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ผ **FastAPI** ๐ โ ๐ช ๐: |
|||
|
|||
```JSON hl_lines="2" |
|||
{ |
|||
"item": { |
|||
"name": "Foo", |
|||
"description": "The pretender", |
|||
"price": 42.0, |
|||
"tax": 3.2 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
โฉ๏ธ: |
|||
|
|||
```JSON |
|||
{ |
|||
"name": "Foo", |
|||
"description": "The pretender", |
|||
"price": 42.0, |
|||
"tax": 3.2 |
|||
} |
|||
``` |
|||
|
|||
## ๐ |
|||
|
|||
๐ ๐ช ๐ฎ ๐ ๐ช ๐ข ๐ *โก ๐ ๏ธ ๐ข*, โ๏ธ ๐จ ๐ช ๐ด โ๏ธ ๐ ๐ช. |
|||
|
|||
โ๏ธ **FastAPI** ๐ ๐ต โซ๏ธ, ๐ค ๐ โ ๐ ๐ ๐ข, & โ & ๐ โ ๐ *โก ๐ ๏ธ*. |
|||
|
|||
๐ ๐ช ๐ฃ โญ ๐ฒ ๐จ ๐ ๐ช. |
|||
|
|||
& ๐ ๐ช ๐ก **FastAPI** โฏ ๐ช ๐ ๐โ ๐ค ๐ด ๐ ๐ข ๐ฃ. |
@ -0,0 +1,382 @@ |
|||
# ๐ช - ๐ ๐ท |
|||
|
|||
โฎ๏ธ **FastAPI**, ๐ ๐ช ๐ฌ, โ, ๐, & โ๏ธ ๐ฒ ๐ ๐ฆ ๐ท (๐ Pydantic). |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ ๐ช ๐ฌ ๐ข ๐พ. ๐ผ, ๐ `list`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="14" |
|||
{!> ../../../docs_src/body_nested_models/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="12" |
|||
{!> ../../../docs_src/body_nested_models/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ โ `tags` ๐, ๐ โซ๏ธ ๐ซ ๐ฃ ๐ ๐ฃ ๐. |
|||
|
|||
## ๐ ๐ โฎ๏ธ ๐ ๐ข |
|||
|
|||
โ๏ธ ๐ โ๏ธ ๐ฏ ๐ ๐ฃ ๐ โฎ๏ธ ๐ ๐, โ๏ธ "๐ ๐ข": |
|||
|
|||
### ๐ โจ `List` |
|||
|
|||
๐ 3๏ธโฃ.9๏ธโฃ & ๐ ๐ ๐ช โ๏ธ ๐ฉ `list` ๐ฃ ๐ซ ๐ โ ๐ฅ ๐ ๐ ๐. ๐ถ |
|||
|
|||
โ๏ธ ๐ โฌ โญ 3๏ธโฃ.9๏ธโฃ (3๏ธโฃ.6๏ธโฃ & ๐), ๐ ๐ฅ ๐ช ๐ `List` โช๏ธโก๏ธ ๐ฉ ๐ `typing` ๐น: |
|||
|
|||
```Python hl_lines="1" |
|||
{!> ../../../docs_src/body_nested_models/tutorial002.py!} |
|||
``` |
|||
|
|||
### ๐ฃ `list` โฎ๏ธ ๐ ๐ข |
|||
|
|||
๐ฃ ๐ ๐ โ๏ธ ๐ ๐ข (๐ ๐), ๐ `list`, `dict`, `tuple`: |
|||
|
|||
* ๐ฅ ๐ ๐ โฌ ๐
๐ 3๏ธโฃ.9๏ธโฃ, ๐ ๐ซ ๐ โฌ โช๏ธโก๏ธ `typing` ๐น |
|||
* ๐ถโโ๏ธ ๐ ๐(โ) "๐ ๐ข" โ๏ธ โฌ ๐: `[` & `]` |
|||
|
|||
๐ 3๏ธโฃ.9๏ธโฃ โซ๏ธ ๐: |
|||
|
|||
```Python |
|||
my_list: list[str] |
|||
``` |
|||
|
|||
โฌ ๐ โญ 3๏ธโฃ.9๏ธโฃ, โซ๏ธ ๐: |
|||
|
|||
```Python |
|||
from typing import List |
|||
|
|||
my_list: List[str] |
|||
``` |
|||
|
|||
๐ ๐ ๐ฉ ๐ โ ๐ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ฉ โ ๐ท ๐ข โฎ๏ธ ๐ ๐. |
|||
|
|||
, ๐ ๐ผ, ๐ฅ ๐ช โ `tags` ๐ฏ "๐ ๐ป": |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="14" |
|||
{!> ../../../docs_src/body_nested_models/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="14" |
|||
{!> ../../../docs_src/body_nested_models/tutorial002_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="12" |
|||
{!> ../../../docs_src/body_nested_models/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
## โ ๐ |
|||
|
|||
โ๏ธ โคด๏ธ ๐ฅ ๐ญ ๐ โซ๏ธ, & ๐ค ๐ ๐ ๐ซ๐ ๐ซ ๐, ๐ซ ๐ ๐ฒ ๐ ๐ป. |
|||
|
|||
& ๐ โ๏ธ ๐ ๐ฝ ๐ โ ๐ ๐ฌ, `set`. |
|||
|
|||
โคด๏ธ ๐ฅ ๐ช ๐ฃ `tags` โ ๐ป: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1 14" |
|||
{!> ../../../docs_src/body_nested_models/tutorial003.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="14" |
|||
{!> ../../../docs_src/body_nested_models/tutorial003_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="12" |
|||
{!> ../../../docs_src/body_nested_models/tutorial003_py310.py!} |
|||
``` |
|||
|
|||
โฎ๏ธ ๐, ๐ฅ ๐ ๐จ ๐จ โฎ๏ธ โ ๐, โซ๏ธ ๐ ๐ โ ๐ ๐ฌ. |
|||
|
|||
& ๐โ ๐ ๐ข ๐ ๐, ๐ฅ โน โ๏ธ โ, โซ๏ธ ๐ ๐ข โ ๐ ๐ฌ. |
|||
|
|||
& โซ๏ธ ๐ โ / ๐ โก๏ธ ๐โโ๏ธ. |
|||
|
|||
## ๐ฆ ๐ท |
|||
|
|||
๐ ๐ข Pydantic ๐ท โ๏ธ ๐. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ช โซ๏ธ โ1๏ธโฃ Pydantic ๐ท. |
|||
|
|||
, ๐ ๐ช ๐ฃ ๐ ๐ฆ ๐ป "๐" โฎ๏ธ ๐ฏ ๐ข ๐, ๐ & ๐ฌ. |
|||
|
|||
๐ ๐, ๐ฒ ๐ฆ. |
|||
|
|||
### ๐ฌ ๐ |
|||
|
|||
๐ผ, ๐ฅ ๐ช ๐ฌ `Image` ๐ท: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9-11" |
|||
{!> ../../../docs_src/body_nested_models/tutorial004.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9-11" |
|||
{!> ../../../docs_src/body_nested_models/tutorial004_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="7-9" |
|||
{!> ../../../docs_src/body_nested_models/tutorial004_py310.py!} |
|||
``` |
|||
|
|||
### โ๏ธ ๐ ๐ |
|||
|
|||
& โคด๏ธ ๐ฅ ๐ช โ๏ธ โซ๏ธ ๐ ๐ข: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="20" |
|||
{!> ../../../docs_src/body_nested_models/tutorial004.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="20" |
|||
{!> ../../../docs_src/body_nested_models/tutorial004_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="18" |
|||
{!> ../../../docs_src/body_nested_models/tutorial004_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ โ ๐ **FastAPI** ๐ โ ๐ช ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"name": "Foo", |
|||
"description": "The pretender", |
|||
"price": 42.0, |
|||
"tax": 3.2, |
|||
"tags": ["rock", "metal", "bar"], |
|||
"image": { |
|||
"url": "http://example.com/baz.jpg", |
|||
"name": "The Foo live" |
|||
} |
|||
} |
|||
``` |
|||
|
|||
๐, ๐คธ ๐ ๐, โฎ๏ธ **FastAPI** ๐ ๐ค: |
|||
|
|||
* ๐จโ๐จ ๐โ๐ฆบ (๐ ๏ธ, โ๏ธ), ๐ฆ ๐ท |
|||
* ๐ฝ ๐ ๏ธ |
|||
* ๐ฝ ๐ฌ |
|||
* ๐ง ๐งพ |
|||
|
|||
## ๐ ๐ & ๐ฌ |
|||
|
|||
โ๏ธ โช๏ธโก๏ธ ๐ โญ ๐ ๐ `str`, `int`, `float`, โ๏ธ. ๐ ๐ช โ๏ธ ๐
๐ โญ ๐ ๐ ๐ โช๏ธโก๏ธ `str`. |
|||
|
|||
๐ ๐ ๐ ๐ โ๏ธ, ๐ ๐ฉบ <a href="https://pydantic-docs.helpmanual.io/usage/types/" class="external-link" target="_blank">Pydantic ๐ ๐</a>. ๐ ๐ ๐ ๐ผ โญ ๐. |
|||
|
|||
๐ผ, `Image` ๐ท ๐ฅ โ๏ธ `url` ๐, ๐ฅ ๐ช ๐ฃ โซ๏ธ โฉ๏ธ `str`, Pydantic `HttpUrl`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="4 10" |
|||
{!> ../../../docs_src/body_nested_models/tutorial005.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="4 10" |
|||
{!> ../../../docs_src/body_nested_models/tutorial005_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="2 8" |
|||
{!> ../../../docs_src/body_nested_models/tutorial005_py310.py!} |
|||
``` |
|||
|
|||
๐ป ๐ โ
โ ๐, & ๐ ๐ป ๐ / ๐ โ
. |
|||
|
|||
## ๐ข โฎ๏ธ ๐ ๐ |
|||
|
|||
๐ ๐ช โ๏ธ Pydantic ๐ท ๐พ `list`, `set`, โ๏ธ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="20" |
|||
{!> ../../../docs_src/body_nested_models/tutorial006.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="20" |
|||
{!> ../../../docs_src/body_nested_models/tutorial006_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="18" |
|||
{!> ../../../docs_src/body_nested_models/tutorial006_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ โ (๐, โ, ๐, โ๏ธ) ๐ป ๐ช ๐: |
|||
|
|||
```JSON hl_lines="11" |
|||
{ |
|||
"name": "Foo", |
|||
"description": "The pretender", |
|||
"price": 42.0, |
|||
"tax": 3.2, |
|||
"tags": [ |
|||
"rock", |
|||
"metal", |
|||
"bar" |
|||
], |
|||
"images": [ |
|||
{ |
|||
"url": "http://example.com/baz.jpg", |
|||
"name": "The Foo live" |
|||
}, |
|||
{ |
|||
"url": "http://example.com/dave.jpg", |
|||
"name": "The Baz" |
|||
} |
|||
] |
|||
} |
|||
``` |
|||
|
|||
!!! info |
|||
๐ โ `images` ๐ ๐ โ๏ธ ๐ ๐ผ ๐. |
|||
|
|||
## ๐ ๐ฆ ๐ท |
|||
|
|||
๐ ๐ช ๐ฌ ๐ฒ ๐ ๐ฆ ๐ท: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9 14 20 23 27" |
|||
{!> ../../../docs_src/body_nested_models/tutorial007.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9 14 20 23 27" |
|||
{!> ../../../docs_src/body_nested_models/tutorial007_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="7 12 18 21 25" |
|||
{!> ../../../docs_src/body_nested_models/tutorial007_py310.py!} |
|||
``` |
|||
|
|||
!!! info |
|||
๐ โ `Offer` โ๏ธ ๐ `Item`โ, โ ๐ โ๏ธ ๐ฆ ๐ `Image`โ |
|||
|
|||
## ๐ช ๐ ๐ |
|||
|
|||
๐ฅ ๐ ๐ ๐ฒ ๐ป ๐ช ๐ โ ๐ป `array` (๐ `list`), ๐ ๐ช ๐ฃ ๐ ๐ข ๐ข, ๐ Pydantic ๐ท: |
|||
|
|||
```Python |
|||
images: List[Image] |
|||
``` |
|||
|
|||
โ๏ธ ๐ 3๏ธโฃ.9๏ธโฃ & ๐: |
|||
|
|||
```Python |
|||
images: list[Image] |
|||
``` |
|||
|
|||
: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="15" |
|||
{!> ../../../docs_src/body_nested_models/tutorial008.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="13" |
|||
{!> ../../../docs_src/body_nested_models/tutorial008_py39.py!} |
|||
``` |
|||
|
|||
## ๐จโ๐จ ๐โ๐ฆบ ๐ |
|||
|
|||
& ๐ ๐ค ๐จโ๐จ ๐โ๐ฆบ ๐. |
|||
|
|||
๐ฌ ๐ ๐: |
|||
|
|||
<img src="/img/tutorial/body-nested-models/image01.png"> |
|||
|
|||
๐ ๐ซ ๐ซ ๐ค ๐ ๐ ๐จโ๐จ ๐โ๐ฆบ ๐ฅ ๐ ๐ท ๐ โฎ๏ธ `dict` โฉ๏ธ Pydantic ๐ท. |
|||
|
|||
โ๏ธ ๐ ๐ซ โ๏ธ ๐ ๐ ๐ซ ๐ฏโโ๏ธ, ๐จ #๏ธโฃ ๐ ๐ & ๐ ๐ข ๐ ๐ ๐ป ๐โโ๏ธ. |
|||
|
|||
## ๐ช โ `dict`โ |
|||
|
|||
๐ ๐ช ๐ฃ ๐ช `dict` โฎ๏ธ ๐ ๐ & ๐ฒ ๐ ๐. |
|||
|
|||
๐ต โ๏ธ ๐ญ โช โซ๏ธโ โ ๐/๐ข ๐ (๐ ๐ผ โฎ๏ธ Pydantic ๐ท). |
|||
|
|||
๐ ๐ โ ๐ฅ ๐ ๐ ๐จ ๐ ๐ ๐ ๐ซ โช ๐ญ. |
|||
|
|||
--- |
|||
|
|||
๐ โ ๐ผ ๐โ ๐ ๐ โ๏ธ ๐ ๐ ๐, โ
`int`. |
|||
|
|||
๐ โซ๏ธโ ๐ฅ ๐ ๐ ๐ฅ. |
|||
|
|||
๐ ๐ผ, ๐ ๐ ๐ซ ๐ `dict` ๐ โซ๏ธ โ๏ธ `int` ๐ โฎ๏ธ `float` ๐ฒ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9" |
|||
{!> ../../../docs_src/body_nested_models/tutorial009.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="7" |
|||
{!> ../../../docs_src/body_nested_models/tutorial009_py39.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
โ๏ธ ๐คฏ ๐ ๐ป ๐ด ๐โ๐ฆบ `str` ๐. |
|||
|
|||
โ๏ธ Pydantic โ๏ธ ๐ง ๐ฝ ๐ ๏ธ. |
|||
|
|||
๐ โ ๐, โ๏ธ ๐ ๐ ๏ธ ๐ฉโ๐ป ๐ช ๐ด ๐จ ๐ป ๐, ๐ ๐ ๐ป ๐ ๐ ๐ข, Pydantic ๐ ๐ ๐ซ & โ ๐ซ. |
|||
|
|||
& `dict` ๐ ๐จ `weights` ๐ ๐ค โ๏ธ `int` ๐ & `float` ๐ฒ. |
|||
|
|||
## ๐ |
|||
|
|||
โฎ๏ธ **FastAPI** ๐ โ๏ธ ๐ ๐ช ๐ Pydantic ๐ท, โช ๐ง ๐ ๐ ๐
, ๐ & ๐. |
|||
|
|||
โ๏ธ โฎ๏ธ ๐ ๐ฐ: |
|||
|
|||
* ๐จโ๐จ ๐โ๐ฆบ (๐ ๏ธ ๐ โ) |
|||
* ๐ฝ ๐ ๏ธ (.โ.. โ / ๐ ๏ธ) |
|||
* ๐ฝ ๐ฌ |
|||
* ๐ ๐งพ |
|||
* ๐ง ๐ฉบ |
@ -0,0 +1,155 @@ |
|||
# ๐ช - โน |
|||
|
|||
## โน โ โฎ๏ธ `PUT` |
|||
|
|||
โน ๐ฌ ๐ ๐ช โ๏ธ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT" class="external-link" target="_blank">๐บ๐ธ๐ `PUT`</a> ๐ ๏ธ. |
|||
|
|||
๐ ๐ช โ๏ธ `jsonable_encoder` ๐ ๐ข ๐ฝ ๐ ๐ ๐ช ๐ช ๐ป (โ
โฎ๏ธ โ ๐ฝ). ๐ผ, ๐ญ `datetime` `str`. |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="30-35" |
|||
{!> ../../../docs_src/body_updates/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="30-35" |
|||
{!> ../../../docs_src/body_updates/tutorial001_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="28-33" |
|||
{!> ../../../docs_src/body_updates/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
`PUT` โ๏ธ ๐จ ๐ฝ ๐ ๐ โ โป ๐ฝ. |
|||
|
|||
### โ ๐ โ |
|||
|
|||
๐ โ ๐ ๐ฅ ๐ ๐ โน ๐ฌ `bar` โ๏ธ `PUT` โฎ๏ธ ๐ช โ: |
|||
|
|||
```Python |
|||
{ |
|||
"name": "Barz", |
|||
"price": 3, |
|||
"description": None, |
|||
} |
|||
``` |
|||
|
|||
โฉ๏ธ โซ๏ธ ๐ซ ๐ โช ๐ช ๐ข `"tax": 20.2`, ๐ข ๐ท ๐ โ ๐ข ๐ฒ `"tax": 10.5`. |
|||
|
|||
& ๐ ๐ ๐ โฎ๏ธ ๐ "๐" `tax` `10.5`. |
|||
|
|||
## ๐ โน โฎ๏ธ `PATCH` |
|||
|
|||
๐ ๐ช โ๏ธ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH" class="external-link" target="_blank">๐บ๐ธ๐ `PATCH`</a> ๐ ๏ธ *๐* โน ๐ฝ. |
|||
|
|||
๐ โ ๐ ๐ ๐ช ๐จ ๐ด ๐ฝ ๐ ๐ ๐ โน, ๐ ๐ ๐ฃ. |
|||
|
|||
!!! Note |
|||
`PATCH` ๐ ๐ โ๏ธ & ๐ญ ๐ `PUT`. |
|||
|
|||
& ๐ ๐ โ๏ธ ๐ด `PUT`, ๐ โน. |
|||
|
|||
๐ **๐** โ๏ธ ๐ซ ๐ ๐ ๐, **FastAPI** ๐ซ ๐ซ ๐ ๐ซ. |
|||
|
|||
โ๏ธ ๐ ๐ฆฎ ๐ฆ ๐, ๐ โ๏ธ ๐, โ ๐ซ ๐ฏ โ๏ธ. |
|||
|
|||
### โ๏ธ Pydantic `exclude_unset` ๐ข |
|||
|
|||
๐ฅ ๐ ๐ ๐จ ๐ โน, โซ๏ธ ๐ถ โ โ๏ธ ๐ข `exclude_unset` Pydantic ๐ท `.dict()`. |
|||
|
|||
๐ `item.dict(exclude_unset=True)`. |
|||
|
|||
๐ ๐ ๐ `dict` โฎ๏ธ ๐ด ๐ฝ ๐ โ ๐โ ๐ `item` ๐ท, ๐ซ ๐ข ๐ฒ. |
|||
|
|||
โคด๏ธ ๐ ๐ช โ๏ธ ๐ ๐ `dict` โฎ๏ธ ๐ด ๐ฝ ๐ โ (๐จ ๐จ), ๐ซ ๐ข ๐ฒ: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="34" |
|||
{!> ../../../docs_src/body_updates/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="34" |
|||
{!> ../../../docs_src/body_updates/tutorial002_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="32" |
|||
{!> ../../../docs_src/body_updates/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
### โ๏ธ Pydantic `update` ๐ข |
|||
|
|||
๐, ๐ ๐ช โ ๐ โป ๐ท โ๏ธ `.copy()`, & ๐ถโโ๏ธ `update` ๐ข โฎ๏ธ `dict` โ ๐ฝ โน. |
|||
|
|||
๐ `stored_item_model.copy(update=update_data)`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="35" |
|||
{!> ../../../docs_src/body_updates/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="35" |
|||
{!> ../../../docs_src/body_updates/tutorial002_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="33" |
|||
{!> ../../../docs_src/body_updates/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
### ๐ โน ๐ |
|||
|
|||
๐, โ ๐ โน ๐ ๐: |
|||
|
|||
* (โ) โ๏ธ `PATCH` โฉ๏ธ `PUT`. |
|||
* ๐ ๐ช ๐ฝ. |
|||
* ๐ฎ ๐ ๐ฝ Pydantic ๐ท. |
|||
* ๐ `dict` ๐ต ๐ข ๐ฒ โช๏ธโก๏ธ ๐ข ๐ท (โ๏ธ `exclude_unset`). |
|||
* ๐ ๐ ๐ ๐ช โน ๐ด ๐ฒ ๐ค โ ๐ฉโ๐ป, โฉ๏ธ ๐ ๐ฒ โช ๐ช โฎ๏ธ ๐ข ๐ฒ ๐ ๐ท. |
|||
* โ ๐ ๐ช ๐ท, ๐ ๏ธ โซ๏ธ ๐ข โฎ๏ธ ๐จ ๐ โน (โ๏ธ `update` ๐ข). |
|||
* ๐ ๐ ๐ท ๐ณ ๐ ๐ช ๐ช ๐ ๐ฝ (๐ผ, โ๏ธ `jsonable_encoder`). |
|||
* ๐ โญ โ๏ธ ๐ท `.dict()` ๐ฉโ๐ฌ ๐, โ๏ธ โซ๏ธ โ ๐ญ (& ๐) ๐ฒ ๐ฝ ๐ ๐ ๐ช ๐ ๐ป, ๐ผ, `datetime` `str`. |
|||
* ๐ ๐ฝ ๐ ๐ฝ. |
|||
* ๐จ โน ๐ท. |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="30-37" |
|||
{!> ../../../docs_src/body_updates/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="30-37" |
|||
{!> ../../../docs_src/body_updates/tutorial002_py39.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="28-35" |
|||
{!> ../../../docs_src/body_updates/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
๐ ๐ช ๐ค โ๏ธ ๐ ๐ โ โฎ๏ธ ๐บ๐ธ๐ `PUT` ๐ ๏ธ. |
|||
|
|||
โ๏ธ ๐ผ ๐ฅ โ๏ธ `PATCH` โฉ๏ธ โซ๏ธ โ ๐ซ โ๏ธ ๐ผ. |
|||
|
|||
!!! note |
|||
๐ ๐ ๐ข ๐ท โ. |
|||
|
|||
, ๐ฅ ๐ ๐ ๐จ ๐ โน ๐ ๐ช ๐ซ ๐ ๐ข, ๐ ๐ช โ๏ธ ๐ท โฎ๏ธ ๐ ๐ข โข ๐ฆ (โฎ๏ธ ๐ข ๐ฒ โ๏ธ `None`). |
|||
|
|||
๐ฌ โช๏ธโก๏ธ ๐ท โฎ๏ธ ๐ ๐ฆ ๐ฒ **โน** & ๐ท โฎ๏ธ โ ๐ฒ **๐**, ๐ ๐ช โ๏ธ ๐ญ ๐ฌ [โ ๐ท](extra-models.md){.internal-link target=_blank}. |
@ -0,0 +1,213 @@ |
|||
# ๐จ ๐ช |
|||
|
|||
๐โ ๐ ๐ช ๐จ ๐ โช๏ธโก๏ธ ๐ฉโ๐ป (โก๏ธ ๐ฌ, ๐ฅ) ๐ ๐ ๏ธ, ๐ ๐จ โซ๏ธ **๐จ ๐ช**. |
|||
|
|||
**๐จ** ๐ช ๐ ๐จ ๐ฉโ๐ป ๐ ๐ ๏ธ. **๐จ** ๐ช ๐ฝ ๐ ๐ ๏ธ ๐จ ๐ฉโ๐ป. |
|||
|
|||
๐ ๐ ๏ธ ๐ ๐ง โ๏ธ ๐จ **๐จ** ๐ช. โ๏ธ ๐ฉโ๐ป ๐ซ ๐ฏ ๐ช ๐จ **๐จ** ๐ช ๐ ๐ฐ. |
|||
|
|||
๐ฃ **๐จ** ๐ช, ๐ โ๏ธ <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> ๐ท โฎ๏ธ ๐ ๐ซ ๐๏ธ & ๐ฐ. |
|||
|
|||
!!! info |
|||
๐จ ๐ฝ, ๐ ๐ โ๏ธ 1๏ธโฃ: `POST` (๐
โ ), `PUT`, `DELETE` โ๏ธ `PATCH`. |
|||
|
|||
๐จ ๐ช โฎ๏ธ `GET` ๐จ โ๏ธ โ ๐ญ ๐ง, ๐, โซ๏ธ ๐โ๐ฆบ FastAPI, ๐ด ๐ถ ๐/๐ โ๏ธ ๐ผ. |
|||
|
|||
โซ๏ธ ๐ซ, ๐ ๐ฉบ โฎ๏ธ ๐ฆ ๐ ๐ ๐ซ ๐ฆ ๐งพ ๐ช ๐โ โ๏ธ `GET`, & ๐ณ ๐ ๐ช ๐ซ ๐โ๐ฆบ โซ๏ธ. |
|||
|
|||
## ๐ Pydantic `BaseModel` |
|||
|
|||
๐ฅ, ๐ ๐ช ๐ `BaseModel` โช๏ธโก๏ธ `pydantic`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="4" |
|||
{!> ../../../docs_src/body/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="2" |
|||
{!> ../../../docs_src/body/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
## โ ๐ ๐ฝ ๐ท |
|||
|
|||
โคด๏ธ ๐ ๐ฃ ๐ ๐ฝ ๐ท ๐ ๐ ๐ โช๏ธโก๏ธ `BaseModel`. |
|||
|
|||
โ๏ธ ๐ฉ ๐ ๐ ๐ ๐ข: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="7-11" |
|||
{!> ../../../docs_src/body/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="5-9" |
|||
{!> ../../../docs_src/body/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐โ ๐ฃ ๐ข ๐ข, ๐โ ๐ท ๐ข โ๏ธ ๐ข ๐ฒ, โซ๏ธ ๐ซ โ. โช, โซ๏ธ โ. โ๏ธ `None` โ โซ๏ธ ๐ฆ. |
|||
|
|||
๐ผ, ๐ ๐ท ๐ ๐ฃ ๐ป "`object`" (โ๏ธ ๐ `dict`) ๐: |
|||
|
|||
```JSON |
|||
{ |
|||
"name": "Foo", |
|||
"description": "An optional description", |
|||
"price": 45.2, |
|||
"tax": 3.5 |
|||
} |
|||
``` |
|||
|
|||
... `description` & `tax` ๐ฆ (โฎ๏ธ ๐ข ๐ฒ `None`), ๐ ๐ป "`object`" ๐ โ: |
|||
|
|||
```JSON |
|||
{ |
|||
"name": "Foo", |
|||
"price": 45.2 |
|||
} |
|||
``` |
|||
|
|||
## ๐ฃ โซ๏ธ ๐ข |
|||
|
|||
๐ฎ โซ๏ธ ๐ *โก ๐ ๏ธ*, ๐ฃ โซ๏ธ ๐ ๐ ๐ ๐ฃ โก & ๐ข ๐ข: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="18" |
|||
{!> ../../../docs_src/body/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="16" |
|||
{!> ../../../docs_src/body/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
...& ๐ฃ ๐ฎ ๐ ๐ท ๐ โ, `Item`. |
|||
|
|||
## ๐ |
|||
|
|||
โฎ๏ธ ๐ ๐ ๐ ๐, **FastAPI** ๐: |
|||
|
|||
* โ ๐ช ๐จ ๐ป. |
|||
* ๐ ๐ ๐ (๐ฅ ๐ช). |
|||
* โ ๐ฝ. |
|||
* ๐ฅ ๐ฝ โ, โซ๏ธ ๐ ๐จ ๐ & ๐ โ, โ ๏ธ โซ๏ธโ ๐โ & โซ๏ธโ โ ๐. |
|||
* ๐ค ๐ ๐จ ๐ ๐ข `item`. |
|||
* ๐ ๐ฃ โซ๏ธ ๐ข ๐ `Item`, ๐ ๐ โ๏ธ ๐ ๐จโ๐จ ๐โ๐ฆบ (๐ ๏ธ, โ๏ธ) ๐ ๐ข & ๐ซ ๐. |
|||
* ๐ <a href="https://json-schema.org" class="external-link" target="_blank">๐ป ๐</a> ๐ ๐ ๐ท, ๐ ๐ช โ๏ธ ๐ซ ๐ ๐ ๐ ๐ ๐ฅ โซ๏ธ โ ๐ ๐ ๐. |
|||
* ๐ ๐ ๐ ๐ ๐ ๐ ๐, & โ๏ธ ๐ง ๐งพ <abbr title="User Interfaces">โ</abbr>. |
|||
|
|||
## ๐ง ๐ฉบ |
|||
|
|||
๐ป ๐ ๐ ๐ท ๐ ๐ ๐ ๐ ๐ ๐, & ๐ ๐ฆ ๐ ๐ ๏ธ ๐ฉบ: |
|||
|
|||
<img src="/img/tutorial/body/image01.png"> |
|||
|
|||
& ๐ โ๏ธ ๐ ๏ธ ๐ฉบ ๐ ๐ *โก ๐ ๏ธ* ๐ ๐ช ๐ซ: |
|||
|
|||
<img src="/img/tutorial/body/image02.png"> |
|||
|
|||
## ๐จโ๐จ ๐โ๐ฆบ |
|||
|
|||
๐ ๐จโ๐จ, ๐ ๐ ๐ข ๐ ๐ ๐ค ๐ ๐ & ๐ ๏ธ ๐ (๐ ๐ซ๐ ๐จ ๐ฅ ๐ ๐จ `dict` โฉ๏ธ Pydantic ๐ท): |
|||
|
|||
<img src="/img/tutorial/body/image03.png"> |
|||
|
|||
๐ ๐ค โ โ
โ ๐ ๐ ๏ธ: |
|||
|
|||
<img src="/img/tutorial/body/image04.png"> |
|||
|
|||
๐ ๐ซ ๐ค, ๐ ๐ ๏ธ ๐ ๐คญ ๐ ๐ง. |
|||
|
|||
& โซ๏ธ ๐ ๐ฏ ๐ง ๐, โญ ๐ ๐ ๏ธ, ๐ โซ๏ธ ๐ ๐ท โฎ๏ธ ๐ ๐จโ๐จ. |
|||
|
|||
๐ค ๐ Pydantic โซ๏ธ ๐โ๐ฆบ ๐. |
|||
|
|||
โฎ๏ธ ๐ผ โ โฎ๏ธ <a href="https://code.visualstudio.com" class="external-link" target="_blank">๐ ๐ ๐</a>. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ค ๐ ๐จโ๐จ ๐โ๐ฆบ โฎ๏ธ <a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">๐</a> & ๐
๐ ๐ ๐จโ๐จ: |
|||
|
|||
<img src="/img/tutorial/body/image05.png"> |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ โ๏ธ <a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">๐</a> ๐ ๐จโ๐จ, ๐ ๐ช โ๏ธ <a href="https://github.com/koxudaxi/pydantic-pycharm-plugin/" class="external-link" target="_blank">Pydantic ๐ ๐</a>. |
|||
|
|||
โซ๏ธ ๐ ๐จโ๐จ ๐โ๐ฆบ Pydantic ๐ท, โฎ๏ธ: |
|||
|
|||
* ๐-๐ ๏ธ |
|||
* ๐ โ
|
|||
* ๐ ๏ธ |
|||
* ๐ |
|||
* ๐ฌ |
|||
|
|||
## โ๏ธ ๐ท |
|||
|
|||
๐ ๐ข, ๐ ๐ช ๐ ๐ ๐ข ๐ท ๐ ๐: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="21" |
|||
{!> ../../../docs_src/body/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="19" |
|||
{!> ../../../docs_src/body/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
## ๐จ ๐ช โ โก ๐ข |
|||
|
|||
๐ ๐ช ๐ฃ โก ๐ข & ๐จ ๐ช ๐ ๐ฐ. |
|||
|
|||
**FastAPI** ๐ ๐ค ๐ ๐ข ๐ข ๐ ๐ โก ๐ข ๐ **โ โช๏ธโก๏ธ โก**, & ๐ ๐ข ๐ข ๐ ๐ฃ Pydantic ๐ท ๐ **โ โช๏ธโก๏ธ ๐จ ๐ช**. |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="17-18" |
|||
{!> ../../../docs_src/body/tutorial003.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="15-16" |
|||
{!> ../../../docs_src/body/tutorial003_py310.py!} |
|||
``` |
|||
|
|||
## ๐จ ๐ช โ โก โ ๐ข ๐ข |
|||
|
|||
๐ ๐ช ๐ฃ **๐ช**, **โก** & **๐ข** ๐ข, ๐ ๐ ๐ฐ. |
|||
|
|||
**FastAPI** ๐ ๐ค ๐ ๐ซ & โ ๐ โช๏ธโก๏ธ โ ๐ฅ. |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="18" |
|||
{!> ../../../docs_src/body/tutorial004.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="16" |
|||
{!> ../../../docs_src/body/tutorial004_py310.py!} |
|||
``` |
|||
|
|||
๐ข ๐ข ๐ ๐ค โฉ: |
|||
|
|||
* ๐ฅ ๐ข ๐ฃ **โก**, โซ๏ธ ๐ โ๏ธ โก ๐ข. |
|||
* ๐ฅ ๐ข **โญ ๐** (๐ `int`, `float`, `str`, `bool`, โ๏ธ) โซ๏ธ ๐ ๐ฌ **๐ข** ๐ข. |
|||
* ๐ฅ ๐ข ๐ฃ ๐ **Pydantic ๐ท**, โซ๏ธ ๐ ๐ฌ ๐จ **๐ช**. |
|||
|
|||
!!! note |
|||
FastAPI ๐ ๐ญ ๐ ๐ฒ `q` ๐ซ โ โฉ๏ธ ๐ข ๐ฒ `= None`. |
|||
|
|||
`Union` `Union[str, None]` ๐ซ โ๏ธ FastAPI, โ๏ธ ๐ โ ๐ ๐จโ๐จ ๐ค ๐ ๐ ๐โ๐ฆบ & ๐ โ. |
|||
|
|||
## ๐ต Pydantic |
|||
|
|||
๐ฅ ๐ ๐ซ ๐ โ๏ธ Pydantic ๐ท, ๐ ๐ช โ๏ธ **๐ช** ๐ข. ๐ ๐ฉบ [๐ช - ๐ ๐ข: โญ ๐ฒ ๐ช](body-multiple-params.md#singular-values-in-body){.internal-link target=_blank}. |
@ -0,0 +1,49 @@ |
|||
# ๐ช ๐ข |
|||
|
|||
๐ ๐ช ๐ฌ ๐ช ๐ข ๐ ๐ ๐ ๐ฌ `Query` & `Path` ๐ข. |
|||
|
|||
## ๐ `Cookie` |
|||
|
|||
๐ฅ ๐ `Cookie`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="3" |
|||
{!> ../../../docs_src/cookie_params/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="1" |
|||
{!> ../../../docs_src/cookie_params/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
## ๐ฃ `Cookie` ๐ข |
|||
|
|||
โคด๏ธ ๐ฃ ๐ช ๐ข โ๏ธ ๐ ๐ โฎ๏ธ `Path` & `Query`. |
|||
|
|||
๐ฅ ๐ฒ ๐ข ๐ฒ, ๐ ๐ช ๐ถโโ๏ธ ๐ โ ๐ฌ โ๏ธ โ ๐ข: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9" |
|||
{!> ../../../docs_src/cookie_params/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="7" |
|||
{!> ../../../docs_src/cookie_params/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
!!! note "๐ก โน" |
|||
`Cookie` "๐ญ" ๐ `Path` & `Query`. โซ๏ธ ๐ โช๏ธโก๏ธ ๐ โ `Param` ๐. |
|||
|
|||
โ๏ธ ๐ญ ๐ ๐โ ๐ ๐ `Query`, `Path`, `Cookie` & ๐ โช๏ธโก๏ธ `fastapi`, ๐ ๐ค ๐ข ๐ ๐จ ๐ ๐. |
|||
|
|||
!!! info |
|||
๐ฃ ๐ช, ๐ ๐ช โ๏ธ `Cookie`, โฉ๏ธ โช ๐ข ๐ ๐ฌ ๐ข ๐ข. |
|||
|
|||
## ๐ |
|||
|
|||
๐ฃ ๐ช โฎ๏ธ `Cookie`, โ๏ธ ๐ โ โ `Query` & `Path`. |
@ -0,0 +1,84 @@ |
|||
# โ (โ๏ธ-๐จ๐ณ โน ๐ค) |
|||
|
|||
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" class="external-link" target="_blank">โ โ๏ธ "โ๏ธ-๐จ๐ณ โน ๐ค"</a> ๐ โ ๐โ ๐ธ ๐โโ ๐ฅ โ๏ธ ๐ธ ๐ ๐ ๐ โฎ๏ธ ๐ฉโ๐ป, & ๐ฉโ๐ป ๐ "๐จ๐ณ" ๐ ๐ธ. |
|||
|
|||
## ๐จ๐ณ |
|||
|
|||
๐จ๐ณ ๐ ๐ ๏ธ (`http`, `https`), ๐ (`myapp.com`, `localhost`, `localhost.tiangolo.com`), & โด (`80`, `443`, `8080`). |
|||
|
|||
, ๐ ๐ซ ๐ ๐จ๐ณ: |
|||
|
|||
* `http://localhost` |
|||
* `https://localhost` |
|||
* `http://localhost:8080` |
|||
|
|||
๐ฅ ๐ซ ๐ `localhost`, ๐ซ โ๏ธ ๐ ๐ ๏ธ โ๏ธ โด,, ๐ซ ๐ "๐จ๐ณ". |
|||
|
|||
## ๐ |
|||
|
|||
, โก๏ธ ๐ฌ ๐ โ๏ธ ๐ธ ๐ ๐ ๐ฅ `http://localhost:8080`, & ๐ฎ ๐ธ ๐ ๐ โฎ๏ธ ๐ฉโ๐ป ๐ `http://localhost` (โฉ๏ธ ๐ฅ ๐ซ โ โด, ๐ฅ ๐ ๐ค ๐ข โด `80`). |
|||
|
|||
โคด๏ธ, ๐ฅ ๐ ๐จ ๐บ๐ธ๐ `OPTIONS` ๐จ ๐ฉโ๐ป, & ๐ฅ ๐ฉโ๐ป ๐จ โ ๐ โ ๐ป โช๏ธโก๏ธ ๐ ๐ ๐จ๐ณ (`http://localhost:8080`) โคด๏ธ ๐ฅ ๐ โก๏ธ ๐ธ ๐ธ ๐จ ๐ฎ ๐จ ๐ฉโ๐ป. |
|||
|
|||
๐ ๐, ๐ฉโ๐ป ๐ โ๏ธ ๐ "โ ๐จ๐ณ". |
|||
|
|||
๐ ๐ผ, โซ๏ธ ๐ โ๏ธ ๐ `http://localhost:8080` ๐ธ ๐ท โ. |
|||
|
|||
## ๐ |
|||
|
|||
โซ๏ธ ๐ช ๐ฃ ๐ `"*"` ("๐") ๐ฌ ๐ ๐ โ. |
|||
|
|||
โ๏ธ ๐ ๐ ๐ด โ ๐ฏ ๐ ๐ป, ๐ซ ๐ ๐ ๐ ๐: ๐ช, โ ๐ ๐ ๐ โ๏ธ โฎ๏ธ ๐จ ๐ค, โ๏ธ. |
|||
|
|||
, ๐ ๐ท โ, โซ๏ธ ๐ป โ ๐ฏ โ ๐จ๐ณ. |
|||
|
|||
## โ๏ธ `CORSMiddleware` |
|||
|
|||
๐ ๐ช ๐ โซ๏ธ ๐ **FastAPI** ๐ธ โ๏ธ `CORSMiddleware`. |
|||
|
|||
* ๐ `CORSMiddleware`. |
|||
* โ ๐ โ ๐จ๐ณ (๐ป). |
|||
* ๐ฎ โซ๏ธ "๐ ๏ธ" ๐ **FastAPI** ๐ธ. |
|||
|
|||
๐ ๐ช โ ๐ฅ ๐ ๐ฉโ๐ป โ: |
|||
|
|||
* ๐ (โ ๐, ๐ช, โ๏ธ). |
|||
* ๐ฏ ๐บ๐ธ๐ ๐ฉโ๐ฌ (`POST`, `PUT`) โ๏ธ ๐ ๐ซ โฎ๏ธ ๐ `"*"`. |
|||
* ๐ฏ ๐บ๐ธ๐ ๐ โ๏ธ ๐ ๐ซ โฎ๏ธ ๐ `"*"`. |
|||
|
|||
```Python hl_lines="2 6-11 13-19" |
|||
{!../../../docs_src/cors/tutorial001.py!} |
|||
``` |
|||
|
|||
๐ข ๐ข โ๏ธ `CORSMiddleware` ๐ ๏ธ ๐ซ ๐ข, ๐ ๐ ๐ช ๐ฏ ๐ ๏ธ ๐ฏ ๐จ๐ณ, ๐ฉโ๐ฌ, โ๏ธ ๐, โ ๐ฅ โ โ๏ธ ๐ซ โ๏ธ-๐ ๐. |
|||
|
|||
๐ โ ๐โ๐ฆบ: |
|||
|
|||
* `allow_origins` - ๐ ๐จ๐ณ ๐ ๐ โ โ โ๏ธ-๐จ๐ณ ๐จ. ๐คถ โ. `['https://example.org', 'https://www.example.org']`. ๐ ๐ช โ๏ธ `['*']` โ ๐ ๐จ๐ณ. |
|||
* `allow_origin_regex` - ๐ป ๐ป ๐ ๐ก ๐จ๐ณ ๐ ๐ โ โ โ๏ธ-๐จ๐ณ ๐จ. โ
`'https://.*\.example\.org'`. |
|||
* `allow_methods` - ๐ ๐บ๐ธ๐ ๐ฉโ๐ฌ ๐ ๐ โ โ๏ธ-๐จ๐ณ ๐จ. ๐ข `['GET']`. ๐ ๐ช โ๏ธ `['*']` โ ๐ ๐ฉ ๐ฉโ๐ฌ. |
|||
* `allow_headers` - ๐ ๐บ๐ธ๐ ๐จ ๐ ๐ ๐ ๐โ๐ฆบ โ๏ธ-๐จ๐ณ ๐จ. ๐ข `[]`. ๐ ๐ช โ๏ธ `['*']` โ ๐ ๐. `Accept`, `Accept-Language`, `Content-Language` & `Content-Type` ๐ ๐ง โ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests" class="external-link" rel="noopener" target="_blank">๐
โ ๐จ</a>. |
|||
* `allow_credentials` - ๐ฆ ๐ ๐ช ๐ ๐โ๐ฆบ โ๏ธ-๐จ๐ณ ๐จ. ๐ข `False`. , `allow_origins` ๐ซ๐ โ `['*']` ๐ โ, ๐จ๐ณ ๐ โ. |
|||
* `expose_headers` - ๐ฆ ๐ ๐จ ๐ ๐ ๐ โ โฟ ๐ฅ. ๐ข `[]`. |
|||
* `max_age` - โ ๐ ๐ฐ ๐ฅ ๐ฅ ๐พ โ ๐จ. ๐ข `600`. |
|||
|
|||
๐ ๏ธ ๐จ 2๏ธโฃ ๐ฏ ๐ ๐บ๐ธ๐ ๐จ... |
|||
|
|||
### โ ๐ซ ๐จ |
|||
|
|||
๐ ๐ `OPTIONS` ๐จ โฎ๏ธ `Origin` & `Access-Control-Request-Method` ๐. |
|||
|
|||
๐ ๐ผ ๐ ๏ธ ๐ ๐ ๐จ ๐จ & ๐จ โฎ๏ธ โ โ ๐, & ๐ฏโโ๏ธ `200` โ๏ธ `400` ๐จ ๐ ๐ฏ. |
|||
|
|||
### ๐
๐จ |
|||
|
|||
๐ ๐จ โฎ๏ธ `Origin` ๐. ๐ ๐ผ ๐ ๏ธ ๐ ๐ถโโ๏ธ ๐จ ๐ ๐, โ๏ธ ๐ ๐ โ โ ๐ ๐ ๐จ. |
|||
|
|||
## ๐
โน |
|||
|
|||
๐ โน ๐ <abbr title="Cross-Origin Resource Sharing">โ</abbr>, โ
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" class="external-link" target="_blank">๐ฆ โ ๐งพ</a>. |
|||
|
|||
!!! note "๐ก โน" |
|||
๐ ๐ช โ๏ธ `from starlette.middleware.cors import CORSMiddleware`. |
|||
|
|||
**FastAPI** ๐ ๐ ๐ ๏ธ `fastapi.middleware` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐ ๏ธ ๐ ๐ โช๏ธโก๏ธ ๐. |
@ -0,0 +1,112 @@ |
|||
# ๐ ๏ธ |
|||
|
|||
๐ ๐ช ๐ ๐น ๐ ๐จโ๐จ, ๐ผ โฎ๏ธ ๐ ๐ ๐ โ๏ธ ๐. |
|||
|
|||
## ๐ค `uvicorn` |
|||
|
|||
๐ FastAPI ๐ธ, ๐ & ๐ `uvicorn` ๐: |
|||
|
|||
```Python hl_lines="1 15" |
|||
{!../../../docs_src/debugging/tutorial001.py!} |
|||
``` |
|||
|
|||
### ๐ `__name__ == "__main__"` |
|||
|
|||
๐ ๐ฏ `__name__ == "__main__"` โ๏ธ ๐ ๐ ๐ ๏ธ ๐โ ๐ ๐ ๐ค โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ python myapp.py |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โ๏ธ ๐ซ ๐ค ๐โ โ1๏ธโฃ ๐ ๐ โซ๏ธ, ๐: |
|||
|
|||
```Python |
|||
from myapp import app |
|||
``` |
|||
|
|||
#### ๐
โน |
|||
|
|||
โก๏ธ ๐ฌ ๐ ๐ ๐ `myapp.py`. |
|||
|
|||
๐ฅ ๐ ๐ โซ๏ธ โฎ๏ธ: |
|||
|
|||
<div class="termy"> |
|||
|
|||
```console |
|||
$ python myapp.py |
|||
``` |
|||
|
|||
</div> |
|||
|
|||
โคด๏ธ ๐ ๐ข `__name__` ๐ ๐, โ ๐ ๐, ๐ โ๏ธ ๐ฒ ๐ป `"__main__"`. |
|||
|
|||
, ๐: |
|||
|
|||
```Python |
|||
uvicorn.run(app, host="0.0.0.0", port=8000) |
|||
``` |
|||
|
|||
๐ ๐. |
|||
|
|||
--- |
|||
|
|||
๐ ๐ ๐ซ ๐จ ๐ฅ ๐ ๐ ๐ ๐น (๐). |
|||
|
|||
, ๐ฅ ๐ โ๏ธ โ1๏ธโฃ ๐ `importer.py` โฎ๏ธ: |
|||
|
|||
```Python |
|||
from myapp import app |
|||
|
|||
# Some more code |
|||
``` |
|||
|
|||
๐ ๐ผ, ๐ง ๐ข ๐ `myapp.py` ๐ ๐ซ โ๏ธ ๐ข `__name__` โฎ๏ธ ๐ฒ `"__main__"`. |
|||
|
|||
, โธ: |
|||
|
|||
```Python |
|||
uvicorn.run(app, host="0.0.0.0", port=8000) |
|||
``` |
|||
|
|||
๐ ๐ซ ๐ ๏ธ. |
|||
|
|||
!!! info |
|||
๐
โน, โ
<a href="https://docs.python.org/3/library/__main__.html" class="external-link" target="_blank">๐ ๐ ๐ฉบ</a>. |
|||
|
|||
## ๐ ๐ ๐ โฎ๏ธ ๐ ๐น |
|||
|
|||
โฉ๏ธ ๐ ๐ Uvicorn ๐ฝ ๐ โช๏ธโก๏ธ ๐ ๐, ๐ ๐ช ๐ค ๐ ๐ ๐ (๐ FastAPI ๐ธ) ๐ โช๏ธโก๏ธ ๐น. |
|||
|
|||
--- |
|||
|
|||
๐ผ, ๐ ๐ ๐, ๐ ๐ช: |
|||
|
|||
* ๐ถ "โน" ๐. |
|||
* "๐ฎ ๐ณ...". |
|||
* ๐ "๐" |
|||
* ๐ ๐น โฎ๏ธ ๐ "`Python: Current File (Integrated Terminal)`". |
|||
|
|||
โซ๏ธ ๐ โคด๏ธ โถ๏ธ ๐ฝ โฎ๏ธ ๐ **FastAPI** ๐, โ๏ธ ๐ 0๏ธโฃ, โ๏ธ. |
|||
|
|||
๐ฅ โ โซ๏ธ ๐ช ๐: |
|||
|
|||
<img src="/img/tutorial/debugging/image01.png"> |
|||
|
|||
--- |
|||
|
|||
๐ฅ ๐ โ๏ธ ๐, ๐ ๐ช: |
|||
|
|||
* ๐ "๐" ๐ฃ. |
|||
* ๐ ๐ "โน...". |
|||
* โคด๏ธ ๐ ๐ฃ ๐ฆ ๐. |
|||
* ๐ ๐ โน (๐ ๐ผ, `main.py`). |
|||
|
|||
โซ๏ธ ๐ โคด๏ธ โถ๏ธ ๐ฝ โฎ๏ธ ๐ **FastAPI** ๐, โ๏ธ ๐ 0๏ธโฃ, โ๏ธ. |
|||
|
|||
๐ฅ โ โซ๏ธ ๐ช ๐: |
|||
|
|||
<img src="/img/tutorial/debugging/image02.png"> |
@ -0,0 +1,247 @@ |
|||
# ๐ ๐ |
|||
|
|||
โญ ๐คฟ โฌ ๐ **๐ ๐** โ๏ธ, โก๏ธ โป โฎ๏ธ ๐ผ. |
|||
|
|||
## `dict` โช๏ธโก๏ธ โฎ๏ธ ๐ผ |
|||
|
|||
โฎ๏ธ ๐ผ, ๐ฅ ๐ฌ `dict` โช๏ธโก๏ธ ๐ ๐ ("โ"): |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9" |
|||
{!> ../../../docs_src/dependencies/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="7" |
|||
{!> ../../../docs_src/dependencies/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
โ๏ธ โคด๏ธ ๐ฅ ๐ค `dict` ๐ข `commons` *โก ๐ ๏ธ ๐ข*. |
|||
|
|||
& ๐ฅ ๐ญ ๐ ๐จโ๐จ ๐ช ๐ซ ๐ ๐ ๐โ๐ฆบ (๐ ๐ ๏ธ) `dict`โ, โฉ๏ธ ๐ซ ๐ช ๐ซ ๐ญ ๐ซ ๐ & ๐ฒ ๐. |
|||
|
|||
๐ฅ ๐ช ๐... |
|||
|
|||
## โซ๏ธโ โ ๐ |
|||
|
|||
๐ ๐ ๐ โ๏ธ ๐ ๐ ๐ฃ ๐ข. |
|||
|
|||
โ๏ธ ๐ ๐ซ ๐ด ๐ ๐ฃ ๐ (๐ โซ๏ธ ๐ ๐ฒ ๐ โ ). |
|||
|
|||
๐ โ ๐ ๐ ๐ "๐ง๐ฒ". |
|||
|
|||
"**๐ง๐ฒ**" ๐ ๐ณ ๐ ๐ ๐ช "๐ค" ๐ ๐ข. |
|||
|
|||
, ๐ฅ ๐ โ๏ธ ๐ `something` (๐ ๐ช _๐ซ_ ๐ข) & ๐ ๐ช "๐ค" โซ๏ธ (๐ ๏ธ โซ๏ธ) ๐: |
|||
|
|||
```Python |
|||
something() |
|||
``` |
|||
|
|||
โ๏ธ |
|||
|
|||
```Python |
|||
something(some_argument, some_keyword_argument="foo") |
|||
``` |
|||
|
|||
โคด๏ธ โซ๏ธ "๐ง๐ฒ". |
|||
|
|||
## ๐ ๐ |
|||
|
|||
๐ 5๏ธโฃ๐ ๐ ๐ โ ๐ ๐ ๐, ๐ โ๏ธ ๐ ๐ โ. |
|||
|
|||
๐ผ: |
|||
|
|||
```Python |
|||
class Cat: |
|||
def __init__(self, name: str): |
|||
self.name = name |
|||
|
|||
|
|||
fluffy = Cat(name="Mr Fluffy") |
|||
``` |
|||
|
|||
๐ ๐ผ, `fluffy` ๐ ๐ `Cat`. |
|||
|
|||
& โ `fluffy`, ๐ "๐ค" `Cat`. |
|||
|
|||
, ๐ ๐ **๐ง๐ฒ**. |
|||
|
|||
โคด๏ธ, **FastAPI**, ๐ ๐ช โ๏ธ ๐ ๐ ๐. |
|||
|
|||
โซ๏ธโ FastAPI ๐ค โ
๐ โซ๏ธ "๐ง๐ฒ" (๐ข, ๐ โ๏ธ ๐ณ ๐) & ๐ข ๐ฌ. |
|||
|
|||
๐ฅ ๐ ๐ถโโ๏ธ "๐ง๐ฒ" ๐ **FastAPI**, โซ๏ธ ๐ ๐ฌ ๐ข ๐ "๐ง๐ฒ", & ๐ ๏ธ ๐ซ ๐ ๐ ๐ข *โก ๐ ๏ธ ๐ข*. โ
๐ง-๐. |
|||
|
|||
๐ โ ๐ง๐ฒ โฎ๏ธ ๐
โโ ๐ข ๐. ๐ โซ๏ธ ๐ *โก ๐ ๏ธ ๐ข* โฎ๏ธ ๐
โโ ๐ข. |
|||
|
|||
โคด๏ธ, ๐ฅ ๐ช ๐ ๐ "โ" `common_parameters` โช๏ธโก๏ธ ๐ ๐ `CommonQueryParams`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="11-15" |
|||
{!> ../../../docs_src/dependencies/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9-13" |
|||
{!> ../../../docs_src/dependencies/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
๐ธ ๐ `__init__` ๐ฉโ๐ฌ โ๏ธ โ ๐ ๐: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="12" |
|||
{!> ../../../docs_src/dependencies/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="10" |
|||
{!> ../../../docs_src/dependencies/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
...โซ๏ธ โ๏ธ ๐ ๐ข ๐ โฎ๏ธ `common_parameters`: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="9" |
|||
{!> ../../../docs_src/dependencies/tutorial001.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="6" |
|||
{!> ../../../docs_src/dependencies/tutorial001_py310.py!} |
|||
``` |
|||
|
|||
๐ ๐ข โซ๏ธโ **FastAPI** ๐ โ๏ธ "โ" ๐. |
|||
|
|||
๐ฏโโ๏ธ ๐ผ, โซ๏ธ ๐ โ๏ธ: |
|||
|
|||
* ๐ฆ `q` ๐ข ๐ข ๐ `str`. |
|||
* `skip` ๐ข ๐ข ๐ `int`, โฎ๏ธ ๐ข `0`. |
|||
* `limit` ๐ข ๐ข ๐ `int`, โฎ๏ธ ๐ข `100`. |
|||
|
|||
๐ฏโโ๏ธ ๐ผ ๐ฝ ๐ ๐, โ, ๐ ๐ ๐ ๐, โ๏ธ. |
|||
|
|||
## โ๏ธ โซ๏ธ |
|||
|
|||
๐ ๐ ๐ช ๐ฃ ๐ ๐ โ๏ธ ๐ ๐. |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="19" |
|||
{!> ../../../docs_src/dependencies/tutorial002.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="17" |
|||
{!> ../../../docs_src/dependencies/tutorial002_py310.py!} |
|||
``` |
|||
|
|||
**FastAPI** ๐ค `CommonQueryParams` ๐. ๐ โ "๐" ๐ ๐ & ๐ ๐ ๐ถโโ๏ธ ๐ข `commons` ๐ ๐ข. |
|||
|
|||
## ๐ โ ๐ `Depends` |
|||
|
|||
๐ โ ๐ฅ โ `CommonQueryParams` ๐ ๐ ๐: |
|||
|
|||
```Python |
|||
commons: CommonQueryParams = Depends(CommonQueryParams) |
|||
``` |
|||
|
|||
๐ `CommonQueryParams`,: |
|||
|
|||
```Python |
|||
... = Depends(CommonQueryParams) |
|||
``` |
|||
|
|||
...โซ๏ธโ **FastAPI** ๐ ๐ค โ๏ธ ๐ญ โซ๏ธโ ๐. |
|||
|
|||
โช๏ธโก๏ธ โซ๏ธ ๐ FastAPI ๐ โ ๐ฃ ๐ข & ๐ โซ๏ธโ FastAPI ๐ ๐ค ๐ค. |
|||
|
|||
--- |
|||
|
|||
๐ ๐ผ, ๐ฅ `CommonQueryParams`,: |
|||
|
|||
```Python |
|||
commons: CommonQueryParams ... |
|||
``` |
|||
|
|||
...๐ซ โ๏ธ ๐ ๐ ๐ **FastAPI**. FastAPI ๐ ๐ซ โ๏ธ โซ๏ธ ๐ฝ ๐ ๏ธ, ๐ฌ, โ๏ธ. (โซ๏ธ โ๏ธ `= Depends(CommonQueryParams)` ๐). |
|||
|
|||
๐ ๐ช ๐ค โ: |
|||
|
|||
```Python |
|||
commons = Depends(CommonQueryParams) |
|||
``` |
|||
|
|||
...: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="19" |
|||
{!> ../../../docs_src/dependencies/tutorial003.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="17" |
|||
{!> ../../../docs_src/dependencies/tutorial003_py310.py!} |
|||
``` |
|||
|
|||
โ๏ธ ๐ฃ ๐ ๐ก ๐ ๐ ๐ ๐จโ๐จ ๐ ๐ญ โซ๏ธโ ๐ ๐ถโโ๏ธ ๐ข `commons`, & โคด๏ธ โซ๏ธ ๐ช โน ๐ โฎ๏ธ ๐ ๐ ๏ธ, ๐ โ
, โ๏ธ: |
|||
|
|||
<img src="/img/tutorial/dependencies/image02.png"> |
|||
|
|||
## โจ |
|||
|
|||
โ๏ธ ๐ ๐ ๐ ๐ฅ โ๏ธ ๐ ๐ ๐ฅ, โ `CommonQueryParams` ๐: |
|||
|
|||
```Python |
|||
commons: CommonQueryParams = Depends(CommonQueryParams) |
|||
``` |
|||
|
|||
**FastAPI** ๐ โจ ๐ซ ๐ผ, ๐โ ๐ *๐ฏ* ๐ ๐ **FastAPI** ๐ "๐ค" โ ๐ ๐ โซ๏ธ. |
|||
|
|||
๐ ๐ฏ ๐ผ, ๐ ๐ช ๐: |
|||
|
|||
โฉ๏ธ โ: |
|||
|
|||
```Python |
|||
commons: CommonQueryParams = Depends(CommonQueryParams) |
|||
``` |
|||
|
|||
...๐ โ: |
|||
|
|||
```Python |
|||
commons: CommonQueryParams = Depends() |
|||
``` |
|||
|
|||
๐ ๐ฃ ๐ ๐ ๐ข, & ๐ โ๏ธ `Depends()` ๐ฎ "๐ข" ๐ฒ (๐ โฎ๏ธ `=`) ๐ ๐ข ๐ข, ๐ต ๐ ๐ข `Depends()`, โฉ๏ธ โ๏ธ โ ๐ ๐ *๐* ๐ `Depends(CommonQueryParams)`. |
|||
|
|||
๐ ๐ผ ๐ โคด๏ธ ๐ ๐: |
|||
|
|||
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="19" |
|||
{!> ../../../docs_src/dependencies/tutorial004.py!} |
|||
``` |
|||
|
|||
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐" |
|||
|
|||
```Python hl_lines="17" |
|||
{!> ../../../docs_src/dependencies/tutorial004_py310.py!} |
|||
``` |
|||
|
|||
...& **FastAPI** ๐ ๐ญ โซ๏ธโ. |
|||
|
|||
!!! tip |
|||
๐ฅ ๐ ๐ ๐
๐จ ๐ ๐, ๐คทโโ โซ๏ธ, ๐ ๐ซ *๐ช* โซ๏ธ. |
|||
|
|||
โซ๏ธ โจ. โฉ๏ธ **FastAPI** ๐
๐ ๐ค ๐ ๐ ๐ ๐. |
@ -0,0 +1,71 @@ |
|||
# ๐ โก ๐ ๏ธ ๐จโ๐จ |
|||
|
|||
๐ผ ๐ ๐ซ ๐ค ๐ช ๐จ ๐ฒ ๐ ๐ ๐ *โก ๐ ๏ธ ๐ข*. |
|||
|
|||
โ๏ธ ๐ ๐ซ ๐จ ๐ฒ. |
|||
|
|||
โ๏ธ ๐ ๐ช โซ๏ธ ๐ ๏ธ/โ. |
|||
|
|||
๐ ๐ผ, โฉ๏ธ ๐ฃ *โก ๐ ๏ธ ๐ข* ๐ข โฎ๏ธ `Depends`, ๐ ๐ช ๐ฎ `list` `dependencies` *โก ๐ ๏ธ ๐จโ๐จ*. |
|||
|
|||
## ๐ฎ `dependencies` *โก ๐ ๏ธ ๐จโ๐จ* |
|||
|
|||
*โก ๐ ๏ธ ๐จโ๐จ* ๐จ ๐ฆ โ `dependencies`. |
|||
|
|||
โซ๏ธ ๐ `list` `Depends()`: |
|||
|
|||
```Python hl_lines="17" |
|||
{!../../../docs_src/dependencies/tutorial006.py!} |
|||
``` |
|||
|
|||
๐ ๐ ๐ ๐ ๏ธ/โ ๐ ๐ ๐ ๐. โ๏ธ ๐ซ ๐ฒ (๐ฅ ๐ซ ๐จ ๐) ๐ ๐ซ ๐ถโโ๏ธ ๐ *โก ๐ ๏ธ ๐ข*. |
|||
|
|||
!!! tip |
|||
๐จโ๐จ โ
โป ๐ข ๐ข, & ๐ฆ ๐ซ โ. |
|||
|
|||
โ๏ธ ๐ `dependencies` *โก ๐ ๏ธ ๐จโ๐จ* ๐ ๐ช โ ๐ญ ๐ซ ๐ ๏ธ โช โ ๐จโ๐จ/๐ญ โ. |
|||
|
|||
โซ๏ธ ๐ช โน โ ๐จ ๐ ๐ฉโ๐ป ๐ ๐ โป ๐ข ๐ ๐ & ๐ช ๐ญ โซ๏ธ ๐. |
|||
|
|||
!!! info |
|||
๐ ๐ผ ๐ฅ โ๏ธ ๐ญ ๐ ๐ `X-Key` & `X-Token`. |
|||
|
|||
โ๏ธ ๐ฐ ๐ผ, ๐โ ๐ ๏ธ ๐โโ, ๐ ๐ ๐ค ๐ ๐ฐ โช๏ธโก๏ธ โ๏ธ ๐ ๏ธ [๐โโ ๐ (โญ ๐)](../security/index.md){.internal-link target=_blank}. |
|||
|
|||
## ๐ โ & ๐จ ๐ฒ |
|||
|
|||
๐ ๐ช โ๏ธ ๐ ๐ *๐ข* ๐ โ๏ธ ๐. |
|||
|
|||
### ๐ ๐ |
|||
|
|||
๐ซ ๐ช ๐ฃ ๐จ ๐ (๐ ๐) โ๏ธ ๐ ๐ง-๐: |
|||
|
|||
```Python hl_lines="6 11" |
|||
{!../../../docs_src/dependencies/tutorial006.py!} |
|||
``` |
|||
|
|||
### ๐ค โ |
|||
|
|||
๐ซ ๐ ๐ช `raise` โ , ๐ ๐ ๐: |
|||
|
|||
```Python hl_lines="8 13" |
|||
{!../../../docs_src/dependencies/tutorial006.py!} |
|||
``` |
|||
|
|||
### ๐จ ๐ฒ |
|||
|
|||
& ๐ซ ๐ช ๐จ ๐ฒ โ๏ธ ๐ซ, ๐ฒ ๐ ๐ซ โ๏ธ. |
|||
|
|||
, ๐ ๐ช ๐ค-โ๏ธ ๐ ๐ (๐ ๐จ ๐ฒ) ๐ โช โ๏ธ ๐ฑ ๐, & โ๏ธ ๐ฒ ๐ ๐ซ โ๏ธ, ๐ ๐ ๐ ๏ธ: |
|||
|
|||
```Python hl_lines="9 14" |
|||
{!../../../docs_src/dependencies/tutorial006.py!} |
|||
``` |
|||
|
|||
## ๐ ๐ช *โก ๐ ๏ธ* |
|||
|
|||
โช, ๐โ ๐ ๐ โ ๐ ๐ฆ ๐ธ ([๐ฆ ๐ธ - ๐ ๐](../../tutorial/bigger-applications.md){.internal-link target=_blank}), ๐ฒ โฎ๏ธ ๐ ๐, ๐ ๐ ๐ก โ ๐ฃ ๐ `dependencies` ๐ข ๐ช *โก ๐ ๏ธ*. |
|||
|
|||
## ๐ ๐ |
|||
|
|||
โญ ๐ฅ ๐ ๐ โ ๐ฎ ๐ ๐ `FastAPI` ๐ธ, ๐ ๐ซ โ ๐ *โก ๐ ๏ธ*. |
Some files were not shown because too many files changed in this diff
Loadingโฆ
Reference in new issue