-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Renamed function to make it more clear * API update to accept Result<> * _with_errors functions, docs and example
- Loading branch information
Showing
10 changed files
with
459 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use axum::response::IntoResponse; | ||
use axum::routing::*; | ||
use axum::Router; | ||
use futures::{stream, Stream, StreamExt}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use tokio::net::TcpListener; | ||
|
||
use axum_streams::*; | ||
|
||
#[derive(Debug, Clone, Deserialize, Serialize)] | ||
struct MyTestStructure { | ||
some_test_field: String, | ||
} | ||
|
||
struct MyError { | ||
message: String, | ||
} | ||
|
||
impl Into<axum::Error> for MyError { | ||
fn into(self) -> axum::Error { | ||
axum::Error::new(self.message) | ||
} | ||
} | ||
|
||
fn source_test_stream() -> impl Stream<Item = Result<MyTestStructure, MyError>> { | ||
// Simulating a stream with a plain vector and throttling to show how it works | ||
tokio_stream::StreamExt::throttle( | ||
stream::iter(vec![ | ||
MyTestStructure { | ||
some_test_field: "test1".to_string() | ||
}; | ||
10000 | ||
]) | ||
.enumerate() | ||
.map(|(idx, item)| { | ||
if idx != 0 && idx % 10 == 0 { | ||
Err(MyError { | ||
message: format!("Error at index {}", idx), | ||
}) | ||
} else { | ||
Ok(item) | ||
} | ||
}), | ||
std::time::Duration::from_millis(500), | ||
) | ||
} | ||
|
||
async fn test_json_array_stream() -> impl IntoResponse { | ||
StreamBodyAs::json_array_with_errors(source_test_stream()) | ||
} | ||
|
||
async fn test_json_nl_stream() -> impl IntoResponse { | ||
StreamBodyAs::json_nl_with_errors(source_test_stream()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
tracing_subscriber::fmt().with_target(false).init(); | ||
|
||
// build our application with a route | ||
let app = Router::new() | ||
// `GET /` goes to `root` | ||
.route("/json-array-stream", get(test_json_array_stream)) | ||
.route("/json-nl-stream", get(test_json_nl_stream)); | ||
|
||
let listener = TcpListener::bind("127.0.0.1:8080").await?; | ||
|
||
axum::serve(listener, app).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.