pub struct Server { /* private fields */ }Expand description
Serves static HTTP content with configurable runtime policies.
You use Server as the main entrypoint to bind an address, map requests to files
under a document root, and apply response policies such as CORS, cache hints, and
simple rate limiting.
For most production setups, prefer Server::builder so optional settings are
explicit and readable.
§Examples
use http_handle::Server;
let server = Server::new("127.0.0.1:8080", ".");
assert_eq!(server.address(), "127.0.0.1:8080");§Panics
This type does not panic on construction.
Implementations§
Source§impl Server
impl Server
Sourcepub fn new(address: &str, document_root: &str) -> Self
pub fn new(address: &str, document_root: &str) -> Self
Creates a server using the minimal required configuration.
Use this constructor when you want a quick default path. For advanced runtime
policy, prefer Server::builder.
§Examples
use http_handle::Server;
let server = Server::new("127.0.0.1:8080", ".");
assert_eq!(server.address(), "127.0.0.1:8080");§Panics
This function does not panic.
Sourcepub fn builder() -> ServerBuilder
pub fn builder() -> ServerBuilder
Sourcepub fn start(&self) -> Result<()>
pub fn start(&self) -> Result<()>
Starts a blocking HTTP/1.1 listener loop.
On Linux, macOS, and Windows, this binds a TcpListener and accepts connections
in a thread-per-connection model.
§Examples
use http_handle::Server;
let server = Server::new("127.0.0.1:8080", ".");
let _ = server.start();§Errors
Returns Err if binding fails or the listener cannot be configured.
§Panics
This function does not intentionally panic.
Sourcepub fn start_with_graceful_shutdown(
&self,
shutdown_timeout: Duration,
) -> Result<()>
pub fn start_with_graceful_shutdown( &self, shutdown_timeout: Duration, ) -> Result<()>
Starts the server with OS-signal-aware graceful shutdown.
On macOS/Linux, this responds to SIGINT/SIGTERM via the installed signal handler.
On Windows, Ctrl+C triggers equivalent shutdown behavior through the same handler API.
§Examples
use http_handle::Server;
use std::time::Duration;
let server = Server::new("127.0.0.1:8080", ".");
let _ = server.start_with_graceful_shutdown(Duration::from_secs(5));§Errors
Returns Err when binding or socket configuration fails.
§Panics
This function does not intentionally panic.
Sourcepub fn start_with_shutdown_signal(
&self,
shutdown: Arc<ShutdownSignal>,
) -> Result<()>
pub fn start_with_shutdown_signal( &self, shutdown: Arc<ShutdownSignal>, ) -> Result<()>
Starts the server with caller-managed shutdown coordination.
§Examples
use http_handle::{Server, ShutdownSignal};
use std::sync::Arc;
use std::time::Duration;
let server = Server::new("127.0.0.1:8080", ".");
let signal = Arc::new(ShutdownSignal::new(Duration::from_secs(2)));
let _ = server.start_with_shutdown_signal(signal);§Errors
Returns Err when binding or listener configuration fails.
§Panics
This function does not intentionally panic.
Sourcepub fn start_with_shutdown_signal_and_ready<F>(
&self,
shutdown: Arc<ShutdownSignal>,
on_ready: F,
) -> Result<()>
pub fn start_with_shutdown_signal_and_ready<F>( &self, shutdown: Arc<ShutdownSignal>, on_ready: F, ) -> Result<()>
Starts the server with a shutdown signal and reports the actual bound address.
This is useful when binding to port 0 in tests and callers need the kernel-assigned
port before sending requests.
§Arguments
shutdown- The shutdown signal to coordinate graceful terminationon_ready- Callback invoked once with the actual boundip:port
§Returns
A Result indicating success or an I/O error.
Sourcepub fn start_with_thread_pool(&self, thread_pool_size: usize) -> Result<()>
pub fn start_with_thread_pool(&self, thread_pool_size: usize) -> Result<()>
Starts the server with thread pooling for better resource management under load.
This method uses a fixed-size thread pool to handle connections, preventing resource exhaustion under high load by limiting the number of concurrent threads.
§Arguments
thread_pool_size- The number of worker threads in the pool
§Returns
A Result indicating success or an I/O error.
Sourcepub fn start_with_pooling(
&self,
thread_pool_size: usize,
max_connections: usize,
) -> Result<()>
pub fn start_with_pooling( &self, thread_pool_size: usize, max_connections: usize, ) -> Result<()>
Starts the server with both thread pooling and connection pooling for optimal resource management.
This method provides the highest level of resource control by combining:
- Fixed-size thread pool to limit concurrent worker threads
- Connection pool to limit the number of simultaneously processed connections
- Graceful degradation when limits are reached
§Arguments
thread_pool_size- The number of worker threads in the poolmax_connections- The maximum number of concurrent connections to process
§Returns
A Result indicating success or an I/O error.
Sourcepub fn cors_enabled(&self) -> Option<bool>
pub fn cors_enabled(&self) -> Option<bool>
Returns the CORS enabled setting
Sourcepub fn cors_origins(&self) -> &Option<Vec<String>>
pub fn cors_origins(&self) -> &Option<Vec<String>>
Returns the CORS origins setting
Sourcepub fn custom_headers(&self) -> &Option<HashMap<String, String>>
pub fn custom_headers(&self) -> &Option<HashMap<String, String>>
Returns the custom headers setting
Sourcepub fn request_timeout(&self) -> Option<Duration>
pub fn request_timeout(&self) -> Option<Duration>
Returns the request timeout setting
Sourcepub fn connection_timeout(&self) -> Option<Duration>
pub fn connection_timeout(&self) -> Option<Duration>
Returns the connection timeout setting
Sourcepub fn document_root(&self) -> &PathBuf
pub fn document_root(&self) -> &PathBuf
Returns the document root path
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Server
impl<'de> Deserialize<'de> for Server
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Server
impl StructuralPartialEq for Server
Auto Trait Implementations§
impl Freeze for Server
impl RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl UnsafeUnpin for Server
impl UnwindSafe for Server
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.