FastAPI is a high performance, easy to learn, fast to code and ready for production python framework. It's easy to start a FastAPI project in a single file as we will see in Demo versions.
from fastapi import FastAPI
app = FastAPI()
@app.route('/')
def index():
return {'greeting': 'hello world!'}
FastAPI allows you to define your own file structure and this can be quite challenging, especially for newbies. That is where this repo comes in. fastapi_structure sets the scalfolding standard that you can adopt and be sure your application can scale without a problem.
+-- fastapi_structure
| +-- application/
| +-- blog/
| +-- __init__.py
| +-- controller.py
| +-- models.py
| +-- routes.py
| +-- schema.py
| +-- session/
| +-- __init__.py
| +-- controller.py
| +-- models.py
| +-- routes.py
| +-- schema.py
| +-- utils/
| +-- __init__.py
| +-- config.py
| +-- factory.py
| +-- __init__.py
| +-- main.py
In this file structure, we have two sub applciations, blog and session. These are so independent that they can be ported to another project with little or no change. In your own case, these may be called something else. The idea here is that each sub application has the exact same files:
- init.py : An init file, this can be an empty file, but it has to be present for python to interpret this folder as a module.
- controllers.py: This is where you define your functions that actually perform specific tasks.
- models.py: If you are using an ORM like SQLAlchemcy, then this is where you define the models for the specific module.
- routes.py: You can as well call this
views.pybut I usually preferroutesbecause it makes it more explicit. Here you define your routes. Each route will call controller function to perfom the requested task. - schema: If you are using a serializer, this is where you will define your serializer models or classes.
To begin using this repo:
- Fork the repo, so you can have a copy of yoru own.
- Clone the repo to you local development environment
- Open your IDE and navigate to the folder to open the project
- Create a virtual environment and install all dependencies found in requirements.txt
- run
python main.py
Start modifying as needed. If you encounter any difficulties, don't hesitate to open an issue.
Happy Coding!! and happy FastAPI Life!