[][src]Struct simple_test_bbarekas::Server

pub struct Server { /* fields omitted */ }

A web server.

This is the core type of this crate, and is used to create a new server and listen for connections.

Implementations

impl Server[src]

pub fn new<H>(handler: H) -> Server where
    H: Fn(Request<Vec<u8>>, ResponseBuilder) -> ResponseResult + 'static + Send + Sync
[src]

Constructs a new server with the given handler.

The handler function is called on all requests.

Errors

The handler function returns a Result so that you may use ? to handle errors. If a handler returns an Err, a 500 will be shown.

If you'd like behavior other than that, return an Ok(Response) with the proper error code. In other words, this behavior is to gracefully handle errors you don't care about, not for properly handling non-HTTP 200 responses.

Examples

extern crate simple_test_bbarekas;

use simple_test_bbarekas::Server;

fn main() {
    let server = Server::new(|request, mut response| {
        Ok(response.body("Hello, world!".as_bytes().to_vec())?)
    });
}

pub fn with_timeout<H>(timeout: Duration, handler: H) -> Server where
    H: Fn(Request<Vec<u8>>, ResponseBuilder) -> ResponseResult + 'static + Send + Sync
[src]

Constructs a new server with the given handler and the specified request timeout.

The handler function is called on all requests.

Errors

The handler function returns a Result so that you may use ? to handle errors. If a handler returns an Err, a 500 will be shown.

If you'd like behavior other than that, return an Ok(Response) with the proper error code. In other words, this behavior is to gracefully handle errors you don't care about, not for properly handling non-HTTP 200 responses.

Examples

extern crate simple_test_bbarekas;

use std::time::Duration;
use simple_test_bbarekas::Server;

fn main() {
    let server = Server::with_timeout(Duration::from_secs(5), |request, mut response| {
        Ok(response.body("Hello, world!".as_bytes().to_vec())?)
    });
}

pub fn listen(&self, host: &str, port: &str) -> ![src]

Tells the server to listen on a specified host and port.

A threadpool is created, and used to handle connections. The pool size is four threads.

This method blocks forever.

The listen method will also serve static files. By default, that directory is "public" in the same directory as where it's run. If you'd like to change this default, please see the set_static_directory method.

If someone tries a path directory traversal attack, this will return a 404. Please note that this is a best effort at the moment.

Panics

There are several circumstances in which listen can currently panic:

Finally, if reading from the stream fails. Timeouts and connection closes are handled, other errors may result in a panic. This will only take down one of the threads in the threadpool, rather than the whole server.

Examples

extern crate simple_test_bbarekas;

use simple_test_bbarekas::Server;

fn main() {
    let server = Server::new(|request, mut response| {
        Ok(response.body("Hello, world!".as_bytes().to_vec())?)
    });

    server.listen("127.0.0.1", "7979");
}

pub fn listen_on_socket(&self, listener: TcpListener) -> ![src]

Tells the server to listen on a provided TcpListener.

A threadpool is created, and used to handle connections. The pool size is four threads.

This method blocks forever.

This method will also serve static files out of a public directory in the same directory as where it's run. If someone tries a path directory traversal attack, this will return a 404.

Examples

extern crate simple_test_bbarekas;

use simple_test_bbarekas::Server;
use std::net::TcpListener;

fn main() {
    let listener = TcpListener::bind(("127.0.0.1", 7979))
        .expect("Error starting the server.");

    let server = Server::new(|request, mut response| {
        Ok(response.body("Hello, world!".as_bytes().to_vec())?)
    });

    server.listen_on_socket(listener);
}

pub fn set_static_directory<P: Into<PathBuf>>(&mut self, path: P)[src]

Sets the proper directory for serving static files.

By default, the server will serve static files inside a public directory. This method lets you set a path to whatever location you'd like.

Examples

extern crate simple_test_bbarekas;

use simple_test_bbarekas::Server;

fn main() {
    let mut server = Server::new(|request, mut response| {
        Ok(response.body("Hello, world!".as_bytes().to_vec())?)
    });

    server.set_static_directory("/var/www/");

    server.listen("127.0.0.1", "7979");
}

pub fn dont_serve_static_files(&mut self)[src]

Disables serving static files.

By default, the server will serve static files inside a public directory, or the directory set by set_static_directory. This method lets you disable this.

It can be re-enabled by a subsequent call to set_static_directory.

Examples

extern crate simple_test_bbarekas;

use simple_test_bbarekas::Server;

fn main() {
    let mut server = Server::new(|request, mut response| {
        Ok(response.body("Hello, world!".as_bytes().to_vec())?)
    });

    server.dont_serve_static_files();

    server.listen("127.0.0.1", "7979");
}

Trait Implementations

impl Debug for Server[src]

Auto Trait Implementations

impl !RefUnwindSafe for Server

impl Send for Server

impl Sync for Server

impl Unpin for Server

impl !UnwindSafe for Server

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.