FastAPI Boilerplate
This repository provides a comprehensive boilerplate for building web applications using FastAPI. It references the educational content provided by Sanjeev Thiyagarajan's FastAPI course on YouTube and GitHub, offering a structured starting point for developers to quickly set up and deploy FastAPI projects.
Project Structure
/FastAPI_Project
├── /alembic
│ ├── /versions
│ └── env.py
├── /app
│ ├── /routers
│ │ ├── __init__.py
│ │ ├── auth.py
│ │ └── sample.py
│ ├── __init__.py
│ ├── config.py
│ ├── database.py
│ ├── main.py
│ ├── models.py
│ ├── oauth2.py
│ ├── schemas.py
│ └── utils.py
├── .env
├── .gitignore
└── alembic.ini
Plain Text
복사
Getting Started
Installation
1.
Clone this repository to your local machine using:
git clone <https://github.com/kcrmin/FastAPI_Boilerplate.git>
Shell
복사
2.
Open directory:
cd FastAPI_Boilerplate
Shell
복사
Set up a virtual environment
Setup Venv
1.
Create venv
python3 -m venv venv
Shell
복사
2.
Activate venv
source venv/bin/activate # On macOS
myenv\\Scripts\\activate # On Windows
Shell
복사
3.
Deactivate venv
deactivate
Shell
복사
Configure VSCode Interpreter
1.
Open Command Palette:
•
Option 1:
◦
Click: [Settings] View
◦
Click: [Settings] Command Palette
•
Option 2:
◦
Enter: [Shift + Command + P]
1.
Click: Python: Select Interpreter
2.
Click: Enter interpreter path…
3.
Enter: [./FastAPI_Boilerplate/venv/bin/python]
Install the required Python packages
pip install 'fastapi[all]'
pip install 'psycopg[binary, pool]'
pip install sqlalchemy
pip install 'passlib[bcrypt]'
pip install 'python-jose[cryptography]'
pip install alembic
Shell
복사
Configuration
1.
Edit .env file: Store your environment variables in this file.
DATABASE_HOSTNAME=localhost
DATABASE_PORT=5432
DATABASE_PASSWORD=password1234
DATABASE_NAME=postgres
DATABASE_USERNAME=postgres
SECRET_KEY="Random_Secret_Key"
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
Plain Text
복사
2.
Edit app/routers/__init__.py: Register routers to the application.
3.
Edit app/schemas.py: Define Pydantic models.
4.
Edit app/models.py: Define SQLAlchemy models.
5.
Add new router files: Create new router files under app/routers.
Running the Application
1.
Start the FastAPI server:
uvicorn app.main:app --reload
Shell
복사
2.
Database Migrations
1.
Initialize Alembic migrations:
alembic revision --autogenerate -m "message"
Shell
복사
2.
Check the current migration:
alembic current
Shell
복사
3.
Apply the latest migration:
alembic upgrade head
Shell
복사
4.
Revert the last migration:
alembic downgrade -1
Shell
복사
Resources
•
•
