Skip to content

Commit e0ea5be

Browse files
authored
Merge pull request #64 from kinode-dao/dr/http-server-add-unbind
HTTP server: add unbind action
2 parents a02248c + 69d8c08 commit e0ea5be

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/http.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pub enum HttpServerAction {
9696
/// lazy_load_blob bytes and serve them as the response to any request to this path.
9797
cache: bool,
9898
},
99+
/// Unbind a previously-bound HTTP path
100+
Unbind { path: String },
99101
/// Bind a path to receive incoming WebSocket connections.
100102
/// Doesn't need a cache since does not serve assets.
101103
WebSocketBind {
@@ -113,6 +115,8 @@ pub enum HttpServerAction {
113115
encrypted: bool,
114116
extension: bool,
115117
},
118+
/// Unbind a previously-bound WebSocket path
119+
WebSocketUnbind { path: String },
116120
/// Processes will RECEIVE this kind of request when a client connects to them.
117121
/// If a process does not want this websocket open, they should issue a *request*
118122
/// containing a [`type@HttpServerAction::WebSocketClose`] message and this channel ID.
@@ -434,6 +438,27 @@ where
434438
resp
435439
}
436440

441+
pub fn unbind_http_path<T>(path: T) -> std::result::Result<(), HttpServerError>
442+
where
443+
T: Into<String>,
444+
{
445+
let res = KiRequest::to(("our", "http_server", "distro", "sys"))
446+
.body(serde_json::to_vec(&HttpServerAction::Unbind { path: path.into() }).unwrap())
447+
.send_and_await_response(5)
448+
.unwrap();
449+
let Ok(Message::Response { body, .. }) = res else {
450+
return Err(HttpServerError::PathBindError {
451+
error: "http_server timed out".to_string(),
452+
});
453+
};
454+
let Ok(resp) = serde_json::from_slice::<std::result::Result<(), HttpServerError>>(&body) else {
455+
return Err(HttpServerError::PathBindError {
456+
error: "http_server gave unexpected response".to_string(),
457+
});
458+
};
459+
resp
460+
}
461+
437462
/// Register a WebSockets path with the HTTP server. Your app must do this
438463
/// in order to receive incoming WebSocket connections.
439464
pub fn bind_ws_path<T>(
@@ -501,6 +526,27 @@ where
501526
resp
502527
}
503528

529+
pub fn unbind_ws_path<T>(path: T) -> std::result::Result<(), HttpServerError>
530+
where
531+
T: Into<String>,
532+
{
533+
let res = KiRequest::to(("our", "http_server", "distro", "sys"))
534+
.body(serde_json::to_vec(&HttpServerAction::WebSocketUnbind { path: path.into() }).unwrap())
535+
.send_and_await_response(5)
536+
.unwrap();
537+
let Ok(Message::Response { body, .. }) = res else {
538+
return Err(HttpServerError::PathBindError {
539+
error: "http_server timed out".to_string(),
540+
});
541+
};
542+
let Ok(resp) = serde_json::from_slice::<std::result::Result<(), HttpServerError>>(&body) else {
543+
return Err(HttpServerError::PathBindError {
544+
error: "http_server gave unexpected response".to_string(),
545+
});
546+
};
547+
resp
548+
}
549+
504550
/// Send an HTTP response to the incoming HTTP request.
505551
pub fn send_response(status: StatusCode, headers: Option<HashMap<String, String>>, body: Vec<u8>) {
506552
KiResponse::new()

0 commit comments

Comments
 (0)