Skip to content

Commit

Permalink
wip: cont on http interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead committed Jun 3, 2024
1 parent 1290b34 commit 9e98acb
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashMap;
use std::error::Error;
use std::net::SocketAddr;

use scru128::Scru128Id;

use serde::{Deserialize, Serialize};

use tokio::io::AsyncWriteExt;
Expand All @@ -17,7 +19,7 @@ use hyper::StatusCode;
use hyper_util::rt::TokioIo;

use crate::listener::Listener;
use crate::store::Store;
use crate::store::{ReadOptions, Store};

#[derive(Serialize, Deserialize, Debug)]
pub struct Request {
Expand All @@ -40,6 +42,7 @@ pub struct Request {

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct Response {
pub request_id: Scru128Id,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -108,6 +111,33 @@ async fn handle(
)
.await;

async fn wait_for_response(store: &Store, frame_id: Scru128Id) -> Option<Response> {
let mut recver = store
.read(ReadOptions {
follow: true,
last_id: Some(frame_id),
})
.await;

while let Some(event_frame) = recver.recv().await {
if event_frame.topic == "http.response" {
if let Some(meta) = event_frame.meta {
if let Ok(res) = serde_json::from_value::<Response>(meta) {
if res.request_id == frame_id {
return Some(res);
}
}
}
}
}

None
}

let response = wait_for_response(&store, frame.id).await;

eprintln!("RESPONSE {:?}", response);

Ok(hyper::Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
Expand Down

0 comments on commit 9e98acb

Please sign in to comment.