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, PathBuf) -> Result<Response, 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, PathBuf) -> Result<Response, 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 that’s about to be sent.
It returns the data or the path to the file that’s to be sent. To forward the choice of the server, return the second argument.
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 ServerError enum.
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, PathBuf) -> Response,
on_request: impl FnMut(&SocketAddr, RequestResult),
) -> Result<Infallible, Error>
pub fn serve_with_callback( &mut self, on_pending_request: impl FnMut(&SocketAddr, PathBuf) -> Response, on_request: impl FnMut(&SocketAddr, RequestResult), ) -> Result<Infallible, Error>
Serve all connections sequentially & indefinitely, returning only on an IO 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.
It returns the data or the path to the file that’s to be sent. To forward the choice of the server, return the second argument.
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 ServerError enum.
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