Skip to content

Commit 2a57542

Browse files
authored
Merge pull request #1158 from Ayobamidele/chore/readme_update
chore: Removed outdated setup instructions to follow current standards
2 parents d6109fd + de8b3d2 commit 2a57542

File tree

2 files changed

+216
-75
lines changed

2 files changed

+216
-75
lines changed

README.md

Lines changed: 216 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,257 @@
1-
# FASTAPI
2-
FastAPI boilerplate
1+
# **FASTAPI Boilerplate**
2+
A FastAPI boilerplate for efficient project setup.
33

4-
## Setup
4+
## **Cloning the Repository**
55

6-
1. Create a virtual environment.
7-
```sh
8-
python3 -m venv .venv
9-
```
10-
2. Activate virtual environment.
11-
```sh
12-
source /path/to/venv/bin/activate`
13-
```
14-
3. Install project dependencies `pip install -r requirements.txt`
15-
4. Create a .env file by copying the .env.sample file
16-
`cp .env.sample .env`
6+
1. **Fork the repository** and clone it:
7+
```sh
8+
git clone https://github.com/<username>/hng_boilerplate_python_fastapi_web.git
9+
```
10+
2. **Navigate into the project directory**:
11+
```sh
12+
cd hng_boilerplate_python_fastapi_web
13+
```
14+
3. **Switch to the development branch** (if not already on `dev`):
15+
```sh
16+
git checkout dev
17+
```
1718

18-
5. Start server.
19-
```sh
20-
python main.py
21-
```
2219

23-
## **DATABASE TEST SETUP**
20+
## **Setup Instructions**
21+
22+
1. **Create a virtual environment**:
23+
```sh
24+
python3 -m venv .venv
25+
```
26+
2. **Activate the virtual environment**:
27+
- On macOS/Linux:
28+
```sh
29+
source .venv/bin/activate
30+
```
31+
- On Windows (PowerShell):
32+
```sh
33+
.venv\Scripts\Activate
34+
```
35+
3. **Install project dependencies**:
36+
```sh
37+
pip install -r requirements.txt
38+
```
39+
4. **Create a `.env` file** from `.env.sample`:
40+
```sh
41+
cp .env.sample .env
42+
```
43+
5. **Start the server**:
44+
```sh
45+
python main.py
46+
```
2447

25-
To set up the database, follow the following steps:
48+
---
2649

27-
**Cloning**
28-
- clone the repository using `git clone https://github.com/hngprojects/hng_boilerplate_python_fastapi_web`
29-
- `cd` into the directory hng_boilerplate_python_fastapi_web
30-
- switch branch using `git checkout backend`
50+
## **Database Setup**
3151

32-
**Environment Setup**
33-
- run `pip install -r requrements.txt` to install dependencies
34-
- create a `.env` file in the root directory and copy the content of `.env.sample` and update it accordingly
52+
### **Replacing Placeholders in Database Setup**
3553

