Skip to content

Bog is a straightforward FastAPI backend for a micro blogging site. It lets users register, post content, follow others, like posts, and view feeds, with secure JWT authentication.

Notifications You must be signed in to change notification settings

TheRootDaemon/bog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Bog - FastAPI Micro-Blogging API

FastAPI Python SQLite Docker

A modern, secure micro-blogging API built with FastAPI

πŸš€ The API is LIVE here, you are more than welcome to test it out πŸš€
πŸ“– Here's the Swagger Documentations by the way. πŸ“–

🌟 What is Bog?

Bog is a straightforward yet powerful FastAPI backend for a micro-blogging social media platform. It provides all the essential features you'd expect from a modern social platform: user registration, secure authentication, post creation and management, social following, post likes, and personalized feeds.

✨ Key Features

  • πŸ” Secure JWT Authentication - Token-based auth with bcrypt password hashing
  • πŸ‘₯ User Management - Registration, profiles, and user discovery
  • πŸ“ Post System - Create, update, delete, and manage blog posts
  • ❀️ Social Interactions - Like/unlike posts and follow/unfollow users
  • πŸ“± Feed System - Discover users and view user-specific post feeds
  • πŸš€ Fast & Modern - Built with FastAPI for high performance
  • 🐳 Docker Ready - Containerized for easy deployment
  • πŸ“š Auto-Generated Docs - Interactive API documentation with Swagger UI

πŸ› οΈ Tech Stack

  • Framework: FastAPI
  • Database: SQLite with SQLAlchemy ORM
  • Authentication: JWT tokens with passlib (bcrypt)
  • Validation: Pydantic models
  • Containerization: Docker & Docker Compose
  • Documentation: Automatic OpenAPI/Swagger docs

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • Git

Local Development Setup

  1. Clone the Repository

    git clone https://github.com/TheRootDaemon/bog.git
    cd bog
  2. Create Virtual Environment

    python -m venv venv
    
    # On Windows
    venv\Scripts\activate
    
    # On macOS/Linux
    source venv/bin/activate
  3. Install Dependencies

    pip install -r requirements.txt
  4. Run the Application

    uvicorn app.main:app --reload
  5. Access the API

🐳 Docker Deployment

Using Docker Compose (Recommended)

# Clone and navigate to project
git clone https://github.com/TheRootDaemon/bog.git
cd bog

# Build and run with Docker Compose
docker-compose up -d

# View logs
docker-compose logs -f

# Stop the application
docker-compose down

Using Docker directly

# Build the image
docker build -t bog-api .

# Run the container
docker run -d -p 10000:10000 --name bog bog-api

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

πŸ“‹ API Reference

πŸ”‘ Authentication

Method Endpoint Description Auth Required
POST /auth/token Login and get JWT token ❌

πŸ‘€ Users

Method Endpoint Description Auth Required
POST /users Register a new user ❌
POST /users/{user_id}/follow Follow a user βœ…
DELETE /users/{user_id}/unfollow Unfollow a user βœ…

πŸ“ Posts

Method Endpoint Description Auth Required
POST /posts Create a new post βœ…
PUT /posts/{post_id} Update your post βœ…
DELETE /posts/{post_id} Delete your post βœ…
POST /posts/{post_id}/like Like a post βœ…
DELETE /posts/{post_id}/like Unlike a post βœ…

πŸ“± Feed

Method Endpoint Description Auth Required
GET /feed Get random users for discovery ❌
GET /feed/{username} Get posts by username ❌

🏠 General

Method Endpoint Description Auth Required
GET / API health check ❌
GET /user Get current user info βœ…

πŸ”§ Usage Examples

Register a New User

curl -X POST "http://localhost:8000/users" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "johndoe",
    "email": "john@example.com",
    "gender": "male",
    "password": "securepassword123"
  }'

Login and Get Token

curl -X POST "http://localhost:8000/auth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=johndoe&password=securepassword123"

Create a Post

curl -X POST "http://localhost:8000/posts" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "title": "My First Post",
    "content": "Hello, Bog community! This is my first post."
  }'

Follow a User

curl -X POST "http://localhost:8000/users/2/follow" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Like a Post

curl -X POST "http://localhost:8000/posts/1/like" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Get User Feed

curl -X GET "http://localhost:8000/feed/johndoe"

πŸ“ Project Structure

bog/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py              # FastAPI app initialization
β”‚   β”œβ”€β”€ database.py          # Database configuration
β”‚   β”œβ”€β”€ models.py            # SQLAlchemy ORM models
β”‚   β”œβ”€β”€ schemas.py           # Pydantic request/response models
β”‚   └── routes/
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ auth.py          # Authentication endpoints
β”‚       β”œβ”€β”€ registerUser.py  # User registration
β”‚       β”œβ”€β”€ follow.py        # Follow/unfollow functionality
β”‚       β”œβ”€β”€ posts.py         # Post management
β”‚       └── feed.py          # Feed endpoints
β”œβ”€β”€ blog.db                  # SQLite database file
β”œβ”€β”€ requirements.txt         # Python dependencies
β”œβ”€β”€ dockerfile              # Docker configuration
β”œβ”€β”€ compose.yml             # Docker Compose configuration
└── README.md               # This file

πŸ” Authentication Flow

  1. Register: Create a new user account via /users
  2. Login: Get JWT token via /auth/token
  3. Authenticate: Include token in Authorization: Bearer <token> header
  4. Access: Use token to access protected endpoints

🌐 Database Schema

Users Table

  • id (Primary Key)
  • username (Unique)
  • email
  • gender
  • password (Hashed)

Posts Table

  • id (Primary Key)
  • author (Foreign Key to Users)
  • title
  • content

Relationships

  • Follow: Many-to-many relationship between users
  • Likes: Many-to-many relationship between users and posts

🚦 Response Codes

Code Description
200 Success
201 Created
400 Bad Request
401 Unauthorized
404 Not Found
422 Validation Error

πŸ§ͺ Testing the API

Visit the interactive API documentation at:

The Swagger UI provides a complete interface to test all endpoints with proper authentication.

πŸ”§ Configuration

Environment Variables

Currently, the application uses default configurations. For production deployment, consider setting:

  • SECRET_KEY: JWT signing secret (currently hardcoded)
  • DATABASE_URL: Database connection string
  • ACCESS_TOKEN_EXPIRE_MINUTES: Token expiration time

Database

The application uses SQLite by default with the database file blog.db. The database is automatically created when you first run the application.

πŸš€ Deployment

Production Considerations

  1. Security:

    • Change the hardcoded SECRET_KEY in auth.py
    • Use environment variables for sensitive data
    • Enable HTTPS in production
  2. Database:

    • Consider PostgreSQL for production
    • Set up proper backups
  3. Performance:

    • Use a production ASGI server like Gunicorn with Uvicorn workers
    • Set up proper logging

🀝 Contributing

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

πŸ”— Links


Made with ❀️ using FastAPI

About

Bog is a straightforward FastAPI backend for a micro blogging site. It lets users register, post content, follow others, like posts, and view feeds, with secure JWT authentication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages