Struct HttpServer

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

The server instance that represents and controls the underlying http-service.

Implementations§

Source§

impl HttpServer

Source

pub fn new() -> Self

Create a new server instance using default configurations and server settings.

Source

pub fn new_with_config(config: ServerConfig) -> Self

Create a new server instance with supplied configuration and settings.

Source

pub fn listen(&mut self, port: u16)

listen will take 1 parameter for the port that the server will be monitoring at, aka 127.0.0.1:port. This function will block until the server is shut down.

§Examples
extern crate rusty_express as express;
use express::prelude::{HttpServer, ServerDef, Router, Route};

let mut server = HttpServer::new();
let mut router = Route::new();

// ... code to add router handlers to ...

server.def_router(router);
server.listen(8080);
Source

pub fn listen_and_serve( &mut self, port: u16, callback: Option<fn(AsyncController)>, )

listen_and_serve will take 2 parameters: 1) the port that the server will be monitoring at, or 127.0.0.1:port; 2) the callback closure that will take an async-controller as input, and run in parallel to the current server instance for async operations.

This function will block until the server is shut down.

§Examples
extern crate rusty_express as express;
use express::prelude::{HttpServer, ServerDef, Router, Route, ControlMessage};
use std::thread;
use std::time::Duration;

let mut server = HttpServer::new();
let mut router = Route::new();

// ... code to add router handlers to ...

server.def_router(router);
server.listen_and_serve(8080, |controller| {
    // sleep for 1 minute
    thread::sleep(Duration::from_secs(60));

    // after waking up from the 1 minute sleep, shut down the server.
    controller.send(ControlMessage::Terminate);
});
Source

pub fn get_courier(&self) -> AsyncController

Obtain an AsyncController, which can be run in a parallel thread and control or update server configurations while it’s running. For more details, see the states module.

Source

pub fn drop_session_auto_clean(&mut self)

Stop and clear the session auto-cleaning schedules. This API can be useful when no more new sessions shall be built and stored in the server, to save server resources.

Source

pub fn config(&mut self) -> &mut ServerConfig

Obtain a reference to the server config, such that we can make updates before launching the server but without creating the config struct and pass it in on building the server.

Source

pub fn config_hot_reload(&self)

Ask the server to reload the configuration settings. Usually used in a separate thread with a cloned server instance, where the server state is corrupted and need a reload to restore the initial server settings.

Trait Implementations§

Source§

impl Default for HttpServer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Router for HttpServer

Source§

fn use_static(&mut self, path: PathBuf) -> &mut dyn Router

§Example
extern crate rusty_express;
use rusty_express::prelude::*;
use std::path::PathBuf;
fn main() {
   // define http server now
   let mut server = HttpServer::new();
   server.set_pool_size(8);
   server.use_static(PathBuf::from(r".\static"));
}
Source§

fn get( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn patch( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn post( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn put( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn delete( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn options( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn other( &mut self, method: &str, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn all( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router

Source§

fn use_custom_static( &mut self, uri: RequestPath, path: PathBuf, ) -> &mut dyn Router

Source§

fn case_sensitive(&mut self, allow_case: bool, method: Option<REST>)

Source§

impl ServerDef for HttpServer

Source§

fn def_router(&mut self, router: Route)

Replace the default server router with the pre-built one. This is a wrapper over Route::use_router, which will achieve same goal.

Source§

fn set_pool_size(&mut self, size: usize)

Set the pool size. Note that this API is only functional before launching the server. After the server has started and been running, set_pool_size will be no-op.

Source§

fn set_read_timeout(&mut self, timeout: u16)

Set the read timeout for the handler. If no more incoming data stream are detected on the socket, the read stream will be closed.

Source§

fn set_write_timeout(&mut self, timeout: u16)

Set the write timeout for the handler. If no more outgoing data stream are detected on the socket, the write stream will be closed.

Source§

fn def_default_response_header(&mut self, header: HashMap<String, String>)

Define headers and their contents that shall go along with every http response. This will remove any existing default headers and corresponding contents.

Source§

fn set_default_response_header(&mut self, field: String, value: String)

Set or update default headers and their contents that shall go along with every http response. If a default header with same name exists, the new contents will replace the existing one.

Source§

fn enable_session_auto_clean(&mut self, auto_clean_period: Duration)

If using the session feature, this API will automatically purge stale session stores, such that we can reclaim resources that’s no longer in use.

Source§

fn disable_session_auto_clean(&mut self)

If using the session feature, this API will turn off the periodic session store clean up

Source§

impl ViewEngineDefinition for HttpServer

Source§

fn view_engine(extension: &str, engine: ViewEngine)

Auto Trait Implementations§

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.
Source§

impl<T> Erased for T