-
Path
validation params of url
-
Query
validation params of query
-
Body
validation params of request body
-
Field
validation field of request body
-
@validator
validation field of request/response body (additional checkers)
-
APIRouter
create group of endpoints, indicate prefix and tags
add to main app for work
- from fastapi import APIRouter
- app.include_router(router_product)
- response_model
parameter in endpoint, for check response
- response_model_exclude
exclude fields in response
- response_model_include
include fields in response
- from fastapi.responses import HTMLResponse, JSONResponse, FileResponse, RedirectResponse
- from fastapi import HTTPException
raise exception for response (usually errors)
- crud.py - functions for endpoints products group
- views.py - bind endpoints and functions products group
- schemas.py - all models of products group
- helper.py - additional functions and classes for products group
- Typing [List, Dict, Set etc.]
validation additional data types
- uvicorn main:app --reload
- uvicorn main:app --reload --port 80 => https://127.0.0.1/docs
- uvicorn main:app --reload --port 8000 --host 0.0.0.0
run server with reload
for install uvicorn pip install uvicorn
- base python3.8(or python3.9) without libraries
- pip install flask/ pydantic/ uvicorn etc... all requrements
- specifying files and folders that cannot be added to github
- venv
- sqlite3
- specifying requirement libraries
- for collect requirements
pip freeze > requirements.txt
- fastapi
- sqlalchemy
- uvicorn
- pydantic
-
deploy - развернуть приложение
-
собрать зависимости и запустить приложение - Docker
-
depends: OS, python, libraries, external(db, other service etc.)
-
command for docker:
docker images
- посмотреть список уставновленных docker-образовdocker run python:3.9-alpine
- run docker-image (if first time, download it)docker run python:3.9-alpine -h 0.0.0.0 -p 8000:8000
run docker-image with arguments: h=0.0.0.0, p=8000:8000docker ps -a
- see all docker-containersdocker stop 14c5d9385025
anddocker rm 14c5d9385025
stop and remove docker-containerdocker rmi b908778bd1b0
- remove docker-image by iddocker build . -t python-6-fastapi-basic
- create image by Dockerfiledocker run -p 80:8000 python-6-fastapi-basic
- run my application FAST APIdocker run -p 80:8000 -it python-6-fastapi-basic
- run container, enter terminal in containerdocker run -p 80:8000 -d python-6-fastapi-basic
- run container in daemondocker exec -it 14c5d9385025 /bin/bash
- enter ti container in terminaldocker logs -f 14c5d9385025
- see logs in container
-
command for dockerfile:
- pip install -r requirements.txt
- COPY
- CMD
- FROM