Struct shrimple_localhost::Server
source · pub struct Server { /* private fields */ }Expand description
Server for serving files locally
Implementations§
source§impl Server
impl Server
sourcepub const DEFAULT_PORT: u16 = 6_969u16
pub const DEFAULT_PORT: u16 = 6_969u16
Chosen by fair dice roll; Guaranteed to be random.
sourcepub fn current_dir() -> Result<Self, Error>
pub fn current_dir() -> Result<Self, Error>
Create a new server for listening to HTTP connections at port Server::DEFAULT_PORT
and serving files from the current directory.
If a custom port needs to be provided, use Server::new_at;
If a custom root needs to be provided, use Server::new.
sourcepub fn new(root: impl AsRef<Path>) -> Result<Self, Error>
pub fn new(root: impl AsRef<Path>) -> Result<Self, Error>
Create a new server for listening to HTTP connections at port Server::DEFAULT_PORT
and serving files from root.
If a custom port needs to be provided, use Server::new_at;
If root is only ever supposed to be the current directory, use Server::current_dir.
sourcepub fn current_dir_at(port: u16) -> Result<Self, Error>
pub fn current_dir_at(port: u16) -> Result<Self, Error>
Create a new server for listening to HTTP connections at port port
and serving files from the current directory.
If it doesn’t matter what port is used, use Server::current_dir;
If a custom root needs to be provided, use Server::new_at.
sourcepub fn new_at(root: impl AsRef<Path>, port: u16) -> Result<Self, Error>
pub fn new_at(root: impl AsRef<Path>, port: u16) -> Result<Self, Error>
Create a new server for listening to HTTP connections at addr
and serving files from root.
If it doesn’t matter what port is used, use Server::new;
If root is only ever supposed to be the current directory, use Server::current_dir_at
sourcepub fn try_serve_with_callback<E>(
&mut self,
on_pending_request: impl FnMut(&SocketAddr, &Path) -> Result<(), E>,
on_request: impl FnMut(&SocketAddr, RequestResult) -> Result<(), E>
) -> Result<Infallible, ServerError<E>>
pub fn try_serve_with_callback<E>( &mut self, on_pending_request: impl FnMut(&SocketAddr, &Path) -> Result<(), E>, on_request: impl FnMut(&SocketAddr, RequestResult) -> Result<(), E> ) -> Result<Infallible, ServerError<E>>
Serve all connections sequentially & indefinitely, returning only on an error, calling:
on_pending_requestwhen a new request is about to get a 200 response. The argument to it is a canonical path to an existing file. The actual file is accessed only after the callback returns; the callback may take advantage of this arrangement by manipulating the file if needed.on_requestwhen a new request has been processed, the request result being represented by the 2nd argument to the callback. This function allows callbacks to return errors & disambiguates server errors & callback errors with theServerErrorenum.
If no such error propagation is needed, consider usingServer::serve_with_callback
If no observation of connections/requests is needed, consider usingServer::serve
sourcepub fn serve_with_callback(
&mut self,
on_pending_request: impl FnMut(&SocketAddr, &Path) + Send + Sync,
on_request: impl FnMut(&SocketAddr, RequestResult) + Send + Sync
) -> Result<Infallible, Error>
pub fn serve_with_callback( &mut self, on_pending_request: impl FnMut(&SocketAddr, &Path) + Send + Sync, on_request: impl FnMut(&SocketAddr, RequestResult) + Send + Sync ) -> Result<Infallible, Error>
Serve all connections sequentially & indefinitely, returning only on an error, calling:
on_pending_requestwhen a new request is about to get a 200 response. The argument to it is a canonical path to an existing file. The actual file is accessed only after the callback returns; the callback may take advantage of this arrangement by manipulating the file if needed.on_requestwhen a new request has been processed, the request result being represented by the 2nd argument to the callback. If no observation of connections/requests is needed, consider usingServer::serve
sourcepub fn serve(&mut self) -> Result<Infallible, Error>
pub fn serve(&mut self) -> Result<Infallible, Error>
Serve all connections sequentially & indefinitely, returning only on an error.
If connections/requests need to be observed (e.g. logged), use
Server::serve_with_callback