Struct simple_server::Server[][src]

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.

Methods

impl Server
[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_server;

use simple_server::Server;

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

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_server;

use std::time::Duration;
use simple_server::Server;

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

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_server;

use simple_server::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");
}

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_server;

use simple_server::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);
}

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_server;

use simple_server::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");
}

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_server;

use simple_server::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]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Server

impl Sync for Server