Skip to content

Instantly share code, notes, and snippets.

@rtyler
Created September 6, 2025 01:02
Show Gist options
  • Save rtyler/d75923b31863ca9b0b77767d111d61c4 to your computer and use it in GitHub Desktop.
Save rtyler/d75923b31863ca9b0b77767d111d61c4 to your computer and use it in GitHub Desktop.
use std::time::Duration;
use tokio_stream::StreamExt as _ ;
use axum::body::Bytes;
use futures::{stream, StreamExt};
use axum::{body::Body, routing::get, Router};
use std::net::SocketAddr;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let app = Router::new().route("/{*path}", get(hello_world));
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let listener = TcpListener::bind(addr).await?;
axum::serve(listener, app.into_make_service()).await?;
Ok(())
}
async fn hello_world() -> axum::body::Body {
let song = vec![
"This is the song",
"that never",
"ends.",
"",
"It just goes",
"on and on",
"my friends.",
"",
"Some people",
"Started singing it",
"not knowing",
"what it was.",
"And they'll continue",
"Singing it",
"forever just because..",
"",
];
let stream = stream::repeat_with(move || {
stream::iter(song.clone().into_iter().map(|lyric| Bytes::from(format!("{lyric}\n"))).map(std::io::Result::Ok))
.throttle(Duration::from_secs(1))
})
.throttle(Duration::from_secs(4))
.flatten();
Body::from_stream(stream)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment