Created
July 15, 2019 15:08
-
-
Save autodidaddict/996ee9168b8fa029bd272d6678cfa9cc to your computer and use it in GitHub Desktop.
Waxosuit Echo Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate wascap_guest as guest; | |
use serde::Serialize; | |
use guest::prelude::*; | |
use std::collections::HashMap; | |
call_handler!(handle_call); | |
pub fn handle_call(ctx: &CapabilitiesContext, cmd: &Command) -> Result<Event> { | |
match cmd.payload { | |
Some(ref p) => match p.type_url.as_ref() { | |
http::TYPE_URL_HTTP_REQUEST => hello_world(ctx, p.value.as_slice()), | |
core::TYPE_URL_HEALTH_REQUEST => Ok(Event::success()), | |
_ => Ok(Event::bad_dispatch(&p.type_url)), | |
}, | |
None => Ok(http::Response::bad_request().as_event(true, None)), | |
} | |
} | |
fn hello_world( | |
_ctx: &CapabilitiesContext, | |
payload: impl Into<http::Request>) -> Result<Event> { | |
let payload = payload.into(); | |
let echo = EchoRequest { | |
method: payload.method, | |
path: payload.path, | |
query_string: payload.query_string, | |
headers: payload.header, | |
body: payload.body | |
}; | |
Ok(http::Response::json(echo, 200, "OK").as_event(true, None)) | |
} | |
#[derive(Serialize)] | |
struct EchoRequest { | |
method: String, | |
path: String, | |
query_string: String, | |
headers: HashMap<String, String>, | |
body: Vec<u8>, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment