This is a template for building web applications using FastAPI framework. FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints.
To get started with this template, follow the steps below:
-
Clone this repository to your local machine:
git clone https://github.com/suhairshareef/fastapi-template.git
-
Navigate to the project directory:
cd fastapi-template
-
Create a virtual environment:
python3 -m venv venv
-
Activate the virtual environment:
source venv/bin/activate
-
Install the required dependencies:
pip install -r requirements.txt
-
Start the application:
python server.py
You should see the FastAPI application running at http://localhost:8000 in your browser.
The project structure is organized as follows:
fastapi-template/
├── migrations/
│ └── __init__.py
├── models/
│ └── __init__.py
├── routes/
│ ├── __init__.py
│ └── routes.py
├── schemas/
│ └── __init__.py
├── helpers/
│ └── __init__.py
├── tests/
│ ├── __init__.py
│ └── test_main.py
├── utils/
│ └── definitions.py
├── .env
├── server.py
├── settings.py
├── .gitignore
├── README.md
└── requirements.txt
- The
models
directory contains the data models for your application. - The
routes
directory contains the API routes implementation. - The
schemas
directory contains the schemas for request/response validation. - The
helpers
directory contains any helper functions or utilities. - The
tests
directory contains the test cases for the application. - The
utils
directory contains utility functions or classes. - The
.env
file contains environment variables for the application. - The
server.py
file is the entry point of the application. - The
settings.py
file contains application settings and configurations. - The
.gitignore
file specifies the files and directories to be ignored by Git. - The
README.md
file contains information about the template and instructions for getting started. - The
requirements.txt
file lists the Python dependencies required by the application.
API routes are defined in the ./routes/
directory. You can add your own routes and handlers in this directory.
Here's an example route that responds with "Hello, World!":
from fastapi import APIRouter
router = APIRouter()
@router.get("/")
async def read_root():
return {"message": "Hello, World!"}
You can add more routes by following the same pattern.
The template includes a basic test case for the main application. You can add more test cases in the tests directory. To run the tests, use the following command:
pytest
To deploy the FastAPI application, you can use any hosting provider or deploy it on your own server. Make sure to configure the appropriate deployment settings based on your chosen deployment method.
This template is licensed under the MIT License. See the LICENSE file for more details.