Manager Crate
Overview
Manager is a scalable, async-driven system that handles requests and communication between different components using a pub/sub model in Rust. It is built on top of the Actix Web framework for HTTP server handling and utilizes the Tokio runtime for asynchronous tasks. This crate allows you to create handlers that process requests and handle messages via a message bus, making it highly modular and easy to extend.
Features
- Dynamic Handler Registration: Register new handlers dynamically that can process requests and publish messages.
- Pub/Sub Messaging: Implement publish/subscribe messaging between different services or components.
- Concurrency Control: Uses a semaphore to limit to
nrrequest at the time from HTTP requests. Handlers inside the service can replicate as many times as they are configured. - TLS Support: Configure secure HTTPS connections with optional client certificate verification.
- File Streaming: Upload and download files with metadata support.
- Graceful Shutdown: Includes an HTTP shutdown mechanism for controlled termination of the service.
- Asynchronous Processing: All handlers and requests are processed asynchronously using Tokio's async runtime.
- Shared State: All handlers can access a shared state to store and retrieve data.
Prerequisites
Before you begin, ensure you have met the following requirements:
- Rust version >= 1.85 (due to the use 2024 rust version).
Installation
To use this crate in your project, add the following dependencies to your Cargo.toml file:
[]
= "0.1"
= "0.6.0"
Core Concepts
Handlers
Handlers process incoming requests and communicate with other handlers. Implement the Base trait to create custom handlers.
Message Bus
The MultiBus facilitates communication between handlers using publish/subscribe patterns.
Shared State
A thread-safe storage mechanism accessible by all handlers, supporting various data types and callable functions.
How to Use
Define a Handler
To create a custom handler, you need to implement the Base trait:
use async_trait;
use Arc;
use ;
use Duration;
use sleep;
use MultiBus;
use ;
;
Create and Start the Manager
The Manager is responsible for initializing all the handlers and launching the HTTP server:
use Manager;
use HashMap;
async
File Handling Traits
The manager provides three methods in the Base trait that you can implement to create handler for file management:
-
UploadHandler: Implement this trait to customize how files are uploaded and stored.
-
DownloadHandler: Implement this trait to customize how files are downloaded.
-
MetadataHandler: Implement this trait to customize how file metadata is retrieved.
Configuration Options
HTTP Endpoints
Handler Endpoints
POST /{handler_name}
Send a request to a registered handler.
Example:
File Operations
Upload a File: POST /stream/upload/{file_name}
Upload files to the server.
Example:
Download a File: GET /stream/download/{file_id}
Download a previously uploaded file.
Example:
Retrieve File Metadata: GET /stream/metadata/{file_id}
Get metadata for a file.
Example:
System Management
Shutdown Server: POST /shutdown
Gracefully shut down the server.
Example:
Authentication
The system uses middleware to authenticate requests using API keys. Include your API key in the Authorization header as a Bearer token.
Configuration Options
The Manager instance provides several configuration methods:
// Configure TLS with optional client certificate verification
manager.with_tls;
// Set allowed client certificate names for authentication - only working if a CA is provided
manager.with_allowed_names;
// Set API key for authentication
manager.with_api_key;
// Set maximum concurrent HTTP requests
manager.with_max_requests;
// Set keep-alive timeout in seconds
manager.with_keep_alive;
Error Handling
Errors during request processing or message dispatching are handled gracefully, and appropriate error messages are returned. If a handler encounters an error, it logs the issue and returns an error message.
Example Error Response
More examples can be seen in the tests.
License
This crate is open-sourced under the MIT license.