Struct Server

Source
pub struct Server { /* private fields */ }
Expand description

Server for serving files locally

Implementations§

Source§

impl Server

Source

pub const DEFAULT_PORT: u16 = 6_969u16

Chosen by fair dice roll; Guaranteed to be random.

Source

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.

Source

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.

Source

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.

Source

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

Source

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_request when 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_request when 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

Source

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_request when 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_request when 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

Source

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

Auto Trait Implementations§

§

impl Freeze for Server

§

impl RefUnwindSafe for Server

§

impl Send for Server

§

impl Sync for Server

§

impl Unpin for Server

§

impl UnwindSafe for Server

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.