+
Skip to content

Nuu-maan/chat-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat API

A real-time chat API built with FastAPI, Redis, and WebSocket support.

License: MIT Python FastAPI Redis

Features

  • Real-time chat using WebSocket connections
  • Redis-based message storage with automatic expiry
  • Room-based chat system
  • User management
  • Message history support
  • Rate limiting
  • CORS support
  • OpenAPI documentation

Recent Updates

  • Fixed Redis implementation to use modern hset commands
  • Updated Pydantic model serialization to use model_dump()
  • Improved WebSocket connection handling
  • Enhanced test coverage for database operations

Project Structure

chat-api/
├── src/                    # Source code
│   ├── api/               # API endpoints
│   │   └── v1/           # API v1 routes
│   ├── config/           # Configuration settings
│   ├── core/             # Core business logic
│   ├── middleware/       # Middleware components
│   ├── models/          # Data models
│   └── services/        # Services (Database, WebSocket)
├── tests/               # Test suite
├── static/             # Static files and documentation assets
├── .env               # Environment variables
├── example.env        # Example environment variables
├── requirements.txt   # Python dependencies
├── setup.py          # Package setup
├── pytest.ini        # Pytest configuration
└── README.md         # Project documentation

Prerequisites

  • Python 3.8+
  • Redis server
  • Virtual environment (recommended)

Quick Start

  1. Clone the repository:
git clone https://github.com/Nuu-maan/chat-api.git
cd chat-api
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Copy example.env to .env and configure your settings:
cp example.env .env

Configuration

Edit .env file to configure:

  • Redis connection settings
  • API settings
  • Rate limiting parameters
  • Logging configuration

The following environment variables are required:

  • REDIS_URL: Redis connection URL (default: redis://localhost:6379)
  • MESSAGE_EXPIRY_DAYS: Number of days to keep messages (default: 7)
  • MAX_MESSAGES_PER_ROOM: Maximum messages per room (default: 100)
  • RATE_LIMIT_REQUESTS: Maximum requests per minute (default: 60)

Running the API

  1. Start the Redis server

  2. Run the FastAPI server:

uvicorn src.main:app --reload

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

API Documentation

Visit the beautiful documentation at:

  • / - Main documentation with Swagger UI
  • /redoc - Alternative documentation with ReDoc

WebSocket Usage

Connect to Chat Room

ws://localhost:8000/api/v1/ws/{room_id}/{user_id}

Message Format

{
    "type": "text",
    "content": "Hello, World!",
    "user_id": "user123",
    "room_id": "room456"
}

REST Endpoints

Rooms

  • POST /api/v1/rooms - Create room
  • GET /api/v1/rooms - List rooms
  • GET /api/v1/rooms/{room_id} - Get room
  • GET /api/v1/rooms/{room_id}/messages - Get room messages

Users

  • POST /api/v1/users - Create user
  • GET /api/v1/users - List users
  • GET /api/v1/users/{user_id} - Get user

Messages

  • POST /api/v1/rooms/{room_id}/messages - Send message

Testing

Run the test suite:

pytest tests/ -v

Contributing

Contributions are greatly appreciated! Here's how you can help:

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

Areas for Contribution

  • Additional features and endpoints
  • Performance improvements
  • Documentation enhancements
  • Bug fixes
  • Test coverage improvements
  • Security enhancements

Development Guidelines

  • Follow PEP 8 style guide
  • Write tests for new features
  • Update documentation for changes
  • Keep commits atomic and well-described
  • Add type hints to new code

License

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

Repository

Visit the project repository at https://github.com/Nuu-maan/chat-api

About

A real-time chat API enabling WebSocket-based chat rooms and persistent messaging

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载