#![expect(
clippy::expect_used,
reason = "example/test/bench: panic-on-error and print-for-output are the standard patterns for demos and harnesses"
)]
use rama::http::layer::trace::TraceLayer;
use rama::http::service::web::WebService;
use rama::http::service::web::response::{IntoResponse, OctetStream};
use rama::stream::io::ReaderStream;
use rama::{Layer, http::server::HttpServer};
#[tokio::main]
async fn main() {
HttpServer::default()
.listen(
"127.0.0.1:62056",
TraceLayer::new_for_http().layer(
WebService::default()
.with_get("/data", serve_binary_data)
.with_get("/download", serve_download),
),
)
.await
.expect("failed to run service");
}
async fn serve_binary_data() -> impl IntoResponse {
let data = b"Hello";
let cursor = std::io::Cursor::new(data);
let stream = ReaderStream::new(cursor);
OctetStream::new(stream)
}
async fn serve_download() -> impl IntoResponse {
let data = b"Binary file content";
let cursor = std::io::Cursor::new(data);
let stream = ReaderStream::new(cursor);
OctetStream::new(stream).with_file_name("file.bin".to_owned())
}