Struct rouille_maint_in::Server[][src]

pub struct Server<F> { /* fields omitted */ }

A listening server.

This struct is the more manual server creation API of rouille and can be used as an alternative to the start_server function.

The start_server function is just a shortcut for Server::new followed with run. See the documentation of the start_server function for more details about the handler.

Example

use rouille::Server;
use rouille::Response;

let server = Server::new("localhost:0", |request| {
    Response::text("hello world")
}).unwrap();
println!("Listening on {:?}", server.server_addr());
server.run();

Implementations

impl<F> Server<F> where
    F: Send + Sync + 'static + Fn(&Request) -> Response
[src]

pub fn new<A>(
    addr: A,
    handler: F
) -> Result<Server<F>, Box<dyn Error + Send + Sync>> where
    A: ToSocketAddrs
[src]

Builds a new Server object.

After this function returns, the HTTP server is listening.

Returns an error if there was an error while creating the listening socket, for example if the port is already in use.

pub fn pool_size(self, pool_size: usize) -> Self[src]

Use a ThreadPool of the given size to process requests

pool_size must be greater than zero or this function will panic.

pub fn server_addr(&self) -> SocketAddr[src]

Returns the address of the listening socket.

pub fn run(self)[src]

Runs the server forever, or until the listening socket is somehow force-closed by the operating system.

pub fn poll(&self)[src]

Processes all the client requests waiting to be processed, then returns.

This function executes very quickly, as each client requests that needs to be processed is processed in a separate thread.

pub fn stoppable(self) -> (JoinHandle<()>, Sender<()>)[src]

Creates a new thread for the server that can be gracefully stopped later.

This function returns a tuple of a JoinHandle and a Sender. You must call JoinHandle::join() otherwise the server will not run until completion. The server can be stopped at will by sending it an empty () message from another thread. There may be a maximum of a 1 second delay between sending the stop message and the server stopping. This delay may be shortened in future.

use std::thread;
use std::time::Duration;
use rouille::Server;
use rouille::Response;

let server = Server::new("localhost:0", |request| {
    Response::text("hello world")
}).unwrap();
println!("Listening on {:?}", server.server_addr());
let (handle, sender) = server.stoppable();

// Stop the server in 3 seconds
thread::spawn(move || {
    thread::sleep(Duration::from_secs(3));
    sender.send(()).unwrap();
});

// Block the main thread until the server is stopped
handle.join().unwrap();

pub fn poll_timeout(&self, dur: Duration)[src]

Same as poll() but blocks for at most duration before returning.

This function can be used implement a custom server loop in a more CPU-efficient manner than calling poll.

Example

use rouille::Server;
use rouille::Response;

let server = Server::new("localhost:0", |request| {
    Response::text("hello world")
}).unwrap();
println!("Listening on {:?}", server.server_addr());

loop {
    server.poll_timeout(std::time::Duration::from_millis(100));
}

Auto Trait Implementations

impl<F> !RefUnwindSafe for Server<F>

impl<F> Send for Server<F> where
    F: Send + Sync

impl<F> !Sync for Server<F>

impl<F> Unpin for Server<F>

impl<F> !UnwindSafe for Server<F>

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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,