
- Create project structure with app organization - Set up FastAPI application with CORS and health endpoint - Implement database models with SQLAlchemy (User, Post, Comment) - Set up Alembic for database migrations - Implement authentication with JWT tokens - Create CRUD operations for all models - Implement REST API endpoints for users, posts, and comments - Add comprehensive documentation in README.md
Blogging API
A RESTful API for a blogging platform built with FastAPI and SQLite.
Features
- User management with authentication
- Blog post creation, retrieval, updating, and deletion
- Comment functionality on blog posts
- Role-based permissions (regular users and superusers)
- RESTful API with proper HTTP methods and status codes
- JWT-based authentication
- SQLite database with SQLAlchemy ORM
- Alembic for database migrations
Prerequisites
- Python 3.8+
- Virtual environment (recommended)
Environment Variables
The application uses the following environment variables:
SECRET_KEY
: Secret key for JWT token encryption (default provided for development)ACCESS_TOKEN_EXPIRE_MINUTES
: JWT token expiration time in minutes (default: 60 * 24 * 8 = 8 days)
Installation
- Clone the repository:
git clone <repository-url>
cd bloggingapi
- Install the dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Run the application:
uvicorn main:app --reload
The API will be available at http://localhost:8000.
API Documentation
Once the application is running, you can access the API documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
API Endpoints
Authentication
POST /api/v1/login/access-token
- Get JWT access token
Users
GET /api/v1/users/
- Get all users (superuser only)POST /api/v1/users/
- Create a new userGET /api/v1/users/me
- Get current userPUT /api/v1/users/me
- Update current userGET /api/v1/users/{user_id}
- Get user by IDDELETE /api/v1/users/{user_id}
- Delete user (superuser only)
Posts
GET /api/v1/posts/
- Get all published postsPOST /api/v1/posts/
- Create a new postGET /api/v1/posts/{post_id}
- Get post by IDPUT /api/v1/posts/{post_id}
- Update postDELETE /api/v1/posts/{post_id}
- Delete postGET /api/v1/posts/user/{user_id}
- Get posts by user
Comments
GET /api/v1/comments/
- Get all comments (superuser only)POST /api/v1/comments/
- Create a new commentGET /api/v1/comments/{comment_id}
- Get comment by IDPUT /api/v1/comments/{comment_id}
- Update commentDELETE /api/v1/comments/{comment_id}
- Delete commentGET /api/v1/comments/post/{post_id}
- Get comments by postGET /api/v1/comments/user/{user_id}
- Get comments by user
Health Check
GET /health
- API health check
Project Structure
.
├── alembic.ini # Alembic configuration
├── app # Application package
│ ├── api # API endpoints
│ │ └── v1 # API version 1
│ │ ├── api.py # API router
│ │ └── endpoints # API endpoint modules
│ │ ├── comments.py # Comment endpoints
│ │ ├── login.py # Authentication endpoints
│ │ ├── posts.py # Post endpoints
│ │ └── users.py # User endpoints
│ ├── auth # Authentication package
│ │ ├── deps.py # Auth dependencies
│ │ └── security.py # Security utilities
│ ├── core # Core package
│ │ └── config.py # Configuration settings
│ ├── crud # CRUD operations
│ │ ├── base.py # Base CRUD class
│ │ ├── comment.py # Comment CRUD
│ │ ├── post.py # Post CRUD
│ │ └── user.py # User CRUD
│ ├── db # Database package
│ │ ├── base.py # Import all models
│ │ ├── base_class.py # Base model class
│ │ └── session.py # Database session
│ ├── models # SQLAlchemy models
│ │ ├── comment.py # Comment model
│ │ ├── post.py # Post model
│ │ └── user.py # User model
│ └── schemas # Pydantic schemas
│ ├── comment.py # Comment schemas
│ ├── post.py # Post schemas
│ └── user.py # User schemas
├── main.py # FastAPI application
├── migrations # Alembic migrations
│ ├── env.py # Alembic environment
│ ├── script.py.mako # Migration script template
│ └── versions # Migration versions
│ └── 001_initial_tables.py # Initial migration
└── requirements.txt # Project dependencies
Development
The project uses Alembic for database migrations. To create a new migration after model changes:
alembic revision --autogenerate -m "Description of changes"
To apply migrations:
alembic upgrade head
Description
Languages
Python
98.9%
Mako
1.1%