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 get_route_matcher(&self) -> RouteMatcher
pub async fn get_route_matcher(&self) -> RouteMatcher
Sourcepub async fn handle_hook(&self, hook: HookType)
pub async fn handle_hook(&self, hook: HookType)
Registers a hook into the server’s processing pipeline.
This function dispatches the provided HookType to the appropriate
internal hook collection based on its variant. The hook will be executed
at the corresponding stage of request processing according to its type:
Panic: Added to panic handlers for error recoveryRequestError: Added to request error handlersRequestMiddleware: Added to pre-route middleware chainRoute: Registered as a route handler for the specified pathResponseMiddleware: Added to post-route middleware chain
§Arguments
HookType- TheHookTypeinstance containing the hook configuration and factory.
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 task_panic<S>(&self) -> &Selfwhere
S: ServerHook,
pub async fn task_panic<S>(&self) -> &Selfwhere
S: ServerHook,
Registers a task panic handler to the processing pipeline.
This method allows registering task panic handlers that implement the ServerHook trait,
which will be executed when a panic occurs during request processing.
§Type Parameters
ServerHook- The task panic handler type that implementsServerHook.
§Returns
&Self- Reference to self for method chaining.
Sourcepub async fn request_error<S>(&self) -> &Selfwhere
S: ServerHook,
pub async fn request_error<S>(&self) -> &Selfwhere
S: ServerHook,
Registers a request error handler to the processing pipeline.
This method allows registering request error handlers that implement the ServerHook trait,
which will be executed when a request error occurs during HTTP request processing.
§Type Parameters
ServerHook- The request error handler 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 hook 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 hook 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: u16) -> String
pub fn format_host_port<H: ToString>(host: H, port: u16) -> String
Sourcepub fn try_flush_stdout() -> Result<()>
pub fn try_flush_stdout() -> Result<()>
Sourcepub fn flush_stdout()
pub fn flush_stdout()
Sourcepub fn try_flush_stderr() -> Result<()>
pub fn try_flush_stderr() -> Result<()>
Sourcepub fn flush_stderr()
pub fn flush_stderr()
Sourcepub fn try_flush_stdout_and_stderr() -> Result<()>
pub fn try_flush_stdout_and_stderr() -> Result<()>
Flushes both the standard output and error streams.
§Returns
io::Result<()>- The result of the flush operation.
Sourcepub fn flush_stdout_and_stderr()
pub fn flush_stdout_and_stderr()
Flushes both the standard output and error streams.
§Panics
This function will panic if either flush operation fails.
Sourcepub async fn handle_request_error(&self, ctx: &Context, error: &RequestError)
pub async fn handle_request_error(&self, ctx: &Context, error: &RequestError)
Handles errors that occur while processing HTTP requests.
§Arguments
&Context- The request context.&RequestError- The error that occurred.
Sourcepub async fn run(&self) -> Result<ServerControlHook, ServerError>
pub async fn run(&self) -> Result<ServerControlHook, ServerError>
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 Result 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.