
- Set up project structure and dependencies - Create database models for users, posts, comments, and tags - Set up Alembic for database migrations - Implement user authentication (register, login) - Create CRUD endpoints for blog posts, comments, and tags - Add health check endpoint - Set up proper error handling - Update README with project details and setup instructions
4.1 KiB
4.1 KiB
Blogging Platform API
A RESTful API for a blogging platform built with FastAPI and SQLite.
Features
- User authentication (register, login)
- Blog post management (CRUD operations)
- Comment system
- Tag/category support for blog posts
- Health check endpoint
- Proper error handling
- API documentation with Swagger and ReDoc
Tech Stack
- FastAPI: Modern, fast web framework for building APIs with Python
- SQLAlchemy: SQL toolkit and Object-Relational Mapping (ORM)
- SQLite: Lightweight, serverless database
- Alembic: Database migration tool
- Pydantic: Data validation and settings management
- JWT: JSON Web Tokens for authentication
- Uvicorn: ASGI server
Project Structure
.
├── alembic.ini
├── app
│ ├── api
│ │ ├── deps.py
│ │ ├── v1
│ │ │ ├── api.py
│ │ │ └── endpoints
│ │ │ ├── auth.py
│ │ │ ├── comments.py
│ │ │ ├── posts.py
│ │ │ ├── tags.py
│ │ │ └── users.py
│ ├── core
│ │ ├── config.py
│ │ └── security.py
│ ├── crud
│ │ ├── base.py
│ │ ├── crud_comment.py
│ │ ├── crud_post.py
│ │ ├── crud_tag.py
│ │ └── crud_user.py
│ ├── db
│ │ ├── base.py
│ │ ├── deps.py
│ │ └── session.py
│ ├── models
│ │ ├── comment.py
│ │ ├── post.py
│ │ ├── tag.py
│ │ └── user.py
│ ├── schemas
│ │ ├── comment.py
│ │ ├── post.py
│ │ ├── tag.py
│ │ ├── token.py
│ │ └── user.py
│ └── utils
│ └── errors.py
├── main.py
├── migrations
│ ├── env.py
│ ├── script.py.mako
│ └── versions
│ └── 0001_initial_migration.py
├── requirements.txt
└── storage
└── db
Setup Instructions
Prerequisites
- Python 3.8 or higher
- pip (Python package installer)
Installation
-
Clone the repository:
git clone <repository-url> cd blogging-platform-api
-
Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables (optional): Create a
.env
file in the root directory with the following variables:SECRET_KEY=your-secret-key ACCESS_TOKEN_EXPIRE_MINUTES=10080 # 7 days
-
Run database migrations:
alembic upgrade head
Running the Application
Start the FastAPI server:
uvicorn main:app --reload
The API will be available at http://localhost:8000
API Documentation
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
- OpenAPI JSON:
http://localhost:8000/openapi.json
API Endpoints
Authentication
POST /api/v1/auth/register
: Register a new userPOST /api/v1/auth/login
: Login and get access token
Users
GET /api/v1/users/me
: Get current userPUT /api/v1/users/me
: Update current user
Blog Posts
GET /api/v1/posts
: List postsPOST /api/v1/posts
: Create a new postGET /api/v1/posts/{id}
: Get a post by IDPUT /api/v1/posts/{id}
: Update a postDELETE /api/v1/posts/{id}
: Delete a post
Comments
GET /api/v1/comments
: List commentsPOST /api/v1/comments
: Create a new commentGET /api/v1/comments/{id}
: Get a comment by IDPUT /api/v1/comments/{id}
: Update a commentDELETE /api/v1/comments/{id}
: Delete a comment
Tags
GET /api/v1/tags
: List tagsPOST /api/v1/tags
: Create a new tagGET /api/v1/tags/{id}
: Get a tag by IDPUT /api/v1/tags/{id}
: Update a tagDELETE /api/v1/tags/{id}
: Delete a tag
Health Check
GET /health
: Check if the API is up and running