Library provides HTTP response streaming support for axum web framework:
- JSON array stream format
- JSON lines stream format
- CSV stream
- Protobuf len-prefixed stream format
- Text stream
This type of responses are useful when you are reading huge stream of objects from some source (such as database, file, etc) and want to avoid huge memory allocation.
Cargo.toml:
[dependencies]
axum-streams = { version = "0.10", features=["json", "csv", "protobuf", "text"] }
axum | axum-streams |
---|---|
0.6 | v0.9-v0.10 |
0.5 | 0.7 |
Example code:
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyTestStructure {
some_test_field: String
}
fn my_source_stream() -> impl Stream<Item=MyTestStructure> {
// Simulating a stream with a plain vector and throttling to show how it works
stream::iter(vec![
MyTestStructure {
some_test_field: "test1".to_string()
}; 1000
]).throttle(std::time::Duration::from_millis(50))
}
async fn test_json_array_stream() -> impl IntoResponse {
StreamBodyAs::json_array(source_test_stream())
}
async fn test_json_nl_stream() -> impl IntoResponse {
StreamBodyAs::json_nl(source_test_stream())
}
async fn test_csv_stream() -> impl IntoResponse {
StreamBodyAs::csv(source_test_stream())
}
async fn test_text_stream() -> impl IntoResponse {
StreamBodyAs::text(source_test_stream())
}
All examples available at examples directory.
To run example use:
# cargo run --example json-example --features json
There is the same functionality for:
Apache Software License (ASL)
Abdulla Abdurakhmanov