Skip to main content

hyperlane/server/
struct.rs

1use crate::*;
2
3/// Represents the state associated with a single connection hook.
4///
5/// This struct encapsulates the necessary context for processing a connection,
6/// including a reference to the network stream and request configuration. It is created
7/// for each connection and passed to the relevant handlers.
8#[derive(Clone, CustomDebug, DisplayDebug, Getter)]
9pub(crate) struct HandlerState {
10    /// A reference to the underlying network stream for the connection.
11    pub(super) stream: ArcRwLockStream,
12    /// The request config for the current connection.
13    pub(super) request_config: RequestConfigData,
14}
15
16/// Represents the internal, mutable state of the web server.
17///
18/// This struct consolidates all the core components required for the server to operate,
19/// including configuration, routing, middleware, and various hooks for extending functionality.
20/// It is not intended to be used directly by end-users, but rather wrapped within the `Server` struct
21/// for thread-safe access.
22#[derive(Clone, CustomDebug, Data, DisplayDebug)]
23pub(crate) struct ServerData {
24    /// Stores the server's configuration settings, such as address, port, and timeouts.
25    #[get(pub(super))]
26    #[get_mut(pub(super))]
27    #[set(pub(super))]
28    pub(super) server_config: ServerConfigData,
29    /// The configuration for HTTP request.
30    #[get(pub(crate))]
31    #[get_mut(pub(super))]
32    #[set(pub(super))]
33    pub(super) request_config: RequestConfigData,
34    /// The routing component responsible for matching incoming requests to their registered handlers.
35    #[get(pub(super))]
36    #[get_mut(pub(super))]
37    #[set(pub(super))]
38    pub(super) route_matcher: RouteMatcher,
39    /// A collection of request error handlers that are invoked when a request error occurs during HTTP request processing.
40    /// This allows for graceful error recovery and customized error responses.
41    #[debug(skip)]
42    #[get(pub(super))]
43    #[get_mut(pub(super))]
44    #[set(pub(super))]
45    pub(super) request_error: ServerHookList,
46    /// A collection of task panic handlers that are invoked when a panic occurs during request processing.
47    /// This allows for graceful error recovery and customized error responses.
48    #[debug(skip)]
49    #[get(pub(super))]
50    #[get_mut(pub(super))]
51    #[set(pub(super))]
52    pub(super) task_panic: ServerHookList,
53    /// A collection of request middleware handlers.
54    #[debug(skip)]
55    #[get(pub(super))]
56    #[get_mut(pub(super))]
57    #[set(pub(super))]
58    pub(super) request_middleware: ServerHookList,
59    /// A collection of response middleware handlers.
60    #[debug(skip)]
61    #[get(pub(super))]
62    #[get_mut(pub(super))]
63    #[set(pub(super))]
64    pub(super) response_middleware: ServerHookList,
65}
66
67/// The primary server structure that provides a thread-safe interface to the server's state.
68///
69/// This struct acts as a public-facing wrapper around an `Arc<RwLock<ServerData>>`.
70/// It allows multiple parts of the application to safely share and modify the server's
71/// configuration and state across different threads and asynchronous tasks.
72#[derive(Clone, CustomDebug, Default, DisplayDebug, Getter)]
73pub struct Server(#[get(pub(super))] pub(super) SharedServerState);