MCPcopy Create free account
hub / github.com/OpenFunction/samples / echo

Function echo

functions/knative/wasmedge/http-server/server/src/main.rs:10–34  ·  view source on GitHub ↗

This is our service handler. It receives a Request, routes on its path, and returns a Future of a Response.

(req: Request<Body>)

Source from the content-addressed store, hash-verified

8/// This is our service handler. It receives a Request, routes on its
9/// path, and returns a Future of a Response.
10async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
11 match (req.method(), req.uri().path()) {
12 // Serve some instructions at /
13 (&Method::GET, "/") => Ok(Response::new(Body::from(
14 "Try POSTing data to /echo such as: `curl localhost:8080/echo -XPOST -d 'hello world'`",
15 ))),
16
17 // Simply echo the body back to the client.
18 (&Method::POST, "/echo") => Ok(Response::new(req.into_body())),
19
20 (&Method::POST, "/echo/reversed") => {
21 let whole_body = hyper::body::to_bytes(req.into_body()).await?;
22
23 let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>();
24 Ok(Response::new(Body::from(reversed_body)))
25 }
26
27 // Return the 404 Not Found for other routes.
28 _ => {
29 let mut not_found = Response::default();
30 *not_found.status_mut() = StatusCode::NOT_FOUND;
31 Ok(not_found)
32 }
33 }
34}
35
36#[tokio::main(flavor = "current_thread")]
37async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected