[][src]Function aphrora::run_server

pub fn run_server<F>(views_handler: F) where
    F: Fn(Request) -> Response,
    F: Send + Copy + 'static, 

Run server with a closure of how to deal with the requests.

Example

Coding a closure in the run_server() function like this, and when visit any view within this port, it will display a word hello on that page.

use aphrora::http::{Request, Response, RequestMethod, ResponseStatus};
use aphrora::run_server;

fn main() {
    run_server(|request| {
        println!("view of request: {}", request.view);
        Response{
            status: ResponseStatus::OK,
            message: String::from("hello"),
        }
    });
}

Here in that example, as the code, when you visit any view, it will return a hello into your browser, you can see a line of hello in which webpage in your browser. You can also replace the hello with a string read from a example.html file, and then it will return show the file in the browser webpage.

There must be various functions in a server, so you are supposed to use something like match expression to deal with that. You can match the view property of the Request and then call some function to deal with it.