| 39 | } |
| 40 | |
| 41 | async fn stream_words() -> Result<Response, AppError> { |
| 42 | let (tx, rx) = mpsc::channel::<Result<Bytes, Infallible>>(8); |
| 43 | let body = Body::from_stream(ReceiverStream::new(rx)); |
| 44 | |
| 45 | tokio::spawn(async move { |
| 46 | for msg in ["Hello", "world", "from", "Lambda!"] { |
| 47 | tokio::time::sleep(Duration::from_millis(500)).await; |
| 48 | if tx.send(Ok(Bytes::from(format!("{msg}\n")))).await.is_err() { |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | Ok(Response::builder() |
| 55 | .status(StatusCode::OK) |
| 56 | .header(CONTENT_TYPE, "text/plain; charset=utf-8") |
| 57 | .header(CACHE_CONTROL, "no-cache") |
| 58 | .body(body)?) |
| 59 | } |
| 60 | |
| 61 | #[tokio::main] |
| 62 | async fn main() -> Result<(), Error> { |