Last active
May 25, 2022 21:54
-
-
Save guyo13/7393696043716f7cfec66be2b43840e6 to your computer and use it in GitHub Desktop.
rtlambda example echo server
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
use rtlambda::prelude::*; | |
use serde::Serialize; | |
#[macro_use] | |
extern crate rtlambda; | |
// Create a struct representing the lambda's response. | |
#[derive(Serialize, Clone)] | |
struct EchoMessage { | |
msg: String, | |
req_id: String, | |
} | |
// Define output and error types for berevity. | |
type OUT = EchoMessage; | |
type ERR = String; | |
// Implement an initialization function. | |
fn initialize() -> Result< | |
Box<dyn Fn(Option<&str>, RefLambdaContext<LambdaRuntimeEnv, UreqResponse>) -> Result<OUT, ERR>>, | |
ERR, | |
> { | |
// One-time initialization of reusable resources goes here | |
// | |
return Ok(Box::new(move |event, context| { | |
// Get the aws request id | |
let req_id = context.aws_request_id().unwrap(); | |
// Unwrap the event string | |
let event = match event { | |
Some(v) => v, | |
None => { | |
return Err(format!( | |
"AWS should not permit empty events. Something strange must've happened." | |
)) | |
} | |
}; | |
if event == "\"\"" { | |
return Err(format!("Empty input, nothing to echo.")); | |
} | |
// Echo the event back as a string. | |
Ok(EchoMessage { | |
msg: format!("ECHO: {}", event), | |
req_id: req_id.to_string(), | |
}) | |
})); | |
} | |
fn main() { | |
// Create a runtime instance and run its loop. | |
let mut runtime = default_runtime!(OUT, ERR, LAMBDA_VER, initialize); | |
runtime.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment