pub struct Server(/* private fields */);Expand description
The primary server structure that provides a thread-safe interface to the server’s state.
This struct acts as a public-facing wrapper around an Arc<RwLock<ServerInner>>.
It allows multiple parts of the application to safely share and modify the server’s
configuration and state across different threads and asynchronous tasks.
Implementations§
Source§impl Server
Represents the server, providing methods to configure and run it.
impl Server
Represents the server, providing methods to configure and run it.
This struct wraps the ServerInner configuration and routing logic,
offering a high-level API for setting up the HTTP and WebSocket server.
Sourcepub async fn from(config: ServerConfig) -> Self
pub async fn from(config: ServerConfig) -> Self
Sourcepub async fn handle_hook(&self, hook: HookMacro)
pub async fn handle_hook(&self, hook: HookMacro)
Handle a given hook macro asynchronously.
This function dispatches the provided HookMacro to the appropriate
internal handler based on its HookType. Supported hook types include
panic hooks, request/response middleware, and routes.
§Arguments
HookMacro: TheHookMacroinstance containing theHookTypeand its handler.
Sourcepub async fn config_str<C: ToString>(&self, config_str: C) -> &Self
pub async fn config_str<C: ToString>(&self, config_str: C) -> &Self
Sourcepub async fn config(&self, config: ServerConfig) -> &Self
pub async fn config(&self, config: ServerConfig) -> &Self
Sourcepub async fn panic_hook<S>(&self) -> &Selfwhere
S: ServerHook,
pub async fn panic_hook<S>(&self) -> &Selfwhere
S: ServerHook,
Registers a panic hook handler to the processing pipeline.
This method allows registering panic hooks that implement the ServerHook trait,
which will be executed when a panic occurs during request processing.
§Type Parameters
ServerHook- The panic hook type that implementsServerHook.
§Returns
&Self- Reference to self for method chaining.
Sourcepub async fn route<S>(&self, path: impl ToString) -> &Selfwhere
S: ServerHook,
pub async fn route<S>(&self, path: impl ToString) -> &Selfwhere
S: ServerHook,
Registers a route handler for a specific path.
This method allows registering route handlers that implement the ServerHook trait,
providing type safety and better code organization.
§Type Parameters
ServerHook- The route handler type that implementsServerHook.
§Arguments
path- The route path pattern.
§Returns
&Self- Reference to self for method chaining.
Sourcepub async fn request_middleware<S>(&self) -> &Selfwhere
S: ServerHook,
pub async fn request_middleware<S>(&self) -> &Selfwhere
S: ServerHook,
Registers request middleware to the processing pipeline.
This method allows registering middleware that implements the ServerHook trait,
which will be executed before route handlers for every incoming request.
§Type Parameters
ServerHook- The middleware type that implementsServerHook.
§Returns
&Self- Reference to self for method chaining.
Sourcepub async fn response_middleware<S>(&self) -> &Selfwhere
S: ServerHook,
pub async fn response_middleware<S>(&self) -> &Selfwhere
S: ServerHook,
Registers response middleware to the processing pipeline.
This method allows registering middleware that implements the ServerHook trait,
which will be executed after route handlers for every outgoing response.
§Type Parameters
ServerHook- The middleware type that implementsServerHook.
§Returns
&Self- Reference to self for method chaining.
Sourcepub fn format_host_port<H: ToString>(host: H, port: usize) -> String
pub fn format_host_port<H: ToString>(host: H, port: usize) -> String
Sourcepub async fn run(&self) -> ServerResult<ServerControlHook>
pub async fn run(&self) -> ServerResult<ServerControlHook>
Starts the server, binds to the configured address, and begins listening for connections.
This is the main entry point to launch the server. It will initialize the panic hook, create a TCP listener, and then enter the connection acceptance loop in a background task.
§Returns
Returns a ServerResult containing a shutdown function on success.
Calling this function will shut down the server by aborting its main task.
Returns an error if the server fails to start.
Trait Implementations§
Source§impl PartialEq for Server
Implements the PartialEq trait for Server.
impl PartialEq for Server
Implements the PartialEq trait for Server.
This allows for comparing two Server instances for equality.
impl Eq for Server
Implements the Eq trait for Server.
This indicates that Server has a total equality relation.