Covertness/coap-rs

Provide Routing tool

jamesmunns opened this issue · 5 comments

It would be nice to have a tool to handle request routing. One good example is Iron's router functionality. This prevents every user from having to reimplement this functionality.

See: https://github.com/iron/router

@Covertness I plan to implement this next, after the Copper Pull Request is closed.

It sounds good! Many HTTP libraries have it.

@jamesmunns Any news about your work? I am currently routing using the following structure:

fn request_handler(req: CoAPRequest) -> Option<CoAPResponse> {
    println!("Receive request: {:?}", req);

    let uri = get_uri(&req);

    if let Some(mut response) = req.response {

        // Default payload
        let mut code = Responses::NotFound;
        let mut resp = "not found".to_owned().into_bytes();

        match uri.as_ref() {
            "status" => {
                code = Responses::Content;
                resp = "available".to_owned().into_bytes();
            },
            "json_status" => {
                code = Responses::Content;
                resp = "{\"status\": \"available\"}".to_owned().into_bytes();
            },
            _ => {

            }
        }

        // Customize response payload
        response.message.header.code = MessageClass::ResponseType(code);
        response.message.payload = resp;

        // Return the modified response
        return Some(response);
    }

    // Return the auto-generated response
    req.response
}

But a router would be nice!

closed. please let me know if anyone want the similar feature. i will implement it.