Skip to content

Commit 15783d9

Browse files
committed
readme update
1 parent 5f947ce commit 15783d9

File tree

1 file changed

+201
-12
lines changed

1 file changed

+201
-12
lines changed

README.md

Lines changed: 201 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,163 @@
11
# FastAPI Blog Project
22

3-
## Database Setup
3+
A RESTful Blog API built with FastAPI, SQLAlchemy, and JWT Authentication.
4+
5+
## Features
6+
7+
- 🔐 **JWT Authentication** - Secure token-based authentication
8+
- 👤 **User Management** - Create, read, update, delete users
9+
- 📝 **Blog Management** - Full CRUD operations for blog posts
10+
- 🔒 **Authorization** - Users can only modify their own data
11+
- 📚 **API Documentation** - Auto-generated Swagger UI and ReDoc
12+
- 🧪 **Testing** - Comprehensive test suite with pytest
13+
14+
## Project Structure
15+
16+
```
17+
FastAPI_Blog_Project/
18+
├── app/
19+
│ ├── api/v1/routes/ # API route handlers
20+
│ │ ├── auth.py # Authentication endpoints
21+
│ │ ├── blog.py # Blog endpoints
22+
│ │ └── user.py # User endpoints
23+
│ ├── core/ # Core configurations
24+
│ │ ├── config.py # Settings management
25+
│ │ ├── database.py # Database connection
26+
│ │ └── security.py # JWT & password utilities
27+
│ ├── models/ # SQLAlchemy models
28+
│ ├── schemas/ # Pydantic schemas
29+
│ └── services/ # Business logic
30+
├── migrations/ # Alembic migrations
31+
├── tests/ # Test suite
32+
└── .env # Environment variables
33+
```
34+
35+
## Setup
436

537
### Prerequisites
38+
39+
- Python 3.10+
640
- MySQL server running on localhost:3306
741
- Database `blog_db` created
8-
- User credentials configured in `.env`
942

10-
### Environment Variables
11-
Configure your `.env` file:
43+
### Installation
44+
45+
1. Clone the repository:
46+
```bash
47+
git clone <repository-url>
48+
cd FastAPI_Blog_Project
49+
```
50+
51+
2. Create and activate virtual environment:
52+
```bash
53+
python -m venv .venv
54+
source .venv/bin/activate # On Windows: .venv\Scripts\activate
55+
```
56+
57+
3. Install dependencies:
58+
```bash
59+
pip install -r requirements.txt
1260
```
61+
62+
4. Configure environment variables - create a `.env` file:
63+
```env
1364
DATABASE_URL=mysql+pymysql://user:password@localhost:3306/blog_db
14-
SECRET_KEY="your-secret-key"
65+
SECRET_KEY="your-secret-key-here"
1566
ALGORITHM=HS256
1667
ACCESS_TOKEN_EXPIRE_MINUTES=30
1768
```
1869

