pub fn start_web_server() {
    use dev_bestia_simple_server::*;
    log::info!("cargo_crev_reviews server started");
    /// nested_function: convert response structs
    fn convert_response(response_with_bytes: ResponseWithBytes, builder: Builder) -> Response<Vec<u8>> {
        let builder = match response_with_bytes.status_code {
            Status::NotFound => builder.status(StatusCode::NOT_FOUND),
            Status::Ok => builder.status(StatusCode::OK),
        };
        let builder = builder.header(http::header::CONTENT_TYPE, response_with_bytes.mime_type.as_bytes());
        let builder = match response_with_bytes.cache_control {
            Some(cache_control) => builder.header(http::header::CACHE_CONTROL, cache_control.as_bytes()),
            None => builder,
        };
        let response = unwrap!(builder.body(response_with_bytes.body));
        // return
        response
    }

    let server = Server::new(|request, response_builder| {
        let path = request.uri().to_string();
        // log::info!("Request received. {} {}", request.method(), request.uri());
        if !request.uri().to_string().starts_with(&format!("/{}", SERVER_FIRST_SUBDIRECTORY.as_str())) {
            let response = convert_response(response_404_not_found(&path), response_builder);
            return Ok(response);
        }
        match request.method() {
            &Method::GET => {
                // GET is used only to request files
                let response_with_bytes = parse_get_uri_and_response_file(&path);
                let response = convert_response(response_with_bytes, response_builder);
                Ok(response)
            }
            &Method::POST => {
                let request_body: &Vec<u8> = request.body();
                let response_body = parse_post_data_and_match_method(request_body);
                match response_body {
                    Ok(response_body) => Ok(response_builder.body(response_body.into_bytes())?),
                    Err(err) => {
                        let response_body = unwrap!(response_err_message(&err));
                        Ok(response_builder.body(response_body.into_bytes())?)
                    }
                }
            }
            _ => {
                let response_body = unwrap!(response_modal_message("Unknown request method!"));
                Ok(response_builder.body(response_body.into_bytes())?)
            }
        }
    });

    // background sync data
    sync_in_background_reviews();
    sync_in_background_yanked();
    sync_in_background_verify();

    server.listen(SERVER_HOST.as_str(), SERVER_PORT.as_str());
}
/// start the simple web server and match the GET or POST method
/// this is the only place where the web server is explicitly stated.
/// Changing this simple function, you can use any other web server library of your choice easily.
start the simple web server and match the GET or POST method
this is the only place where the web server is explicitly stated.
Changing this simple function, you can use any other web server library of your choice easily.
start the simple web server and match the GET or POST method this is the only place where the web server is explicitly stated . Changing this simple function you can use any other web server library of your choice easily .
pub fn start_web_server() {
    use dev_bestia_simple_server::*;
    log::info!("cargo_crev_reviews server started");
    /// nested_function: convert response structs
    fn convert_response(response_with_bytes: ResponseWithBytes, builder: Builder) -> Response<Vec<u8>> {
        let builder = match response_with_bytes.status_code {
            Status::NotFound => builder.status(StatusCode::NOT_FOUND),
            Status::Ok => builder.status(StatusCode::OK),
        };
        let builder = builder.header(http::header::CONTENT_TYPE, response_with_bytes.mime_type.as_bytes());
        let builder = match response_with_bytes.cache_control {
            Some(cache_control) => builder.header(http::header::CACHE_CONTROL, cache_control.as_bytes()),
            None => builder,
        };
        let response = unwrap!(builder.body(response_with_bytes.body));
        // return
        response
    }
    let server = Server::new(|request, response_builder| {
        let path = request.uri().to_string();
        // log::info!("Request received. {} {}", request.method(), request.uri());
        if !request.uri().to_string().starts_with(&format!("/{}", SERVER_FIRST_SUBDIRECTORY.as_str())) {
            let response = convert_response(response_404_not_found(&path), response_builder);
            return Ok(response);
        }
        match request.method() {
            &Method::GET => {
                // GET is used only to request files
                let response_with_bytes = parse_get_uri_and_response_file(&path);
                let response = convert_response(response_with_bytes, response_builder);
                Ok(response)
            }
            &Method::POST => {
                let request_body: &Vec<u8> = request.body();
                let response_body = parse_post_data_and_match_method(request_body);
                match response_body {
                    Ok(response_body) => Ok(response_builder.body(response_body.into_bytes())?),
                    Err(err) => {
                        let response_body = unwrap!(response_err_message(&err));
                        Ok(response_builder.body(response_body.into_bytes())?)
                    }
                }
            }
            _ => {
                let response_body = unwrap!(response_modal_message("Unknown request method!"));
                Ok(response_builder.body(response_body.into_bytes())?)
            }
        }
    });
    // background sync data
    sync_in_background_reviews();
    sync_in_background_yanked();
    sync_in_background_verify();
    server.listen(SERVER_HOST.as_str(), SERVER_PORT.as_str());
}
pub fn start_web_server ( ) { use dev_bestia_simple_server :: * ; log :: info ! ( " " ) ; /// nested_function: convert response structs fn convert_response ( response_with_bytes : ResponseWithBytes , builder : Builder ) -> Response < Vec < u8 > > { let builder = match response_with_bytes . status_code { Status :: NotFound => builder . status ( StatusCode :: NOT_FOUND ) , Status :: Ok => builder . status ( StatusCode :: OK ) , } ; let builder = builder . header ( http :: header :: CONTENT_TYPE , response_with_bytes . mime_type . as_bytes ( ) ) ; let builder = match response_with_bytes . cache_control { Some ( cache_control ) => builder . header ( http :: header :: CACHE_CONTROL , cache_control . as_bytes ( ) ) , None => builder , } ; let response = unwrap ! ( builder body ( response_with_bytes body ) ) ; // return response } let server = Server :: new ( | request , response_builder | { let path = request . uri ( ) . to_string ( ) ; // log::info!("Request received. {} {}", request.method(), request.uri()); if ! request . uri ( ) . to_string ( ) . starts_with ( & format ! ( " " SERVER_FIRST_SUBDIRECTORY as_str ( ) ) ) { let response = convert_response ( response_404_not_found ( & path ) , response_builder ) ; return Ok ( response ) ; } match request . method ( ) { & Method :: GET => { // GET is used only to request files let response_with_bytes = parse_get_uri_and_response_file ( & path ) ; let response = convert_response ( response_with_bytes , response_builder ) ; Ok ( response ) } & Method :: POST => { let request_body : & Vec < u8 > = request . body ( ) ; let response_body = parse_post_data_and_match_method ( request_body ) ; match response_body { Ok ( response_body ) => Ok ( response_builder . body ( response_body . into_bytes ( ) ) ? ) , Err ( err ) => { let response_body = unwrap ! ( response_err_message ( err ) ) ; Ok ( response_builder . body ( response_body . into_bytes ( ) ) ? ) } } } _ => { let response_body = unwrap ! ( response_modal_message ( " " ) ) ; Ok ( response_builder . body ( response_body . into_bytes ( ) ) ? ) } } } ) ; // background sync data sync_in_background_reviews ( ) ; sync_in_background_yanked ( ) ; sync_in_background_verify ( ) ; server . listen ( SERVER_HOST . as_str ( ) , SERVER_PORT . as_str ( ) ) ; }
start the simple web server and match the GET or POST method
this is the only place where the web server is explicitly stated.
start the simple web server and match the GET or POST method this is the only place where the web server is explicitly stated .