monolake_services/
lib.rs

1#![feature(let_chains)]
2#![feature(impl_trait_in_assoc_type)]
3//! # Monolake Services
4//!
5//! `monolake-services` is a crate that provides a collection of services
6//! for building high-performance, modular HTTP servers and Thrift services. It offers a range of
7//! components that can be easily combined with custom user-created services to create robust and
8//! flexible server applications.
9//!
10//! ## Key Components
11//!
12//! ### HTTP Services
13//!
14//! #### Connection Handlers
15//!
16//! - [`HttpCoreService`](http::core): The main service for handling HTTP/1.1 and HTTP/2
17//!   connections.
18//! - [`H2Detect`](http::detect): Automatic detection of HTTP protocol versions. #[cfg_attr(feature
19//!   = "hyper", doc = "- [`HyperCoreService`](hyper::HyperCoreService): A high-performance HTTP
20//!   service built on top of the Hyper library.")]
21//!
22//! #### Request Handlers
23//!
24//! - [`ConnectionReuseHandler`](http::handlers::connection_persistence): Manages HTTP connection
25//!   persistence and keep-alive behavior. It ensures proper handling of connection lifecycles
26//!   across different HTTP versions.
27//!
28//! - [`ContentHandler`](http::handlers::content_handler): Handles content encoding and decoding for
29//!   requests and responses. It supports various compression methods and ensures efficient data
30//!   transfer.
31//!
32//! - [`RewriteAndRouteHandler`](http::handlers::route): Directs requests to appropriate handlers
33//!   based on predefined rules. It allows for flexible URL-based routing and request dispatching.
34//!
35//! - [`UpstreamHandler`](http::handlers::upstream): Manages proxying of requests to upstream
36//!   servers. It supports load balancing, connection pooling, and error handling for backend
37//!   services.
38//!
39//! - [`OpenIdHandler`](crate::http::handlers::OpenIdHandler): Provides OpenID Connect
40//!   authentication (optional feature). It enables secure user authentication using OpenID Connect
41//!   protocols.
42//!
43//! ### Thrift Services
44//!
45//! - [`TtheaderCoreService`](thrift::ttheader): Core service for handling Thrift THeader protocol
46//!   connections.
47//! - [`ProxyHandler`](thrift::handlers::proxy): Proxy service for routing Thrift requests to
48//!   upstream servers.
49//!
50//! The Thrift module provides components for handling Thrift protocol communications, including
51//! core services for processing Thrift requests and proxy handlers for routing requests
52//! to upstream Thrift servers. It supports the THeader protocol, connection pooling, and
53//! integrates seamlessly with the `service_async` framework.
54//!
55//! ### Common Services
56//!
57//! - [`CatchPanicService`](common::CatchPanicService): Catches panics in inner services and
58//!   converts them to errors. It enhances system stability by preventing panics from crashing the
59//!   entire server.
60//!
61//! - [`ContextService`](common::ContextService): Inserts context information into the request
62//!   processing pipeline. It works with `certain_map` for flexible and type-safe context
63//!   management.
64//!
65//! - [`TimeoutService`](common::TimeoutService) Adds configurable timeout functionality to any
66//!   inner service. It ensures that long-running operations don't block the server indefinitely.
67//!
68//! ### TLS Service
69//!
70//! - [`UnifiedTlsService`](crate::tls): Provides a unified interface for different TLS
71//!   implementations (Rustls and Native TLS). It allows for flexible TLS configuration and seamless
72//!   switching between TLS backends.
73//!
74//! ### Proxy Protocol Service
75//!
76//! - [`ProxyProtocolService`](crate::proxy_protocol::ProxyProtocolService): Handles PROXY protocol
77//!   headers in incoming connections. It preserves client IP information when operating behind load
78//!   balancers or proxies.
79//!
80//! ## Service Trait
81//!
82//! All services in this crate implement the `Service` trait, which is defined as follows:
83//!
84//! ```ignore
85//! pub trait Service<Request> {
86//!     type Response;
87//!     type Error;
88//!
89//!     fn call(&self, req: Request) -> impl Future<Output = Result<Self::Response, Self::Error>>;
90//! }
91//! ```
92//!
93//! This trait allows for efficient and flexible composition of services, enabling
94//! the creation of complex processing pipelines.
95//!
96//! ## Features
97//!
98//! - Modular design allowing easy composition of services
99//! - Support for HTTP/1.x and HTTP/2 protocols
100//! - Support for Thrift THeader protocol
101//! - Flexible routing and request processing capabilities
102//! - TLS support with multiple backend options
103//! - PROXY protocol support for preserving client IP information
104//!
105//! ## Usage Example
106//!
107//! Here's a basic example of how to compose these services:
108//!
109//! ```ignore
110//! use monolake_services::{
111//!     HttpCoreService, H2Detect, ConnectionReuseHandler,
112//!     ContentHandler, RewriteAndRouteHandler, UpstreamHandler, UnifiedTlsService,
113//!     ProxyProtocolService, HyperCoreService
114//! };
115//! use service_async::{layer::FactoryLayer, stack::FactoryStack};
116//!
117//! let config = ServerConfig {
118//!     // ... configuration options ...
119//! };
120//!
121//! let stack = FactoryStack::new(config)
122//!     .push(UpstreamHandler::layer())
123//!     .push(RewriteAndRouteHandler::layer())
124//!     .push(ContentHandler::layer())
125//!     .push(ConnectionReuseHandler::layer())
126//!     .push(HyperCoreService::layer());
127//!     .push(H2Detect::layer())
128//!     .push(UnifiedTlsService::layer())
129//!     .push(ContextService::layer());
130//!
131//!
132//! let service = stack.make_async().await.unwrap();
133//! // Use the service to handle incoming connections
134//! ```
135//!
136//! ## Performance Considerations
137//!
138//! - Efficient async I/O operations using the `monoio` runtime
139//! - Connection pooling and keep-alive support for improved resource utilization
140//! - Optimized routing and request handling
141//! - Support for HTTP/2 multiplexing
142//! - Efficient Thrift request processing and proxying
143//!
144//! ## Customization
145//!
146//! The modular nature of the services allows for easy extension and customization.
147//! Users can implement their own services that conform to the `Service` trait
148//! and integrate them seamlessly into the processing pipeline.
149//!
150//! ## Additional Resources
151//!
152//! For more detailed information on each component, please refer to the documentation
153//! of individual modules and the examples directory in the crate's repository.
154pub mod common;
155pub mod http;
156pub mod tcp;
157pub mod thrift;
158
159#[cfg(feature = "proxy-protocol")]
160pub mod proxy_protocol;
161
162#[cfg(feature = "tls")]
163pub mod tls;
164
165#[cfg(feature = "hyper")]
166pub mod hyper;