pub struct Server<T: Clone + Send + 'static> {
pub max_content_length: usize,
pub endpoints: Vec<EndPoint<T>>,
pub static_endpoints: HashMap<String, String>,
pub public_var: Option<T>,
/* private fields */
}Fields§
§max_content_length: usize§endpoints: Vec<EndPoint<T>>§static_endpoints: HashMap<String, String>§public_var: Option<T>Implementations§
Source§impl<T: Clone + Send + 'static> Server<T>
impl<T: Clone + Send + 'static> Server<T>
Sourcepub fn new(
max_content_length: Option<usize>,
public_var: Option<T>,
) -> Server<T>
pub fn new( max_content_length: Option<usize>, public_var: Option<T>, ) -> Server<T>
max_content_length is the max length of the request in bytes.
For example if the max is set to 1024 but the request is 1 000 000 it will close it straight away.
Sourcepub fn use_middleware(
&mut self,
handle: fn(url: &Url, req: &Request, res: &mut Response, public_var: &Option<T>) -> bool,
)
pub fn use_middleware( &mut self, handle: fn(url: &Url, req: &Request, res: &mut Response, public_var: &Option<T>) -> bool, )
Add function as middleware (just before sending response). The response is a bool. If it’s true, the request will continue; if it’s false, it will stop.
Sourcepub fn new_static(
&mut self,
path: &str,
folder: &str,
) -> Result<(), HttpServerError>
pub fn new_static( &mut self, path: &str, folder: &str, ) -> Result<(), HttpServerError>
Creates a new static url For example a folder named “images” on path /images every image in that folder will be exposed like “/images/example.png”
Sourcepub fn get(
&mut self,
path: &str,
handle: fn(req: Request, res: Response, public_var: Option<T>),
) -> Result<(), HttpServerError>
pub fn get( &mut self, path: &str, handle: fn(req: Request, res: Response, public_var: Option<T>), ) -> Result<(), HttpServerError>
Creates a new GET endpoint
Sourcepub fn post(
&mut self,
path: &str,
handle: fn(req: Request, res: Response, public_var: Option<T>),
) -> Result<(), HttpServerError>
pub fn post( &mut self, path: &str, handle: fn(req: Request, res: Response, public_var: Option<T>), ) -> Result<(), HttpServerError>
Creates a new POST endpoint
Sourcepub fn put(
&mut self,
path: &str,
handle: fn(req: Request, res: Response, public_var: Option<T>),
) -> Result<(), HttpServerError>
pub fn put( &mut self, path: &str, handle: fn(req: Request, res: Response, public_var: Option<T>), ) -> Result<(), HttpServerError>
Creates a new PUT endpoint
Sourcepub fn delete(
&mut self,
path: &str,
handle: fn(req: Request, res: Response, public_var: Option<T>),
) -> Result<(), HttpServerError>
pub fn delete( &mut self, path: &str, handle: fn(req: Request, res: Response, public_var: Option<T>), ) -> Result<(), HttpServerError>
Creates a new DELETE endpoint
Sourcepub fn listen(
&mut self,
port: u32,
address: Option<&str>,
threads: Option<usize>,
on_complete: fn(),
) -> Result<(), HttpServerError>
pub fn listen( &mut self, port: u32, address: Option<&str>, threads: Option<usize>, on_complete: fn(), ) -> Result<(), HttpServerError>
Starts listening on the given port. If no provided threads will use cpu threads as value. The higher the value the higher the cpu usage. The on_complete function is executed after the listener has started.