-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstreaming_api.rs
More file actions
55 lines (50 loc) · 1.5 KB
/
streaming_api.rs
File metadata and controls
55 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use rustapi_rs::prelude::*;
use std::convert::Infallible;
#[derive(Debug, Serialize, Schema)]
struct ProgressUpdate {
step: u32,
message: String,
}
async fn progress_feed(
) -> Sse<impl futures_util::Stream<Item = std::result::Result<SseEvent, Infallible>>> {
let events = vec![
Ok::<_, Infallible>(
SseEvent::json_data(&ProgressUpdate {
step: 1,
message: "queued".to_string(),
})
.expect("json data should serialize")
.event("progress")
.id("1"),
),
Ok::<_, Infallible>(
SseEvent::json_data(&ProgressUpdate {
step: 2,
message: "processing".to_string(),
})
.expect("json data should serialize")
.event("progress")
.id("2"),
),
Ok::<_, Infallible>(
SseEvent::json_data(&ProgressUpdate {
step: 3,
message: "done".to_string(),
})
.expect("json data should serialize")
.event("complete")
.id("3")
.retry(2_000),
),
];
sse_from_iter(events).keep_alive(KeepAlive::new())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
println!("Starting streaming example...");
println!(" -> GET http://127.0.0.1:3000/events");
RustApi::new()
.route("/events", get(progress_feed))
.run("127.0.0.1:3000")
.await
}