Skip to content

ThreadsofDaemonS/FastAPIBlog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPIBlog

A modern blog API built with FastAPI featuring AI-powered moderation, automatic replies, and comprehensive analytics.

πŸš€ Features

  • User Authentication: JWT-based authentication system with registration and login
  • Blog Posts Management: Create, read, update, and delete blog posts
  • Comment System: Interactive commenting with AI moderation
  • AI Moderation: Automatic toxicity detection using Google's Gemini AI
  • Auto-Reply: Intelligent automatic responses to comments using AI
  • Analytics: Comprehensive analytics for posts and comments with daily breakdowns
  • Async Architecture: Built with async/await for high performance
  • Database Migrations: Alembic integration for database schema management

πŸ› οΈ Technology Stack

  • Backend: FastAPI (Python 3.9+)
  • Database: PostgreSQL with SQLAlchemy (async)
  • AI: Google Gemini AI for moderation and auto-replies
  • Authentication: JWT tokens with bcrypt password hashing
  • Migrations: Alembic
  • Testing: pytest with async support
  • Containerization: Docker & Docker Compose

πŸ“‹ Prerequisites

  • Python 3.9+
  • PostgreSQL
  • Docker & Docker Compose (optional)
  • Google AI API key for moderation features

βš™οΈ Installation and Setup

1. Clone the Repository

git clone https://github.com/ThreadsofDaemonS/FastAPIBlog.git
cd FastAPIBlog

2. Environment Configuration

Copy the sample environment file and configure your settings:

cp .env.sample .env

Edit .env with your configuration:

# PostgreSQL
POSTGRES_DB=fastapi_blog
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
POSTGRES_HOST=localhost
POSTGRES_PORT=5432

DATABASE_URL=postgresql+asyncpg://postgres:your_password@localhost:5432/fastapi_blog

# JWT Security
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

# Google AI
GOOGLE_API_KEY=your-google-ai-api-key

3. Using Docker (Recommended)

# Start all services
docker-compose up -d --build

# View logs
docker-compose logs -f web

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

4. Manual Installation

# Install dependencies
pip install -r requirements.txt

# Start PostgreSQL database
# Make sure PostgreSQL is running and database is created

# Run migrations
alembic upgrade head

# Start the application
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload

πŸ“ Project Structure

FastAPIBlog/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ core/                 # Core functionality
β”‚   β”‚   β”œβ”€β”€ db.py            # Database configuration
β”‚   β”‚   └── security.py      # Authentication & security
β”‚   β”œβ”€β”€ models/              # SQLAlchemy models
β”‚   β”‚   β”œβ”€β”€ user.py          # User model
β”‚   β”‚   β”œβ”€β”€ post.py          # Post model
β”‚   β”‚   └── comment.py       # Comment model
β”‚   β”œβ”€β”€ routers/             # API route handlers
β”‚   β”‚   β”œβ”€β”€ auth.py          # Authentication endpoints
β”‚   β”‚   β”œβ”€β”€ post.py          # Post management endpoints
β”‚   β”‚   β”œβ”€β”€ comment.py       # Comment endpoints
β”‚   β”‚   └── analytics.py     # Analytics endpoints
β”‚   β”œβ”€β”€ schemas/             # Pydantic schemas
β”‚   β”‚   β”œβ”€β”€ user.py          # User schemas
β”‚   β”‚   β”œβ”€β”€ post.py          # Post schemas
β”‚   β”‚   └── comment.py       # Comment schemas
β”‚   β”œβ”€β”€ services/            # Business logic
β”‚   β”‚   β”œβ”€β”€ auth.py          # Authentication service
β”‚   β”‚   β”œβ”€β”€ user.py          # User service
β”‚   β”‚   β”œβ”€β”€ post.py          # Post service
β”‚   β”‚   β”œβ”€β”€ comment.py       # Comment service
β”‚   β”‚   β”œβ”€β”€ ai_moderation.py # AI moderation service
β”‚   β”‚   └── auto_reply.py    # Auto-reply service
β”‚   └── main.py              # FastAPI application entry point
β”œβ”€β”€ alembic/                 # Database migrations
β”œβ”€β”€ tests/                   # Test suite
β”œβ”€β”€ docker-compose.yml       # Docker services configuration
β”œβ”€β”€ Dockerfile              # Docker container configuration
β”œβ”€β”€ requirements.txt         # Python dependencies
└── README.md               # This file

πŸ”§ API Documentation

Authentication Endpoints (/auth)

Register User

POST /auth/register
Content-Type: application/json

{
    "email": "user@example.com",
    "password": "securepassword"
}

Response:

{
    "id": 1,
    "email": "user@example.com"
}

Login

POST /auth/login
Content-Type: application/x-www-form-urlencoded

username=user@example.com&password=securepassword

better login through /docs/ using button Authorize

Response:

{
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "token_type": "bearer"
}

Get Current User

GET /auth/me
Authorization: Bearer <token>

Get All Users

GET /auth/all-users

Post Endpoints (/posts)

