Skip to content

Latest commit

Β 

History

134 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ CMS Blog Backend

A production-ready FastAPI backend for an AI-powered Blog Generator CMS with multi-step content creation workflow.

✨ Features

πŸ” Authentication & Authorization

  • JWT-based authentication with secure token management
  • Role-based access control (Admin vs User)
  • Argon2 password hashing for maximum security

πŸ€– AI-Powered Content Generation

  • 5-step blog creation workflow with exactly 5 options at each step:
    • Topic Ideas β†’ Titles β†’ Intros β†’ Outlines β†’ Image Prompts
  • Gemini AI integration for intelligent content generation
  • AI-generated cover images with custom prompts
  • Markdown to HTML conversion with live preview

πŸ“ Blog Management

  • Multi-stage publishing workflow: saved β†’ pending β†’ published/rejected
  • Admin moderation panel for content approval
  • Image upload support (generated or device upload)
  • Dashboard statistics for content tracking

🎨 Developer Experience

  • Clean REST API with automatic documentation (Swagger/ReDoc)
  • Async MongoDB operations using Motor
  • Modular architecture with services and routers
  • Type-safe with Pydantic schemas

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         FastAPI App                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚   Auth   β”‚  β”‚    AI    β”‚  β”‚  Blogs   β”‚  β”‚  Admin   β”‚   β”‚
β”‚  β”‚  Router  β”‚  β”‚  Router  β”‚  β”‚  Router  β”‚  β”‚  Router  β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚              Services Layer                          β”‚   β”‚
β”‚  β”‚  β€’ Gemini Service  β€’ Image Service  β€’ Markdown      β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚                  MongoDB (Motor)                     β”‚   β”‚
β”‚  β”‚            β€’ users_col  β€’ blogs_col                  β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Tech Stack

Component Technology
Framework FastAPI
Database MongoDB (Motor async driver)
Authentication JWT (python-jose)
Password Security Argon2 (argon2-cffi)
AI Engine Google Gemini AI
Markdown Parser markdown
Server Uvicorn (ASGI)

πŸ“‹ Prerequisites

  • Python 3.10+ (Python 3.11 recommended)
  • MongoDB (local installation or MongoDB Atlas)
  • Google Gemini API Key (Get one here)

πŸš€ Quick Start

1️⃣ Clone the Repository

git clone <repository-url>
cd backend

2️⃣ Create Virtual Environment

Windows (PowerShell):

python -m venv venv
.\venv\Scripts\Activate.ps1

Linux/macOS:

python -m venv venv
source venv/bin/activate

3️⃣ Install Dependencies

python -m pip install -U pip
pip install -r requirements.txt

4️⃣ Configure Environment

Create a .env file in the backend/ directory:

# Application
APP_NAME=CMS Blog API
ENV=dev

# Database
MONGODB_URI=mongodb://localhost:27017
MONGODB_DB=cms_blog

# Security
JWT_SECRET=your-super-secret-key-change-this
JWT_EXPIRES_MINUTES=10080

# Admin Account
ADMIN_EMAIL=admin@company.com

# CORS & Public URL
CORS_ORIGINS=http://localhost:5173,http://127.0.0.1:5173
PUBLIC_BASE_URL=http://127.0.0.1:8000

# Gemini AI
GEMINI_API_KEY=your_gemini_api_key_here
GEMINI_TEXT_MODEL=gemini-2.0-flash-exp
GEMINI_IMAGE_MODEL=imagen-3.0-generate-001

πŸ’‘ Tip: Make sure to get your Gemini API key from Google AI Studio.

5️⃣ Run the Server

uvicorn main:app --reload --port 8000

The API will be available at:


πŸ“ Project Structure

backend/
β”œβ”€β”€ πŸ“„ main.py                    # Application entrypoint
β”œβ”€β”€ πŸ“„ requirements.txt           # Python dependencies
β”œβ”€β”€ πŸ“„ .env                       # Environment config (DO NOT COMMIT)
β”œβ”€β”€ πŸ“„ .gitignore                 # Git ignore rules
β”œβ”€β”€ πŸ“‚ uploads/                   # Generated/uploaded images
β”œβ”€β”€ πŸ“‚ venv/                      # Virtual environment
β”‚
β”œβ”€β”€ πŸ“‚ core/
β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   β”œβ”€β”€ πŸ“„ config.py              # Settings & environment config
β”‚   β”œβ”€β”€ πŸ“„ deps.py                # Auth dependencies
β”‚   └── πŸ“„ verify.py              # Token verification utilities
β”‚
β”œβ”€β”€ πŸ“‚ app/
β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   β”‚
β”‚   β”œβ”€β”€ πŸ“‚ models/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ db.py              # MongoDB connection & collections
β”‚   β”‚   └── πŸ“„ schemas.py         # Pydantic models
β”‚   β”‚
β”‚   β”œβ”€β”€ πŸ“‚ routers/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ auth.py            # Signup/Login endpoints
β”‚   β”‚   β”œβ”€β”€ πŸ“„ ai.py              # AI generation endpoints
β”‚   β”‚   β”œβ”€β”€ πŸ“„ blogs.py           # Blog CRUD & workflow
β”‚   β”‚   └── πŸ“„ admin.py           # Admin moderation panel
β”‚   β”‚
β”‚   └── πŸ“‚ services/
β”‚       β”œβ”€β”€ πŸ“„ __init__.py
β”‚       β”œβ”€β”€ πŸ“„ gemini_service.py  # Gemini AI integration
β”‚       β”œβ”€β”€ πŸ“„ image_service.py   # Image generation
β”‚       └── πŸ“„ markdown_service.py # Markdown β†’ HTML

πŸ”Œ API Endpoints

πŸ” Authentication (/auth)

Method Endpoint Description
POST /auth/auth/signup Register new user
POST /auth/auth/login Login and get JWT token

πŸ€– AI Generation (/ai)

Method Endpoint Description
POST /ai/ideas Generate topic ideas (default 5, accepts count)
POST /ai/titles Generate 5 titles for selected topic
POST /ai/intros Generate 5 intro paragraphs
POST /ai/outlines Generate 5 blog outlines
POST /ai/image-prompts Generate 5 image prompts
POST /ai/image-generate Generate single cover image
POST /ai/blog-generate Generate final blog (Markdown + HTML)

πŸ“ Blog Management (/blogs)

Method Endpoint Description
POST /blog save final blog
GET /blog List current user's blogs
GET /blogs/stats Get blog statistics for dashboard
POST /blogs/uploads/images Upload custom cover image
GET /blogs/{blog_id} Get single blog by ID
POST /blogs/{blog_id}/publish-request Request admin approval for publishing

πŸ‘‘ Admin Panel (/admin)

Method Endpoint Description
GET /admin/blogs List all blogs with optional status filter
POST /admin/blogs/{blog_id}/approve Approve blog for publishing
POST /admin/blogs/{blog_id}/reject Reject blog with feedback message

🎯 Workflow

graph LR
    A[Topic Ideas] --> B[Titles]
    B --> C[Intros]
    C --> D[Outlines]
    D --> E[Image Prompts]
    E --> F[Generate Image]
    F --> G[Generate Blog]
    G --> H[Save Draft]
    H --> I[Request Publish]
    I --> J{Admin Review}
    J -->|Approve| K[Published]
    J -->|Reject| L[Rejected]
Loading

Each step provides exactly 5 options to choose from, ensuring a guided and structured content creation process.


πŸ”’ Security Features

  • JWT Authentication with configurable expiration
  • Argon2 Password Hashing (industry standard)
  • Role-Based Access Control (RBAC)
  • CORS Protection with whitelist
  • Environment-based Configuration (no hardcoded secrets)

πŸ“Š Database Schema

Users Collection

{
  "_id": "ObjectId",
  "email": "user@example.com",
  "password_hash": "argon2$...",
  "role": "user|admin",
  "created_at": "ISO-8601"
}

Blogs Collection

{
  "_id": "ObjectId",
  "owner": "user_id",
  "status": "saved|pending|published|rejected",
  "title": "Blog Title",
  "intro": "Introduction text",
  "outline": ["Section 1", "Section 2"],
  "image_url": "/uploads/image.png",
  "markdown": "# Blog content...",
  "html": "<h1>Blog content...</h1>",
  "created_at": "ISO-8601",
  "admin_feedback": "Optional rejection reason"
}

πŸ§ͺ Development

Run with Auto-reload

uvicorn main:app --reload --port 8000

Access Interactive Docs

MongoDB Indexes

Automatically created on startup:

  • Users: Unique index on email
  • Blogs: Indexes on status, owner, created_at

🚒 Deployment

Environment Variables

Ensure all required environment variables are set in production:

  • Set ENV=production
  • Use strong JWT_SECRET
  • Configure MONGODB_URI for MongoDB Atlas
  • Set PUBLIC_BASE_URL to your domain

Recommended Stack

  • Backend: FastAPI on Docker/Railway/Render
  • Database: MongoDB Atlas
  • Static Files: CDN for uploaded images
  • Reverse Proxy: Nginx or Caddy

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages