Code Monkey home page Code Monkey logo

fastapi-production-boilerplate's Introduction

FastAPI Boilerplate for Production Development with MySql and Alembic Migrations

FastAPI

FastAPI framework, high performance, easy to learn, fast to code, ready for production

Technology Stack:

  • Python version 3.7 or above
  • FastAPI
  • Uvicorn (server)
  • Sqlalchemy
  • MySql
  • Alembic (database migrations)

Directory Tree

fastapi-production-boilerplate
┣ project
┃ ┣ alembic
┃ ┃ ┣ versions
┃ ┃ ┣ env.py
┃ ┃ ┣ README
┃ ┃ ┗ script.py.mako
┃ ┣ common
┃ ┃ ┣ auth.py
┃ ┃ ┗ utility.py
┃ ┣ constant
┃ ┃ ┗ status_constant.py
┃ ┣ database
┃ ┃ ┗ database.py
┃ ┣ endpoints
┃ ┃ ┣ admin_app
┃ ┃ ┃ ┣ authentication.py
┃ ┃ ┃ ┗ __init__.py
┃ ┃ ┣ client_app
┃ ┃ ┃ ┣ client_authentication.py
┃ ┃ ┃ ┗ __init__.py
┃ ┃ ┗ __init__.py
┃ ┣ media
┃ ┃ ┣ excels
┃ ┃ ┃ ┗ user_excels
┃ ┃ ┗ images
┃ ┃   ┗ profile_pics
┃ ┣ models
┃ ┃ ┣ cli_client.py
┃ ┃ ┗ usr_user.py
┃ ┣ routes
┃ ┃ ┗ api.py
┃ ┣ schemas
┃ ┃ ┃ ┣ login.cpython-310.pyc
┃ ┃ ┃ ┗ register.cpython-310.pyc
┃ ┃ ┣ login.py
┃ ┃ ┗ register.py
┃ ┣ alembic.ini
┃ ┣ test.py
┃ ┗ __init__.py
┣ .env
┣ .gitignore
┣ application.py
┣ README.md
┗ requirements.txt

How to start the project ?

git clone https://github.com/Hunnyjain7/fastapi-production-boilerplate.git
cd fastapi-production-boilerplate
python -m venv env                  #create a virtual environment
.\env\Scripts\activate              #activate your virtual environment
pip install -r requirements.txt
update your database connection string in .env
uvicorn application:app --reload    #start server use --host for host if required
visit Welcome screen at 127.0.0.1:8000 
visit Swagger UI docs screen at 127.0.0.1:8000/docs  # here all api routing and request can be triggered...

Guide towards the Alembic Migrations for SqlAlchemy in FastAPI

* Install alembic
pip install alembic

step 1: cd project then alembic init alembic

step 2: mention your mysql connection string inside sqlalchemy.url

step 3: in env.py file inside alembic dir mention all models inside target_metadata this way
target_metadata = [usr_user.Base.metadata, cli_client.Base.metadata]

step 4: alembic revision --autogenerate -m "message to identify migration"

step 5: now below command will migrate all changes to the database
alembic upgrade head

step 6: whenever you make changes into the models then create the revision and upgrade it, even if you remove fields from the models upgrade command will be used
alembic revision --autogenerate -m "second migration message"
followed by alembic upgrade first three or four initials of your recent version created in my case command was
alembic upgrade 2a43 

to downgrade the recent migration simply alembic downgrade first three or four initials of your recent version created in my case command was
alembic downgrade 2a43

fastapi-production-boilerplate's People

Contributors

hunnyjain7 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.