Create Post

POST /posts/
Authorization: Bearer <token>
Content-Type: application/json

{
    "content": "This is my first blog post!",
    "auto_reply_enabled": true,
    "reply_delay_sec": 5
}

Response:

{
    "id": 1,
    "content": "This is my first blog post!",
    "is_blocked": false,
    "auto_reply_enabled": true,
    "reply_delay_sec": 5
}

Get My Posts

GET /posts/
Authorization: Bearer <token>

Get Specific Post

GET /posts/{post_id}
Authorization: Bearer <token>

Comment Endpoints (/comments)

Create Comment

POST /comments/
Authorization: Bearer <token>
Content-Type: application/json

{
    "post_id": 1,
    "content": "Great post! Thanks for sharing."
}

Response:

{
    "id": 1,
    "post_id": 1,
    "user_id": 2,
    "content": "Great post! Thanks for sharing.",
    "is_blocked": false,
    "created_at": "2024-01-15T10:30:00.000Z"
}

Get Post Comments

GET /comments/post/{post_id}

Analytics Endpoints (/api)

Comments Daily Breakdown

GET /api/comments-daily-breakdown?date_from=2024-01-01&date_to=2024-01-31

Response:

[
    {
        "date": "2024-01-15",
        "total_comments": 25,
        "blocked_comments": 2
    },
    {
        "date": "2024-01-16",
        "total_comments": 18,
        "blocked_comments": 1
    }
]

πŸ€– AI Features

Content Moderation

The application uses Google's Gemini AI to automatically detect toxic, offensive, or inappropriate content in posts and comments. Blocked content is flagged and can be reviewed by administrators.

Features:

  • Automatic toxicity detection
  • Manual blacklist for immediate blocking
  • Configurable moderation thresholds

Auto-Reply System

When enabled on a post, the system automatically generates relevant replies to comments using AI:

Features:

  • Contextual replies based on post content and comment
  • Configurable delay before reply
  • Automatic activation per post
  • Prevents self-replies (post author won't get auto-replies on their own posts)

πŸ§ͺ Testing

Run Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=app

# Run specific test file
pytest tests/test_analytics.py

# Run with verbose output
pytest -v

better through docker-compose

docker-compose exec web pytest -v tests/

Test Coverage

The project includes comprehensive test coverage for:

  • Authentication flows
  • CRUD operations
  • Analytics endpoints
  • Error handling

🐳 Docker Usage

Development Environment

# Start services in development mode
docker-compose up -d --build

# View logs
docker-compose logs -f web

# Stop services
docker-compose down

# Use -v if you want clear database
docker-compose down -v

# Rebuild after changes
docker-compose up -d --build

Production Deployment

The application is containerized and ready for production deployment. Update environment variables appropriately for your production environment.

πŸ”’ Security Features

  • JWT Authentication: Secure token-based authentication
  • Password Hashing: bcrypt for secure password storage
  • SQL Injection Protection: SQLAlchemy ORM prevents SQL injection
  • Input Validation: Pydantic schemas validate all inputs
  • Content Moderation: AI-powered toxic content detection

πŸ“Š Database Schema

Users Table

  • id: Primary key
  • email: Unique user email
  • hashed_password: Bcrypt hashed password

Posts Table

  • id: Primary key
  • user_id: Foreign key to users
  • content: Post content
  • is_blocked: Moderation flag
  • auto_reply_enabled: Auto-reply setting
  • reply_delay_sec: Delay before auto-reply

Comments Table

  • id: Primary key
  • post_id: Foreign key to posts
  • user_id: Foreign key to users
  • content: Comment content
  • is_blocked: Moderation flag
  • created_at: Timestamp

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”§ Configuration Options

Environment Variables

Variable Description Default
POSTGRES_DB PostgreSQL database name -
POSTGRES_USER PostgreSQL username -
POSTGRES_PASSWORD PostgreSQL password -
POSTGRES_HOST PostgreSQL host localhost
POSTGRES_PORT PostgreSQL port 5432
SECRET_KEY JWT secret key -
ALGORITHM JWT algorithm HS256
ACCESS_TOKEN_EXPIRE_MINUTES Token expiration time 30
GOOGLE_API_KEY Google AI API key -

🚨 Troubleshooting

Common Issues

  1. Database Connection Error

    • Verify PostgreSQL is running
    • Check DATABASE_URL configuration
    • Ensure database exists
  2. Migration Issues

    # Reset migrations (development only)
    alembic downgrade base
    alembic upgrade head
  3. AI Moderation Not Working

    • Verify GOOGLE_API_KEY is set correctly
    • Check Google AI API quota and billing
  4. Authentication Errors

    • Verify SECRET_KEY is set
    • Check token expiration settings

πŸ“ž Support

For support and questions:

  • Create an issue on GitHub
  • Check existing documentation
  • Review the troubleshooting section

Note: This application is designed for educational and development purposes. Ensure proper security measures are implemented before deploying to production.

About

A simple API for managing posts and comments with AI moderation and auto-reply.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •