Giter Site home page Giter Site logo

pavelprokhorenko / async-fastapi-template Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 104 KB

⏳ Fully asynchronous FastAPI template

Dockerfile 1.16% Python 97.17% Mako 1.05% Shell 0.62%
python api alembic async asynchronous docker fastapi json openapi pydantic pytest rest-api swagger web jwt jwt-authentication postgres postgresql sqlalchemy pytest-asyncio

async-fastapi-template's Introduction

Web

Dependencies

Config

  • Create ".env" file and add environment variables to it:
# Base
SECRET_KEY=some-secret-key
PROJECT_NAME='Some Project Name'
SERVER_HOST=http://0.0.0.0:8881

# Databases
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=postgres
POSTGRES_DB=postgres
POSTGRES_TEST_DB=test
PGDATA=/var/lib/postgresql/data/pgdata

# SMTP
SMTP_USER=some-email
SMTP_PASSWORD=some-password

# Auth
FIRST_SUPERUSER_USERNAME=[email protected]
FIRST_SUPERUSER_PASSWORD=password
FIRST_SUPERUSER_FIRST_NAME='first name'
FIRST_SUPERUSER_LAST_NAME='last name'
  • To generate 32 byte SECRET_KEY:
$ openssl rand -hex 32
  • Other project settings (e.g. SMPT) you can see in "app/core/config.py"

Development

  • Run server with all dependencies:
$ docker-compose up --build
  • Add pre-commit hook
$ pre-commit install

Tests

Testing is done with pytest, tests are placed in the "tests" directory

For testing, you should go inside the container and run the command pytest .

$ docker exec -it <FastAPI container ID> bash
$ pytest tests --asyncio-mode=auto

Project structure

-> *app*
   -> *api* — layer with logic for processing requests via api
   -> *core* — global things for the project, like settings (`config.py`)
   -> *db* — database and session initialization
   -> *models* — models in SQLAlchemy terminology (not to be confused with *schemas* in pydantic and business models)
   -> *schemas* — schemes for validating/serializing request/response objects (they are also models in pydantic terminology)
   -> *services* — service layer, all business logic is placed here.
   -> *utils* — utility logic.
-> *tests* — root for tests
-> *.env.template* — file to list all environment variables used inside the service

Migrations

  • After creating the model in app/models, you need to import it in app/db/base.py (in order to make it visible to alembic)
  • Make migrations (run inside container)
$ alembic -n postgres revision --autogenerate -m "add column last_name to User model"
  • Run migrations
$ alembic -n postgres upgrade head
  • Postgres
$ alembic -n postgres revision --autogenerate -m "text"
$ alembic -n postgres upgrade head
$ alembic -n postgres downgrade -1

async-fastapi-template's People

Contributors

pavelprokhorenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.