36-
**Create your local database**
37-
```bash
38-
sudo -u root psql
54+
When setting up the database, you need to replace **placeholders** with your actual values. Below is a breakdown of where to replace them:
55+
56+
---
57+
58+
## **Step 1: Create a Database User**
59+
```sql
60+
CREATE USER user WITH PASSWORD 'your_password';
3961
```
62+
🔹 **Replace:**
63+
- `user` → Your **preferred database username** (e.g., `fastapi_user`).
64+
- `'your_password'` → A **secure password** for the user (e.g., `'StrongP@ssw0rd'`).
65+
66+
**Example:**
67+
```sql
68+
CREATE USER fastapi_user WITH PASSWORD 'StrongP@ssw0rd';
69+
```
70+
71+
---
72+
73+
## **Step 2: Create the Database**
4074
```sql
41-
CREATE USER user WITH PASSWORD 'your desired password';
4275
CREATE DATABASE hng_fast_api;
76+
```
77+
🔹 **Replace:**
78+
- `hng_fast_api` → Your **preferred database name** (e.g., `fastapi_db`).
79+
80+
**Example:**
81+
```sql
82+
CREATE DATABASE fastapi_db;
83+
```
84+
85+
---
86+
87+
## **Step 3: Grant Permissions**
88+
```sql
4389
GRANT ALL PRIVILEGES ON DATABASE hng_fast_api TO user;
4490
```
91+
🔹 **Replace:**
92+
- `hng_fast_api` → The **database name you used** in Step 2.
93+
- `user` → The **username you created** in Step 1.
4594

46-
**Starting the database**
47-
after cloning the database, dont run
48-
`alembic revision --autogenerate -m 'initial migration'`
49-
but run
50-
`alembic upgrade head`
95+
**Example:**
96+
```sql
97+
GRANT ALL PRIVILEGES ON DATABASE fastapi_db TO fastapi_user;
98+
```
5199

52-
if you make changes to any table locally, then run the below command.
53-
```bash
54-
alembic revision --autogenerate -m 'initial migration'
55-
alembic upgrade head
100+
---
101+
102+
## **Step 4: Update `.env` File**
103+
Edit the `.env` file to match your setup.
104+
105+
```env
106+
DATABASE_URL=postgresql://user:your_password@localhost/hng_fast_api
56107
```
108+
🔹 **Replace:**
109+
- `user` → Your **database username**.
110+
- `your_password` → Your **database password**.
111+
- `hng_fast_api` → Your **database name**.
57112

