Skip to content

karimovsardorbek/real_time_news_fastapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📰 Real-Time News Portal (FastAPI)

License: MIT
Python
FastAPI

A high-performance real-time news aggregation portal built with FastAPI that delivers live updates via WebSocket with async REST API endpoints.


✨ Key Features

  • 🔴 Live Updates: WebSocket-powered real-time news streaming
  • High Performance: Async FastAPI with SQLAlchemy
  • 🛠️ Admin Tools: Generate mock news with Faker library
  • 🔐 JWT Auth: Secure token-based authentication with bcrypt

🚀 Quick Start

Prerequisites

  • Python 3.8+
  • pip package manager

Installation

# Clone repository
git clone https://github.com/karimovsardorbek/real_time_news_fastapi.git
cd real_time_news_fastapi

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate     # Windows

# Install dependencies
pip install -r requirements.txt

# Start FastAPI server
cd fastapi-news-portal
uvicorn main:app --host 0.0.0.0 --port 8010 --reload

# Frontend (in new terminal)
cd frontend
python -m http.server 8020

📡 API Documentation

🔐 Authentication Endpoints

Register User

curl -X POST 'http://localhost:8010/register/' \
-H 'Content-Type: application/json' \
-d '{
  "username": "admin",
  "password": "yourpassword"
}'

Login & Get Token

curl -X POST 'http://localhost:8010/token' \
-H 'Content-Type: application/json' \
-d '{
  "username": "admin", 
  "password": "yourpassword"
}'

Response:

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

📰 News Endpoints

Get All Articles

curl -X GET 'http://localhost:8010/api/articles/'

Generate Fake News (Protected)

curl -X POST 'http://localhost:8010/api/generate-news/' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'

🔌 WebSocket Connection

const socket = new WebSocket('ws://localhost:8010/ws/news/');

🛠️ Tech Stack

Backend:

  • FastAPI - Modern, fast web framework
  • SQLAlchemy - Python SQL toolkit and ORM
  • Uvicorn - ASGI server implementation
  • SQLite - Lightweight database
  • WebSockets - Real-time bidirectional communication
  • JWT - JSON Web Token authentication
  • Bcrypt - Password hashing
  • Faker - Generate fake data for testing

Frontend:

  • Vanilla JavaScript - Pure JS with modern ES6+ features
  • HTML5 - Semantic markup
  • CSS3 - Modern styling with animations & grid
  • WebSocket API - Browser native WebSocket support

🔧 Development

Database Models

The application uses SQLAlchemy with SQLite for data persistence:

  • Articles are stored with auto-incrementing IDs
  • Users have bcrypt-hashed passwords
  • Database is created automatically on first run

WebSocket Broadcasting

When new articles are generated:

  1. Article is saved to database
  2. WebSocket manager broadcasts to all connected clients
  3. Frontend receives update and adds article to DOM

🚀 Deployment

Production Setup

# Install production server
pip install gunicorn

# Run with Gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8010

# Or with Uvicorn (recommended)
uvicorn main:app --host 0.0.0.0 --port 8010 --workers 4

🤝 Contributing

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

About

Real time news with fastapi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors