Skip to content

Logic-ui/Blog_Website

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Blog Website - Complete Setup Guide

πŸ“‹ Project Overview

This is a full-stack blog website with:

  • Backend: FastAPI with SQLAlchemy ORM
  • Frontend: React with React Router
  • Database: SQLite (easily switchable to PostgreSQL)
  • Authentication: JWT Token-based
  • Roles: Admin, Author, Reader

🎯 Features

πŸ“– For Readers

  • Browse all published blogs
  • Read blog posts with author information
  • User authentication

✍️ For Authors

  • Create and upload blogs
  • Edit their own blogs
  • Publish/unpublish blogs
  • View all their published blogs

πŸ‘¨β€πŸ’Ό For Admins

  • View all blogs and users
  • Delete blogs
  • Create and manage notices/announcements
  • Delete user accounts if needed

πŸ› οΈ Technologies Used

Backend

  • FastAPI - Modern Python web framework
  • SQLAlchemy - ORM for database operations
  • Pydantic - Data validation
  • SQLite - Lightweight database
  • JWT - Authentication tokens
  • Passlib & Bcrypt - Password hashing

Frontend

  • React 18 - UI library
  • React Router DOM - Client-side routing
  • Axios - HTTP client
  • CSS - Styling

πŸ“ Directory Structure

Blog-website/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   β”œβ”€β”€ user.py        # User model
β”‚   β”‚   β”‚   β”œβ”€β”€ blog.py        # Blog model
β”‚   β”‚   β”‚   └── notice.py      # Notice model
β”‚   β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”‚   β”œβ”€β”€ users.py       # User API endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ blogs.py       # Blog API endpoints
β”‚   β”‚   β”‚   └── notices.py     # Notice API endpoints
β”‚   β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   β”‚   β”œβ”€β”€ user.py        # User validation schemas
β”‚   β”‚   β”‚   β”œβ”€β”€ blog.py        # Blog validation schemas
β”‚   β”‚   β”‚   └── notice.py      # Notice validation schemas
β”‚   β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”‚   └── database.py    # DB configuration
β”‚   β”‚   └── auth.py            # Authentication utilities
β”‚   β”œβ”€β”€ main.py                # FastAPI app entry
β”‚   β”œβ”€β”€ requirements.txt       # Python dependencies
β”‚   └── .env                   # Environment variables
β”‚
└── frontend/
    β”œβ”€β”€ public/
    β”‚   └── index.html
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ components/
    β”‚   β”‚   β”œβ”€β”€ Header.js
    β”‚   β”‚   └── BlogCard.js
    β”‚   β”œβ”€β”€ pages/
    β”‚   β”‚   β”œβ”€β”€ Home.js
    β”‚   β”‚   β”œβ”€β”€ Login.js
    β”‚   β”‚   β”œβ”€β”€ Register.js
    β”‚   β”‚   β”œβ”€β”€ BlogDetail.js
    β”‚   β”‚   β”œβ”€β”€ CreateBlog.js
    β”‚   β”‚   └── Admin.js
    β”‚   β”œβ”€β”€ services/
    β”‚   β”‚   └── api.js         # API client
    β”‚   β”œβ”€β”€ context/
    β”‚   β”‚   └── AuthContext.js # Auth state management
    β”‚   β”œβ”€β”€ styles/
    β”‚   β”‚   β”œβ”€β”€ index.css
    β”‚   β”‚   β”œβ”€β”€ Header.css
    β”‚   β”‚   β”œβ”€β”€ Auth.css
    β”‚   β”‚   β”œβ”€β”€ Home.css
    β”‚   β”‚   β”œβ”€β”€ Blog.css
    β”‚   β”‚   β”œβ”€β”€ CreateBlog.css
    β”‚   β”‚   └── Admin.css
    β”‚   β”œβ”€β”€ App.js
    β”‚   └── index.js
    └── package.json

πŸš€ Installation & Setup

Prerequisites

  • Python 3.8+
  • Node.js 14+
  • npm or yarn

1️⃣ Backend Setup

Step 1: Navigate to backend directory

cd Blog-website/backend

Step 2: Create virtual environment

python -m venv venv

Step 3: Activate virtual environment

# On Windows
venv\Scripts\activate

# On macOS/Linux
source venv/bin/activate

Step 4: Install dependencies

pip install -r requirements.txt

Step 5: Run the backend server

python main.py

The backend will start at http://localhost:8000

API Documentation: Visit http://localhost:8000/docs (Swagger UI)

2️⃣ Frontend Setup

Step 1: Navigate to frontend directory

cd Blog-website/frontend

Step 2: Install dependencies

npm install

Step 3: Start development server

npm start

The frontend will open at http://localhost:3000


πŸ”‘ Authentication

Registration

  1. Click "Register" on the home page
  2. Enter username, email, full name, and password
  3. Choose role: Reader or Author
  4. Click Register

Login

  1. Click "Login" on the home page
  2. Enter username and password
  3. Token is saved to localStorage

πŸ’‘ How to Use

πŸ“– As a Reader

  1. Browse Blogs: Visit homepage to see all published blogs
  2. Read Blog: Click "Read More" on any blog card to view full content
  3. View Author: See author info on blog detail page

✍️ As an Author

  1. Register with "Author" role
  2. Click "Write Blog" in navigation
  3. Fill in title, description, and content
  4. Click "Publish Blog"
  5. Your blog appears on homepage

πŸ‘¨β€πŸ’Ό As an Admin

  1. Register first as normal user (or backend admin setup)
  2. Click "Admin" in navigation
  3. Manage Blogs: View and delete any blog
  4. Create Notices: Post announcements for users
  5. View Users: See all registered users

πŸ“‘ API Endpoints

User Endpoints

  • POST /api/users/register - Register new user
  • POST /api/users/login - User login
  • GET /api/users/me - Get current user (requires auth)
  • GET /api/users/ - List all users
  • GET /api/users/{user_id} - Get user details
  • PUT /api/users/{user_id} - Update user
  • DELETE /api/users/{user_id} - Delete user (admin only)

Blog Endpoints

  • POST /api/blogs/ - Create blog (author/admin only)
  • GET /api/blogs/ - List published blogs
  • GET /api/blogs/{blog_id} - Get blog details
  • PUT /api/blogs/{blog_id} - Update blog (author/admin)
  • DELETE /api/blogs/{blog_id} - Delete blog (author/admin)
  • GET /api/blogs/author/{author_id} - Get author's blogs

Notice Endpoints

  • POST /api/notices/ - Create notice (admin only)
  • GET /api/notices/ - List all notices
  • GET /api/notices/{notice_id} - Get notice details
  • PUT /api/notices/{notice_id} - Update notice (admin only)
  • DELETE /api/notices/{notice_id} - Delete notice (admin only)

πŸ” Admin Setup (Backend)

To manually create an admin user, run Python shell in backend:

from app.database.database import SessionLocal
from app.models.user import User, UserRole
from app.auth import get_password_hash

db = SessionLocal()

admin = User(
    username="admin",
    email="admin@example.com",
    full_name="Admin User",
    hashed_password=get_password_hash("admin123"),
    role=UserRole.ADMIN
)

db.add(admin)
db.commit()
print("Admin user created!")

πŸ—„οΈ Database

Models

User

- id: Integer (PK)
- username: String
- email: String
- full_name: String
- hashed_password: String
- role: Enum (admin, author, reader)
- is_active: Boolean
- created_at: DateTime
- updated_at: DateTime

Blog

- id: Integer (PK)
- title: String
- content: Text
- description: String
- author_id: Foreign Key
- is_published: Boolean
- created_at: DateTime
- updated_at: DateTime

Notice

- id: Integer (PK)
- title: String
- content: Text
- sender_id: Foreign Key
- created_at: DateTime
- updated_at: DateTime

πŸ”„ Workflow Examples

Example 1: Create and Publish a Blog

  1. Register as Author
  2. Go to "Write Blog"
  3. Fill in details and publish
  4. Blog automatically appears on homepage

Example 2: Admin Deletes a Blog

  1. Login as Admin
  2. Go to Admin Panel
  3. View all blogs
  4. Click Delete on any blog

Example 3: Browse and Read Blogs

  1. Visit homepage (no login needed)
  2. See all published blogs
  3. Click any blog to read full content
  4. See author details

βš™οΈ Configuration

Backend (.env)

DATABASE_URL=sqlite:///./blog.db
SECRET_KEY=your-secret-key-change-in-production

For Production Database (PostgreSQL):

DATABASE_URL=postgresql://user:password@localhost/blog_db

Frontend (API URL)

Edit frontend/src/services/api.js:

const API_BASE_URL = 'http://localhost:8000/api';

πŸ› Troubleshooting

Backend Issues

Port 8000 already in use

# Change port in main.py or use:
uvicorn app.main:app --port 8001

Database errors

# Delete SQLite file and restart
rm blog.db
python main.py

Frontend Issues

Port 3000 already in use

# Use different port:
PORT=3001 npm start

CORS errors

  • Backend CORS is enabled for all origins
  • Check that frontend URL matches in settings

πŸ“¦ Production Deployment

Backend (Gunicorn)

pip install gunicorn
gunicorn -w 4 -k uvicorn.workers.UvicornWorker app.main:app

Frontend (Build)

npm run build

🀝 Contributing

Feel free to extend this project with:

  • Comments on blogs
  • Search functionality
  • Categories/Tags
  • User profiles
  • Email notifications
  • Image uploads

πŸ“ License

This project is open source and available under MIT License.


πŸ“ž Support

For issues or questions, please check the code comments or reach out!

Happy blogging! πŸ“βœ¨

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors