Struct shrimple_localhost::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, &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_request when 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_request when 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 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, &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_request when 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_request when 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 using Server::serve
source

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

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>,

§

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>,

§

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.