|
| 1 | +use axum::{ |
| 2 | + body::Body, |
| 3 | + http, |
| 4 | + http::{ |
| 5 | + header::{CACHE_CONTROL, CONTENT_TYPE}, |
| 6 | + StatusCode, |
| 7 | + }, |
| 8 | + response::{IntoResponse, Response}, |
| 9 | + routing::get, |
| 10 | + Router, |
| 11 | +}; |
| 12 | +use bytes::Bytes; |
| 13 | +use lambda_http::{lambda_runtime, tracing, Error, StreamAdapter}; |
| 14 | +use std::{convert::Infallible, time::Duration}; |
| 15 | +use thiserror::Error; |
| 16 | +use tokio::sync::mpsc; |
| 17 | +use tokio_stream::wrappers::ReceiverStream; |
| 18 | + |
| 19 | +#[derive(Debug, Error)] |
| 20 | +pub enum AppError { |
| 21 | + #[error("{0}")] |
| 22 | + Http(#[from] http::Error), |
| 23 | +} |
| 24 | + |
| 25 | +impl IntoResponse for AppError { |
| 26 | + fn into_response(self) -> Response { |
| 27 | + (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +type AppResult<T = Response> = Result<T, AppError>; |
| 32 | + |
| 33 | +async fn stream_handler() -> AppResult { |
| 34 | + let (tx, rx) = mpsc::channel::<Result<Bytes, Infallible>>(8); |
| 35 | + let body = Body::from_stream(ReceiverStream::new(rx)); |
| 36 | + |
| 37 | + tokio::spawn(async move { |
| 38 | + for msg in ["Hello", "world", "from", "Lambda!"] { |
| 39 | + tokio::time::sleep(Duration::from_millis(500)).await; |
| 40 | + if tx.send(Ok(Bytes::from(format!("{msg}\n")))).await.is_err() { |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + Ok(Response::builder() |
| 47 | + .status(StatusCode::OK) |
| 48 | + .header(CONTENT_TYPE, "text/plain; charset=utf-8") |
| 49 | + .header(CACHE_CONTROL, "no-cache") |
| 50 | + .body(body)?) |
| 51 | +} |
| 52 | + |
| 53 | +#[tokio::main] |
| 54 | +async fn main() -> Result<(), Error> { |
| 55 | + tracing::init_default_subscriber(); |
| 56 | + |
| 57 | + let app = Router::new().route("/", get(stream_handler)); |
| 58 | + |
| 59 | + let runtime = lambda_runtime::Runtime::new(StreamAdapter::from(app)); |
| 60 | + |
| 61 | + runtime.run().await |
| 62 | +} |
0 commit comments