A modern blog platform built with FastAPI and SQLAlchemy, featuring user authentication, role-based access control, and a complete REST API for managing posts and comments.
- User Management: User registration, authentication, and role-based authorization (Reader, Editor, Admin)
- Posts: Create, read, update, and delete blog posts with publishing status
- Comments: Comment on posts with moderation capabilities
- Authentication: JWT-based authentication with secure password hashing
- Database Migrations: Alembic for schema management
- RESTful API: Complete API for all operations
- Backend: FastAPI
- Database: SQLAlchemy ORM with SQLite
- Authentication: JWT (PyJWT)
- Password Hashing: Passlib
- Migrations: Alembic
Backend/
├── backend_app/
│ ├── api/
│ │ └── v1/
│ │ └── endpoints/
│ │ ├── auth.py # Authentication endpoints
│ │ ├── users.py # User management endpoints
│ │ └── posts.py # Post and comment endpoints
│ ├── core/
│ │ ├── config.py # Configuration settings
│ │ ├── deps.py # Dependency injection
│ │ ├── jwt.py # JWT utilities
│ │ ├── security.py # Security utilities
│ │ └── permissions.py # Permission checks
│ ├── db/
│ │ ├── base.py # SQLAlchemy base
│ │ ├── session.py # Database session
│ │ └── deps.py # Database dependencies
│ ├── models/
│ │ ├── user.py # User model
│ │ ├── post.py # Post model
│ │ └── comment.py # Comment model
│ ├── schemas/
│ │ ├── user.py # User schemas
│ │ ├── post.py # Post and comment schemas
│ │ └── auth.py # Auth schemas
│ ├── services/
│ │ ├── user_service.py # User business logic
│ │ └── post_service.py # Post business logic
│ └── main.py # FastAPI app
├── alembic/ # Database migrations
└── requirements.txt # Python dependencies
- Python 3.8+
- pip
- Clone the repository
git clone <repository-url>
cd Blog/Backend- Create a virtual environment
python -m venv venv
source venv/Scripts/activate # On Windows
# or
source venv/bin/activate # On macOS/Linux- Install dependencies
pip install -r requirements.txt- Set up environment variables
Create a
.envfile in theBackenddirectory:
DATABASE_URL=sqlite:///./blog.db
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
- Run database migrations
alembic upgrade head- Create an admin user (optional)
python -m backend_app.scripts.create_admin- Run the server
uvicorn backend_app.main:app --reloadThe API will be available at http://localhost:8000
API documentation at http://localhost:8000/docs
POST /api/v1/auth/register- Register a new userPOST /api/v1/auth/login- Login and get access token
GET /api/v1/users/me- Get current user profileGET /api/v1/users/{user_id}- Get user by IDPUT /api/v1/users/me- Update current user
GET /api/v1/posts/- List postsPOST /api/v1/posts/- Create a post (Editor/Admin)GET /api/v1/posts/{post_id}- Get a specific postPUT /api/v1/posts/{post_id}- Update a postDELETE /api/v1/posts/{post_id}- Delete a postPOST /api/v1/posts/{post_id}/publish- Publish a post
GET /api/v1/posts/{post_id}/comments- List comments on a postPOST /api/v1/posts/{post_id}/comments- Create a commentPUT /api/v1/posts/comments/{comment_id}- Update a commentDELETE /api/v1/posts/comments/{comment_id}- Delete a comment
- Reader: Can view published posts and comment
- Editor: Can create, edit, and publish posts
- Admin: Full access to all operations
This project is licensed under the MIT License.
Created as part of a Python learning journey.