use std::sync::Arc;
use std::time::Duration;
use davidrs::http::stream::{self, Cors, StreamApi, StreamRequest, StreamResponse};
use davidrs::http::{Failure, PlainErrors, Public, StatusCode};
use davidrs::{Context, RuntimeError};
async fn count(
_app: Arc<()>,
request: StreamRequest,
context: Context<()>,
) -> Result<StreamResponse, Failure> {
if !request.wants_events() {
return Ok(stream::json(
StatusCode::OK,
&serde_json::json!({"count": 3}),
));
}
let deadline = context.deadline().with_margin(Duration::from_secs(1));
Ok(stream::events(deadline, |events| async move {
for n in 1..=3 {
let step = n.to_string();
let frame = stream::sse_frame(Some(&step), Some("count"), &step);
if events.send(frame).await.is_err() {
return;
}
}
}))
}
#[tokio::main]
async fn main() -> Result<(), RuntimeError> {
let _telemetry = davidrs::telemetry::init("stream-http-example")?;
StreamApi::new("count", Public, PlainErrors)
.cors(Cors::new(vec!["http://localhost:3000".to_owned()]))
.run(Arc::new(()), count)
.await
}