An open API service indexing awesome lists of open source software.

https://github.com/yasharthbajpai/blog-application

A modern, secure blog application built with FastAPI, featuring JWT authentication and SQLAlchemy ORM.
https://github.com/yasharthbajpai/blog-application

crud fastapi python3 sqllite

Last synced: about 2 months ago
JSON representation

A modern, secure blog application built with FastAPI, featuring JWT authentication and SQLAlchemy ORM.

Awesome Lists containing this project

README

        

# FastAPI Blog Application

A modern, secure blog application built with FastAPI, featuring JWT authentication and SQLAlchemy ORM.

## Features

- πŸ” JWT Authentication
- πŸ‘₯ User Management
- πŸ“ Blog Post Management
- πŸ”’ Secure Password Hashing
- πŸ—„οΈ SQLite Database (can be configured for other databases)
- πŸ“š API Documentation with Swagger UI

## Tech Stack

- FastAPI
- SQLAlchemy
- Pydantic
- JWT Authentication
- Python-Jose
- Passlib (bcrypt)
- Uvicorn

## Prerequisites

- Python 3.8+
- pip (Python package manager)

## Installation

1. Clone the repository:
```bash
git clone https://github.com/yourusername/blog.git
cd blog
```

2. Create and activate a virtual environment:
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```

3. Install dependencies:
```bash
pip install -r requirements.txt
```

## Configuration

1. Copy the `.env.example` to `.env` and update the values:
```bash
cp .env.example .env
```

2. Update the `.env` file with your secret key and database configuration.

## Running the Application

1. Start the development server:
```bash
uvicorn main:app --reload
```

2. Access the API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc

## Project Structure

```
blog-api/
β”‚
β”œβ”€β”€ main.py # Entry point of the application
β”‚
β”œβ”€β”€ routers/ # API route handlers organized by resource
β”‚ β”œβ”€β”€ __init__.py # Makes the directory a Python package
β”‚ β”œβ”€β”€ blog.py # Blog-related endpoints
β”‚ β”œβ”€β”€ user.py # User management endpoints
β”‚ └── login.py # Authentication endpoints (register, login, logout)
β”‚
β”œβ”€β”€ models.py # SQLAlchemy ORM models (User, Blog)
β”‚
β”œβ”€β”€ schemas.py # Pydantic models for request/response validation
β”‚
β”œβ”€β”€ database.py # Database connection and session management
β”‚
β”œβ”€β”€ requirements.txt # Project dependencies
β”‚
β”œβ”€β”€ .env.example # Example environment variables template
β”‚
β”œβ”€β”€ .env # Environment variables (not versioned)
β”‚
β”œβ”€β”€ .gitignore # Git ignore file
β”‚
β”œβ”€β”€ LICENSE # Project license (CC0 v1.0)
β”‚
β”œβ”€β”€ README.md # Project documentation
β”‚
β”œβ”€β”€ TABLEPLUS_GUIDE.md # Guide for database management with TablePlus
β”‚
└── test_tableplus.py # Test script for API functionality
```

### Component Details

#### Core Components

- **main.py**: Application entry point. Initializes FastAPI, includes routers, and sets up middleware.

- **models.py**: Defines SQLAlchemy ORM models:
- `User`: Represents application users with authentication details
- `Blog`: Represents blog posts linked to users

- **schemas.py**: Defines Pydantic models for:
- Request validation (ensuring correct data format for API inputs)
- Response serialization (standardizing API outputs)
- Data transformation between API and database layers

- **database.py**: Sets up SQLAlchemy engine, session management, and database connection.

#### Routers (API Endpoints)

- **routers/blog.py**: CRUD operations for blog posts:
- Create: `POST /blog/`
- Read: `GET /blog/` and `GET /blog/{id}`
- Update: `PUT /blog/{id}`
- Delete: `DELETE /blog/{id}`

- **routers/user.py**: User management endpoints:
- Read: `GET /user/` and `GET /user/{id}`
- Delete: `DELETE /user/{id}`

- **routers/login.py**: Authentication endpoints:
- Register: `POST /auth/register`
- Login: `POST /auth/login`
- Logout: `POST /auth/logout`

#### Configuration and Documentation

- **.env.example**: Template showing required environment variables
- **.env**: Actual environment variables with sensitive configuration
- **README.md**: Project documentation (this file)
- **TABLEPLUS_GUIDE.md**: Instructions for database management

#### Testing and Development

- **test_tableplus.py**: Test script for API functionality:
- User registration and authentication
- Blog post creation and retrieval

## API Flow Diagram

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Client │◄────►│ FastAPI │◄────►│ SQLAlchemy β”‚β—„β”€β”€β”€β”€β–Ίβ”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (Browser/ β”‚ β”‚ (Routers) β”‚ β”‚ ORM β”‚ β”‚ SQLite β”‚
β”‚ App/etc.) β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Databaseβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β–² β–²
β”‚ β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
└───────────── JWT β”‚
β”‚ Security β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## API Endpoints

### Authentication
- `POST /auth/register` - Register a new user
- `POST /auth/login` - Login and get JWT token
- `POST /auth/logout` - Logout (client-side token removal)

### Users
- `GET /user/{id}` - Get a specific user
- `GET /user/` - Get all users
- `DELETE /user/{id}` - Delete a user

### Blogs
- `POST /blog/` - Create a new blog
- `GET /blog/{id}` - Get a specific blog
- `GET /blog/` - Get all blogs
- `PUT /blog/{id}` - Update a blog (only by owner)
- `DELETE /blog/{id}` - Delete a blog (only by owner)

## Security Features

- JWT token-based authentication
- Password hashing with bcrypt
- Protected routes
- User-specific blog operations
- Input validation with Pydantic

## Development

To contribute to the project:

1. Fork the repository
2. Create a feature branch
3. Commit your changes
4. Push to the branch
5. Create a Pull Request

## License

This project is licensed under the Creative Commons Zero v1.0 Universal License - see the [LICENSE](LICENSE) file for details.

## Author

Created by Yasharth Bajpai

## Acknowledgments

- FastAPI documentation
- SQLAlchemy documentation
- JWT.io for token information