A FastAPI boilerplate for efficient project setup.
- Fork the repository and clone it:
git clone https://github.com/<username>/hng_boilerplate_python_fastapi_web.git
- Navigate into the project directory:
cd hng_boilerplate_python_fastapi_web
- Switch to the development branch (if not already on
dev
):git checkout dev
- Create a virtual environment:
python3 -m venv .venv
- Activate the virtual environment:
- On macOS/Linux:
source .venv/bin/activate
- On Windows (PowerShell):
.venv\Scripts\Activate
- On macOS/Linux:
- Install project dependencies:
pip install -r requirements.txt
- Create a
.env
file from.env.sample
:cp .env.sample .env
- Start the server:
python main.py
When setting up the database, you need to replace placeholders with your actual values. Below is a breakdown of where to replace them:
CREATE USER user WITH PASSWORD 'your_password';
πΉ Replace:
user
β Your preferred database username (e.g.,fastapi_user
).'your_password'
β A secure password for the user (e.g.,'StrongP@ssw0rd'
).
β Example:
CREATE USER fastapi_user WITH PASSWORD 'StrongP@ssw0rd';
CREATE DATABASE hng_fast_api;
πΉ Replace:
hng_fast_api
β Your preferred database name (e.g.,fastapi_db
).
β Example:
CREATE DATABASE fastapi_db;
GRANT ALL PRIVILEGES ON DATABASE hng_fast_api TO user;
πΉ Replace:
hng_fast_api
β The database name you used in Step 2.user
β The username you created in Step 1.
β Example:
GRANT ALL PRIVILEGES ON DATABASE fastapi_db TO fastapi_user;
Edit the .env
file to match your setup.
DATABASE_URL=postgresql://user:your_password@localhost/hng_fast_api
πΉ Replace:
user
β Your database username.your_password
β Your database password.hng_fast_api
β Your database name.
β Example:
DATABASE_URL=postgresql://fastapi_user:StrongP@ssw0rd@localhost/fastapi_db
After setting up the database, test the connection:
psql -U user -d hng_fast_api -h localhost
πΉ Replace:
user
β Your database username.hng_fast_api
β Your database name.
β Example:
psql -U fastapi_user -d fastapi_db -h localhost
alembic upgrade head
Do NOT run alembic revision --autogenerate -m 'initial migration'
initially!
alembic revision --autogenerate -m 'your migration message'
alembic upgrade head
python3 seed.py
- After creating new tables or modifying models:
- Run Alembic migrations:
alembic revision --autogenerate -m "Migration message" alembic upgrade head
- Ensure you import new models into
api/v1/models/__init__.py
. - You do NOT need to manually import them in
alembic/env.py
.
- Run Alembic migrations:
- Check if a related route file already exists in
api/v1/routes/
.- If yes, add your route inside the existing file.
- If no, create a new file following the naming convention.
- Define the router inside the new route file:
- Include the prefix (without
/api/v1
since it's already handled).
- Include the prefix (without
- Register the router in
api/v1/routes/__init__.py
:from .new_route import router as new_router api_version_one.include_router(new_router)
Ensure pytest
is installed in your virtual environment:
pip install pytest
From the project root directory, run:
pytest
This will automatically discover and execute all test files in the tests/
directory.
To run tests in a specific model directory (e.g., tests/v1/user/
):
pytest tests/v1/user/
To run tests from a specific test file (e.g., test_signup.py
inside tests/v1/auth/
):
pytest tests/v1/auth/test_signup.py
If you want to run a specific test inside a file, use:
pytest tests/v1/auth/test_signup.py::test_user_signup
For verbose output, add the -v
flag:
pytest -v
To check test coverage, install pytest-cov
:
pip install pytest-cov
Then run:
pytest --cov=api
If you encounter this issue when running:
alembic revision --autogenerate -m 'your migration message'
Run the following command first:
alembic upgrade head
Then retry:
alembic revision --autogenerate -m 'your migration message'
- Test your endpoints and models before pushing changes.
- Push Alembic migrations if database models are modified.
- Ensure your code follows project standards and passes tests before submitting a pull request.