70+
> **Tip:** Generate a secure secret key with: `openssl rand -base64 32`
71+
72+
5. Run database migrations:
73+
```bash
74+
alembic upgrade head
75+
```
76+
77+
## Running the Application
78+
79+
### Development Server
80+
```bash
81+
fastapi dev
82+
```
83+
84+
### Production Server
85+
```bash
86+
fastapi run
87+
```
88+
89+
The API will be available at:
90+
- **API:** `http://127.0.0.1:8000`
91+
- **Swagger Docs:** `http://127.0.0.1:8000/docs`
92+
- **ReDoc:** `http://127.0.0.1:8000/redoc`
93+
94+
## API Endpoints
95+
96+
### Authentication
97+
98+
| Method | Endpoint | Description | Auth Required |
99+
|--------|----------|-------------|---------------|
100+
| POST | `/api/v1.1/auth/login` | Login and get JWT token ||
101+
102+
### Users
103+
104+
| Method | Endpoint | Description | Auth Required |
105+
|--------|----------|-------------|---------------|
106+
| POST | `/api/v1.1/users/` | Create a new user ||
107+
| GET | `/api/v1.1/users/` | Get all users ||
108+
| GET | `/api/v1.1/users/me` | Get current user ||
109+
| GET | `/api/v1.1/users/{id}` | Get user by ID ||
110+
| PUT | `/api/v1.1/users/{id}` | Update user | ✅ (own profile only) |
111+
| DELETE | `/api/v1.1/users/{id}` | Delete user | ✅ (own account only) |
112+
113+
### Blogs
114+
115+
| Method | Endpoint | Description | Auth Required |
116+
|--------|----------|-------------|---------------|
117+
| POST | `/api/v1.1/blogs/` | Create a new blog ||
118+
| GET | `/api/v1.1/blogs/` | Get all blogs ||
119+
| GET | `/api/v1.1/blogs/{id}` | Get blog by ID ||
120+
| PUT | `/api/v1.1/blogs/{id}` | Update blog ||
121+
| DELETE | `/api/v1.1/blogs/{id}` | Delete blog ||
122+
123+
## Authentication
124+
125+
### Login
126+
127+
```bash
128+
curl -X POST "http://127.0.0.1:8000/api/v1.1/auth/login" \
129+
-d "username=your_email@example.com&password=your_password"
130+
```
131+
132+
**Response:**
133+
```json
134+
{
135+
"access_token": "eyJhbGciOiJIUzI1NiIs...",
136+
"token_type": "bearer"
137+
}
138+
```
139+
140+
### Using the Token
141+
142+
Include the token in the `Authorization` header:
143+
144+
```bash
145+
curl -X GET "http://127.0.0.1:8000/api/v1.1/users/me" \
146+
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."
147+
```
148+
149+
### Swagger UI Authentication
150+
151+
1. Go to `http://127.0.0.1:8000/docs`
152+
2. Click the **Authorize** button (🔓)
153+
3. Enter your email as username and password
154+
4. Click **Authorize**
155+
5. All protected endpoints will now include your token automatically
156+
19157
## Alembic Migrations
20158

21159
### Generate a New Migration
22-
After making changes to your models, generate a migration:
160+
After making changes to your models:
23161
```bash
24162
alembic revision --autogenerate -m "description of changes"
25163
```
@@ -46,16 +184,67 @@ alembic history
46184
alembic current
47185
```
48186

49-
## Running the Application
187+
## Testing
50188

51-
### Development Server
189+
### Run All Tests
52190
```bash
53-
fastapi dev
191+
pytest
54192
```
55193

56-
### Production Server
194+
### Run with Coverage
57195
```bash
58-
fastapi run
196+
pytest --cov=app --cov-report=html
59197
```
60198

61-
The API will be available at `http://127.0.0.1:8000` with docs at `/docs`.
199+
### Run Specific Test File
200+
```bash
201+
pytest tests/test_users.py
202+
pytest tests/test_blogs.py
203+
```
204+
205+
### Run Specific Test Class
206+
```bash
207+
pytest tests/test_users.py::TestAuthentication
208+
pytest tests/test_blogs.py::TestCreateBlog
209+
```
210+
211+
### Run with Verbose Output
212+
```bash
213+
pytest -v
214+
```
215+
216+
## Example Usage
217+
218+
### Create a User
219+
```bash
220+
curl -X POST "http://127.0.0.1:8000/api/v1.1/users/" \
221+
-H "Content-Type: application/json" \
222+
-d '{"name": "John Doe", "email": "john@example.com", "password": "securepass123"}'
223+
```
224+
225+
### Login
226+
```bash
227+
curl -X POST "http://127.0.0.1:8000/api/v1.1/auth/login" \
228+
-d "username=john@example.com&password=securepass123"
229+
```
230+
231+
### Create a Blog (Authenticated)
232+
```bash
233+
curl -X POST "http://127.0.0.1:8000/api/v1.1/blogs/" \
234+
-H "Content-Type: application/json" \
235+
-H "Authorization: Bearer <your-token>" \
236+
-d '{"title": "My First Blog", "content": "Hello World!", "author_id": 1}'
237+
```
238+
239+
### Get All Blogs
240+
```bash
241+
curl -X GET "http://127.0.0.1:8000/api/v1.1/blogs/"
242+
```
243+
244+
## License
245+
246+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
247+
248+
## Author
249+
250+
**Vinald** - [vinald.me](http://vinald.me)

0 commit comments

Comments
 (0)