58-
**create dummy data**
59-
```bash
60-
python3 seed.py
113+
**Example:**
114+
```env
115+
DATABASE_URL=postgresql://fastapi_user:StrongP@ssw0rd@localhost/fastapi_db
61116
```
62117

118+
---
63119

64-
**Adding tables and columns to models**
120+
## **Step 5: Verify Connection**
121+
After setting up the database, test the connection:
65122

66-
After creating new tables, or adding new models. Make sure to run alembic revision --autogenerate -m "Migration messge"
123+
```sh
124+
psql -U user -d hng_fast_api -h localhost
125+
```
126+
🔹 **Replace:**
127+
- `user` → Your **database username**.
128+
- `hng_fast_api` → Your **database name**.
67129

68-
After creating new tables, or adding new models. Make sure you import the new model properly in th 'api/v1/models/__init__.py file
130+
**Example:**
131+
```sh
132+
psql -U fastapi_user -d fastapi_db -h localhost
133+
```
69134

70-
After importing it in the init file, you need not import it in the /alembic/env.py file anymore
135+
## **Step 6: Run database migrations**
136+
```sh
137+
alembic upgrade head
138+
```
139+
_Do NOT run `alembic revision --autogenerate -m 'initial migration'` initially!_
71140

141+
## **Step 7: If making changes to database models, update migrations**
142+
```sh
143+
alembic revision --autogenerate -m 'your migration message'
144+
alembic upgrade head
145+
```
146+
## **Step 8: Seed dummy data**
147+
```sh
148+
python3 seed.py
149+
```
72150

73-
**Adding new routes**
151+
---
74152

75-
To add a new route, confirm if a file relating to that route is not already created. If it is add the route in that file using the already declared router
153+
## **Adding Tables and Columns**
76154

77-
If the there is no file relating to the route in the 'api/v1/routes/' directory create a new one following the naming convention
155+
1. **After creating new tables or modifying models**:
156+
- Run Alembic migrations:
157+
```sh
158+
alembic revision --autogenerate -m "Migration message"
159+
alembic upgrade head
160+
```
161+
- Ensure you **import new models** into `api/v1/models/__init__.py`.
162+
- You do NOT need to manually import them in `alembic/env.py`.
78163

79-
After creating the new route file, declare the router and add the prefix as well as the tag
164+
---
80165

81-
The prefix should not include the base prefix ('/api/v1') as it is already includedin the base `api_version_one` router
166+
## **Adding New Routes**
82167

83-
After creating the router, import it in the 'api/v1/routes/__init__.py' file and include the router in the `api_version_one` router using
84-
```python
85-
api_version_one.include_router(<router_name>)
168+
1. **Check if a related route file already exists** in `api/v1/routes/`.
169+
- If yes, add your route inside the existing file.
170+
- If no, create a new file following the naming convention.
171+
2. **Define the router** inside the new route file:
172+
- Include the prefix (without `/api/v1` since it's already handled).
173+
3. **Register the router in `api/v1/routes/__init__.py`**:
174+
```python
175+
from .new_route import router as new_router
176+
api_version_one.include_router(new_router)
177+
```
178+
179+
---
180+
181+
## **Running Tests with Pytest**
182+
183+
### **Install Pytest**
184+
Ensure `pytest` is installed in your virtual environment:
185+
```sh
186+
pip install pytest
187+
```
188+
189+
### **Run all tests in the project**
190+
From the **project root directory**, run:
191+
```sh
192+
pytest
193+
```
194+
This will automatically discover and execute all test files in the `tests/` directory.
195+
196+
### **Run tests in a specific directory**
197+
To run tests in a specific model directory (e.g., `tests/v1/user/`):
198+
```sh
199+
pytest tests/v1/user/
200+
```
201+
202+
### **Run a specific test file**
203+
To run tests from a specific test file (e.g., `test_signup.py` inside `tests/v1/auth/`):
204+
```sh
205+
pytest tests/v1/auth/test_signup.py
206+
```
207+
208+
### **Run a specific test function**
209+
If you want to run a specific test inside a file, use:
210+
```sh
211+
pytest tests/v1/auth/test_signup.py::test_user_signup
86212
```
87213
88-
## TEST THE ENDPOINT
89-
- run the following code
214+
### **Run tests with detailed output**
215+
For verbose output, add the `-v` flag:
216+
```sh
217+
pytest -v
90218
```
91-
python -m unittest tests/v1/test_login.py
92-
python -m unittest tests/v1/test_signup.py
219+
220+
### **Run tests and generate coverage report**
221+
To check test coverage, install `pytest-cov`:
222+
```sh
223+
pip install pytest-cov
224+
```
225+
Then run:
226+
```sh
227+
pytest --cov=api
93228
```
94229
95-
## Issues
96-
if you encounter the following Error, when you run the code below
230+
---
97231
98-
**alembic revision --autogenerate -m 'your migration message'**
232+
## **Common Migration Issues & Solutions**
99233
234+
### **Error: "Target database is not up to date."**
235+
If you encounter this issue when running:
236+
```sh
237+
alembic revision --autogenerate -m 'your migration message'
100238
```
101-
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
102-
INFO [alembic.runtime.migration] Will assume transactional DDL.
103-
ERROR [alembic.util.messaging] Target database is not up to date.
104-
FAILED: Target database is not up to date.
239+
#### **Solution**:
240+
Run the following command first:
241+
```sh
242+
alembic upgrade head
105243
```
244+
Then retry:
245+
```sh
246+
alembic revision --autogenerate -m 'your migration message'
247+
```
248+
249+
---
250+
251+
## **Contribution Guidelines**
106252
107-
## Solutions
108-
Run the following code below first to update the datebase
109-
**alembic upgrade head**
110-
then, run this again.
111-
**alembic revision --autogenerate -m 'your migration message'**
253+
- **Test your endpoints and models** before pushing changes.
254+
- **Push Alembic migrations** if database models are modified.
255+
- Ensure your code **follows project standards** and **passes tests** before submitting a pull request.
112256
113-
## update
114-
please make sure to test your endpoint or model before pushing.
115-
push your alembic migrations.
257+
💯 Happy coding!

api/utils/logger.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22

3-
# Configure the logging
43
logging.basicConfig(
54
level=logging.ERROR,
65
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",

0 commit comments

Comments
 (0)