How azarenkov/chat-app works
This project is a web-based chat application built in Rust using Actix Web. The application supports user registration, login with JWT authentication (with tokens valid for 1 hour), retrieval of chat history, and real-time chatting through WebSocket connections. MongoDB is used for persistent data storage, and Sentry is integrated for error logging. Docker (via docker-compose) and Nginx are also part of the ecosystem for deployment and reverse proxy management.
Main Components
• Client: Representing end-users (mobile or web clients) that perform HTTP requests (for registration, login, fetching chat history) and establish WebSocket connections for real-time messaging.
• HTTP API Server: The central server (built with Actix Web) that exposes endpoints for authentication, message handling, and WebSocket communication.
• Controllers: Clearly separated modules such as the auth_controller and message_controller that handle incoming HTTP requests and route them appropriately.
• Services: Business logic is abstracted in layers like auth_service, message_service, and JWT service (encapsulated in src/services and infrastructure/jwt). This layer processes the logic needed for authenticating users, sending messages, and managing JWT tokens.
• Repository/Data Access Layer: Modules in src/repositories (such as user_repository and message_repository) which interface with MongoDB. They encapsulate database operations.
• Infrastructure: Contains essential supportive components, including:
• Database Connector (src/infrastructure/db) for handling MongoDB connections.
• JWT Management (src/infrastructure/jwt) for token generation/verification.
• WebSocket Handling (src/infrastructure/ws) to manage real-time communication.
• App Setup (initialization routines and configuration management).
• External Services and Tools:
• MongoDB: The document-oriented database storing user information and chat history.
• Sentry: Integrated middleware for monitoring and logging errors.
• Nginx: Likely used as a reverse proxy/load balancer (as indicated by the badge in the README).
• Docker/Docker-Compose: Used for containerization and orchestrating services (as seen with docker-compose.yml and init-mongo.js).
Relationships and Interactions
• Clients communicate with the API server through HTTP/HTTPS for registration, login, and retrieving old messages.
• For authentication, the auth_controller handles user credentials, delegates to the auth_service, which in turn utilizes the JWT service and repository layer for user verification and token generation.
• Once authenticated, clients use the JWT token to establish a WebSocket connection (managed by the WebSocket infrastructure). The ws module processes incoming messages, which are then sent via the message_controller and further handled in the service and repository layers.
• Across these interactions, controllers call corresponding services; services interact with repositories to read/write from MongoDB.
• Middleware (Sentry) monitors HTTP requests and WebSocket activities, capturing errors and performance metrics.
• Docker and Nginx play roles in deployment and traffic management.
Architectural Patterns and Design Principles
• Layered Architecture: The separation of controllers, services, repositories, and infrastructure clearly indicates a layered approach, making the system modular and maintainable.
• Dependency Abstraction: Abstract service interfaces (e.g., auth_service_abstract, jwt_service_abstract) facilitate easier modifications or testing.
• Real-Time and HTTP Duality: Handling both stateless HTTP requests and persistent WebSocket connections highlights a dual-mode interaction pattern.
• Security: Incorporation of JWT tokens for session management and authentication provides a secure access layer.