This repository contains a template for creating a FastAPI project with structured organization and key features like error logging, environment variable configuration, and class-based API organization.
Follow these instructions to set up and run the project on your local machine.
Make sure you have the following software installed on your machine:
- Python 3.7+
- pip (Python package manager)
-
Clone the repository to your local machine:
git clone https://github.com/yourusername/fastapi-project-template.git
-
Navigate to the project directory:
cd fastapi-project-template
-
Install project dependencies using pip:
pip install -r requirements.txt
-
Running the Server
To run the FastAPI server, execute the following command:
uvicorn app:app --host ${APP_HOST} --port ${APP_PORT} --reload
-
Create a
.env
file in the root directory of the project. -
Define your configuration variables in the
.env
file. Here's an example:# FastAPI settings APP_HOST=0.0.0.0 APP_PORT=8000 # Logging settings LOG_LEVEL=DEBUG LOG_FILE=log.log
To run the FastAPI server, execute the following command:
python app/main.py
By default, the server will listen on 0.0.0.0:8000
(or as configured in the .env
file).
Once the server is running, you can access the API documentation at http://localhost:8000/docs
.
app/
: Main application directory.main.py
: Entrypoint for the FastAPI application.routers/
: Directory to store router modules.user.py
: Router module for user-related endpoints.docs.py
: Router module for serving API documentation.
templates/
: Directory to store HTML templates.swagger_ui.html
: HTML template for Swagger UI.
settings.py
: Configuration settings for the application.decorators.py
: Custom decorators for error logging and other functionalities.
.env
: File to store environment variables.requirements.txt
: List of Python dependencies for the project.
Feel free to contribute to this project by opening issues or pull requests. Any feedback or improvements are welcome!
This project is licensed under the MIT License - see the LICENSE file for details.