pub struct Server { /* private fields */ }Expand description
Represents the internal, mutable state of the web server.
This struct consolidates all the core components required for the server to operate, including configuration, routing, middleware, and various hooks for extending functionality.
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 Server configuration and routing logic,
offering a high-level API for setting up the HTTP and WebSocket server.
Sourcepub fn handle_hook(&mut self, hook: HookType)
pub fn handle_hook(&mut 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 fn config_from_json<C>(&mut self, json: C) -> &mut Self
pub fn config_from_json<C>(&mut self, json: C) -> &mut Self
Sourcepub fn server_config(&mut self, config: ServerConfig) -> &mut Self
pub fn server_config(&mut self, config: ServerConfig) -> &mut Self
Sourcepub fn request_config(&mut self, request_config: RequestConfig) -> &mut Self
pub fn request_config(&mut self, request_config: RequestConfig) -> &mut Self
Sourcepub fn task_panic<S>(&mut self) -> &mut Selfwhere
S: ServerHook,
pub fn task_panic<S>(&mut self) -> &mut 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
&mut Self- Reference to self for method chaining.
Sourcepub fn request_error<S>(&mut self) -> &mut Selfwhere
S: ServerHook,
pub fn request_error<S>(&mut self) -> &mut 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
&mut Self- Reference to self for method chaining.
Sourcepub fn route<S>(&mut self, path: impl AsRef<str>) -> &mut Selfwhere
S: ServerHook,
pub fn route<S>(&mut self, path: impl AsRef<str>) -> &mut 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
AsRef<str>- The route path pattern.
§Returns
&mut Self- Reference to self for method chaining.
Sourcepub fn request_middleware<S>(&mut self) -> &mut Selfwhere
S: ServerHook,
pub fn request_middleware<S>(&mut self) -> &mut 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
&mut Self- Reference to self for method chaining.
Sourcepub fn response_middleware<S>(&mut self) -> &mut Selfwhere
S: ServerHook,
pub fn response_middleware<S>(&mut self) -> &mut 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
&mut Self- Reference to self for method chaining.
Sourcepub fn format_bind_address<H>(host: H, port: u16) -> String
pub fn format_bind_address<H>(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: &mut Context,
error: &RequestError,
)
pub async fn handle_request_error( &self, ctx: &mut Context, error: &RequestError, )
Handles errors that occur while processing HTTP requests.
§Arguments
&mut 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.
Source§impl Server
impl Server
pub fn get_server_config(&self) -> &ServerConfig
pub fn get_request_config(&self) -> &RequestConfig
pub fn get_route_matcher(&self) -> &RouteMatcher
pub fn get_request_error(&self) -> &ServerHookList
pub fn get_task_panic(&self) -> &ServerHookList
pub fn get_request_middleware(&self) -> &ServerHookList
pub fn get_response_middleware(&self) -> &ServerHookList
Source§impl Server
impl Server
pub fn new( server_config: ServerConfig, request_config: RequestConfig, route_matcher: RouteMatcher, request_error: ServerHookList, task_panic: ServerHookList, request_middleware: ServerHookList, response_middleware: ServerHookList, ) -> Self
Trait Implementations§
Source§impl From<RequestConfig> for Server
Converts a RequestConfig into a Server instance.
impl From<RequestConfig> for Server
Converts a RequestConfig into a Server instance.
This allows creating a Server directly from its request configuration,
using default values for other fields.
Source§impl From<ServerConfig> for Server
Converts a ServerConfig into a Server instance.
impl From<ServerConfig> for Server
Converts a ServerConfig into a Server instance.
This allows creating a Server directly from its configuration,
using default values for other fields.
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.