Skip to content

git-akki/python-blog-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Blog Platform

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.

Features

  • 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

Tech Stack

  • Backend: FastAPI
  • Database: SQLAlchemy ORM with SQLite
  • Authentication: JWT (PyJWT)
  • Password Hashing: Passlib
  • Migrations: Alembic

Project Structure

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

Setup Instructions

Prerequisites

  • Python 3.8+
  • pip

Installation

  1. Clone the repository
git clone <repository-url>
cd Blog/Backend
  1. Create a virtual environment
python -m venv venv
source venv/Scripts/activate  # On Windows
# or
source venv/bin/activate      # On macOS/Linux
  1. Install dependencies
pip install -r requirements.txt
  1. Set up environment variables Create a .env file in the Backend directory:
DATABASE_URL=sqlite:///./blog.db
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
  1. Run database migrations
alembic upgrade head
  1. Create an admin user (optional)
python -m backend_app.scripts.create_admin
  1. Run the server
uvicorn backend_app.main:app --reload

The API will be available at http://localhost:8000 API documentation at http://localhost:8000/docs

API Endpoints

Authentication

  • POST /api/v1/auth/register - Register a new user
  • POST /api/v1/auth/login - Login and get access token

Users

  • GET /api/v1/users/me - Get current user profile
  • GET /api/v1/users/{user_id} - Get user by ID
  • PUT /api/v1/users/me - Update current user

Posts

  • GET /api/v1/posts/ - List posts
  • POST /api/v1/posts/ - Create a post (Editor/Admin)
  • GET /api/v1/posts/{post_id} - Get a specific post
  • PUT /api/v1/posts/{post_id} - Update a post
  • DELETE /api/v1/posts/{post_id} - Delete a post
  • POST /api/v1/posts/{post_id}/publish - Publish a post

Comments

  • GET /api/v1/posts/{post_id}/comments - List comments on a post
  • POST /api/v1/posts/{post_id}/comments - Create a comment
  • PUT /api/v1/posts/comments/{comment_id} - Update a comment
  • DELETE /api/v1/posts/comments/{comment_id} - Delete a comment

User Roles

  • Reader: Can view published posts and comment
  • Editor: Can create, edit, and publish posts
  • Admin: Full access to all operations

License

This project is licensed under the MIT License.

Author

Created as part of a Python learning journey.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors