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 arguments to it are:- IP of the sender of the request;
- Canonical path to the file on the machine.
-
on_requestwhen a new request has been processed. The arguments to it are:- IP of the sender of the request;
- Result of the request.
This function allows callbacks to return errors & disambiguates server errors & callback
errors with the
ServerErrorenum.
If no such error propagation is needed, consider using Server::serve_with_callback
If no observation of connections/requests is needed, consider using Server::serve
sourcepub fn serve_with_callback(
&mut self,
on_pending_request: impl FnMut(&SocketAddr, &Path),
on_request: impl FnMut(&SocketAddr, RequestResult)
) -> Result<Infallible, Error>
pub fn serve_with_callback( &mut self, on_pending_request: impl FnMut(&SocketAddr, &Path), on_request: impl FnMut(&SocketAddr, RequestResult) ) -> 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 arguments to it are:- IP of the sender of the request;
- Canonical path to the file on the machine.
-
on_requestwhen a new request has been processed. The arguments to it are:- IP of the sender of the request;
- Result of the request.
This function allows callbacks to return errors & disambiguates server errors & callback
errors with the
ServerErrorenum.
If no observation of connections/requests is needed, consider using Server::serve
If the callbacks have to return an error, consider using Server::try_serve_with_callback
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.
Equivalent to serve function and the like.
If connections/requests need to be observed (e.g. logged), use
Server::serve_with_callback