Struct lambda_runtime::Runtime
source · pub struct Runtime<S> { /* private fields */ }
Expand description
Lambda runtime executing a handler function on incoming requests.
Middleware can be added to a runtime using the Runtime::layer method in order to execute logic prior to processing the incoming request and/or after the response has been sent back to the Lambda Runtime API.
§Example
use lambda_runtime::{Error, LambdaEvent, Runtime};
use serde_json::Value;
use tower::service_fn;
#[tokio::main]
async fn main() -> Result<(), Error> {
let func = service_fn(func);
Runtime::new(func).run().await?;
Ok(())
}
async fn func(event: LambdaEvent<Value>) -> Result<Value, Error> {
Ok(event.payload)
}
Implementations§
source§impl<'a, F, EventPayload, Response, BufferedResponse, StreamingResponse, StreamItem, StreamError> Runtime<RuntimeApiClientService<RuntimeApiResponseService<CatchPanicService<'a, F>, EventPayload, Response, BufferedResponse, StreamingResponse, StreamItem, StreamError>>>where
F: Service<LambdaEvent<EventPayload>, Response = Response>,
F::Future: Future<Output = Result<Response, F::Error>>,
F::Error: Into<Diagnostic> + Debug,
EventPayload: for<'de> Deserialize<'de>,
Response: IntoFunctionResponse<BufferedResponse, StreamingResponse>,
BufferedResponse: Serialize,
StreamingResponse: Stream<Item = Result<StreamItem, StreamError>> + Unpin + Send + 'static,
StreamItem: Into<Bytes> + Send,
StreamError: Into<BoxError> + Send + Debug,
impl<'a, F, EventPayload, Response, BufferedResponse, StreamingResponse, StreamItem, StreamError> Runtime<RuntimeApiClientService<RuntimeApiResponseService<CatchPanicService<'a, F>, EventPayload, Response, BufferedResponse, StreamingResponse, StreamItem, StreamError>>>where
F: Service<LambdaEvent<EventPayload>, Response = Response>,
F::Future: Future<Output = Result<Response, F::Error>>,
F::Error: Into<Diagnostic> + Debug,
EventPayload: for<'de> Deserialize<'de>,
Response: IntoFunctionResponse<BufferedResponse, StreamingResponse>,
BufferedResponse: Serialize,
StreamingResponse: Stream<Item = Result<StreamItem, StreamError>> + Unpin + Send + 'static,
StreamItem: Into<Bytes> + Send,
StreamError: Into<BoxError> + Send + Debug,
sourcepub fn new(handler: F) -> Self
pub fn new(handler: F) -> Self
Create a new runtime that executes the provided handler for incoming requests.
In order to start the runtime and poll for events on the Lambda Runtime APIs, you must call Runtime::run.
Note that manually creating a Runtime does not add tracing to the executed handler as is done by super::run. If you want to add the default tracing functionality, call Runtime::layer with a super::layers::TracingLayer.
source§impl<S> Runtime<S>
impl<S> Runtime<S>
sourcepub fn layer<L>(self, layer: L) -> Runtime<L::Service>
pub fn layer<L>(self, layer: L) -> Runtime<L::Service>
Add a new layer to this runtime. For an incoming request, this layer will be executed before any layer that has been added prior.
§Example
use lambda_runtime::{layers, Error, LambdaEvent, Runtime};
use serde_json::Value;
use tower::service_fn;
#[tokio::main]
async fn main() -> Result<(), Error> {
let runtime = Runtime::new(service_fn(echo)).layer(
layers::TracingLayer::new()
);
runtime.run().await?;
Ok(())
}
async fn echo(event: LambdaEvent<Value>) -> Result<Value, Error> {
Ok(event.payload)
}