pub struct HttpServer { /* private fields */ }
Expand description
The server instance that represents and controls the underlying http-service.
Implementations§
Source§impl HttpServer
impl HttpServer
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new server instance using default configurations and server settings.
Sourcepub fn new_with_config(config: ServerConfig) -> Self
pub fn new_with_config(config: ServerConfig) -> Self
Create a new server instance with supplied configuration and settings.
Sourcepub fn listen(&mut self, port: u16)
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);
Sourcepub fn listen_and_serve(
&mut self,
port: u16,
callback: Option<fn(AsyncController)>,
)
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);
});
Sourcepub fn get_courier(&self) -> AsyncController
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.
Sourcepub fn drop_session_auto_clean(&mut self)
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.
Sourcepub fn config(&mut self) -> &mut ServerConfig
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.
Sourcepub fn config_hot_reload(&self)
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
impl Default for HttpServer
Source§impl Router for HttpServer
impl Router for HttpServer
Source§fn use_static(&mut self, path: PathBuf) -> &mut dyn Router
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"));
}
fn get( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn patch( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn post( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn put( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn delete( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn options( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn other( &mut self, method: &str, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn all( &mut self, uri: RequestPath, callback: fn(&Box<Request>, &mut Box<Response>), ) -> &mut dyn Router
fn use_custom_static( &mut self, uri: RequestPath, path: PathBuf, ) -> &mut dyn Router
fn case_sensitive(&mut self, allow_case: bool, method: Option<REST>)
Source§impl ServerDef for HttpServer
impl ServerDef for HttpServer
Source§fn def_router(&mut self, router: Route)
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)
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)
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)
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>)
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)
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)
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)
fn disable_session_auto_clean(&mut self)
If using the session
feature, this API will turn off the periodic session store clean up