Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 59 additions & 48 deletions kinode/packages/app_store/app_store/src/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ pub fn handle_http_request(
Ok(())
}

fn get_package_id(url_params: &HashMap<String, String>) -> anyhow::Result<PackageId> {
let Some(package_id) = url_params.get("id") else {
return Err(anyhow::anyhow!("Missing id"));
};

let id = package_id.parse::<PackageId>()?;
Ok(id)
}

fn gen_package_info(
id: &PackageId,
listing: Option<&PackageListing>,
Expand Down Expand Up @@ -94,23 +103,10 @@ fn serve_paths(
requested_packages: &mut HashMap<PackageId, RequestedPackage>,
req: &IncomingHttpRequest,
) -> anyhow::Result<(StatusCode, Option<HashMap<String, String>>, Vec<u8>)> {
let path = req.path()?;
let method = req.method()?;

// TODO get rid of this workaround when we change `IncomingHttpRequest`
let bound_path: &str = if path.ends_with("auto-update") {
"/apps/:id/auto-update"
} else if path.ends_with("mirror") {
"/apps/:id/mirror"
} else if path.ends_with("caps") {
"/apps/:id/caps"
} else if path.starts_with("/apps/listed/") {
"/apps/listed/:id"
} else if &path == "/apps/listed" || &path == "/apps" {
&path
} else {
"/apps/:id"
};
let bound_path: &str = req.bound_path(Some(&our.process.to_string()));
let url_params = req.url_params();

// print_to_terminal(0, &format!("HTTP {method} {path} {bound_path}"));

Expand All @@ -121,7 +117,7 @@ fn serve_paths(
return Ok((
StatusCode::METHOD_NOT_ALLOWED,
None,
format!("Invalid method {method} for {path}").into_bytes(),
format!("Invalid method {method} for {bound_path}").into_bytes(),
));
}
let all: Vec<serde_json::Value> = state
Expand All @@ -140,7 +136,7 @@ fn serve_paths(
return Ok((
StatusCode::METHOD_NOT_ALLOWED,
None,
format!("Invalid method {method} for {path}").into_bytes(),
format!("Invalid method {method} for {bound_path}").into_bytes(),
));
}
let all: Vec<serde_json::Value> = state
Expand All @@ -159,11 +155,14 @@ fn serve_paths(
// update a downloaded app: PUT
// uninstall/delete a downloaded app: DELETE
"/apps/:id" => {
let package_id = path
.split("/")
.last()
.unwrap_or_default()
.parse::<PackageId>()?;
let Ok(package_id) = get_package_id(url_params) else {
return Ok((
StatusCode::BAD_REQUEST,
None,
format!("Missing id").into_bytes(),
));
};

match method {
Method::GET => {
let Some(pkg) = state.downloaded_packages.get(&package_id) else {
Expand Down Expand Up @@ -234,18 +233,21 @@ fn serve_paths(
_ => Ok((
StatusCode::METHOD_NOT_ALLOWED,
None,
format!("Invalid method {method} for {path}").into_bytes(),
format!("Invalid method {method} for {bound_path}").into_bytes(),
)),
}
}
// GET detail about a specific listed app
// download a listed app: POST
"/apps/listed/:id" => {
let package_id = path
.split("/")
.last()
.unwrap_or_default()
.parse::<PackageId>()?;
let Ok(package_id) = get_package_id(url_params) else {
return Ok((
StatusCode::BAD_REQUEST,
None,
format!("Missing id").into_bytes(),
));
};

match method {
Method::GET => {
let Some(listing) = state.get_listing(&package_id) else {
Expand Down Expand Up @@ -318,18 +320,21 @@ fn serve_paths(
_ => Ok((
StatusCode::METHOD_NOT_ALLOWED,
None,
format!("Invalid method {method} for {path}").into_bytes(),
format!("Invalid method {method} for {bound_path}").into_bytes(),
)),
}
}
// GET caps for a specific downloaded app
// approve capabilities for a downloaded app: POST
"/apps/:id/caps" => {
let package_id = path
.split("/")
.nth(2)
.unwrap_or_default()
.parse::<PackageId>()?;
let Ok(package_id) = get_package_id(url_params) else {
return Ok((
StatusCode::BAD_REQUEST,
None,
format!("Missing id").into_bytes(),
));
};

match method {
// return the capabilities for that app
Method::GET => Ok(match crate::fetch_package_manifest(&package_id) {
Expand All @@ -356,18 +361,21 @@ fn serve_paths(
_ => Ok((
StatusCode::METHOD_NOT_ALLOWED,
None,
format!("Invalid method {method} for {path}").into_bytes(),
format!("Invalid method {method} for {bound_path}").into_bytes(),
)),
}
}
// start mirroring a downloaded app: PUT
// stop mirroring a downloaded app: DELETE
"/apps/:id/mirror" => {
let package_id = path
.split("/")
.nth(2)
.unwrap_or_default()
.parse::<PackageId>()?;
let Ok(package_id) = get_package_id(url_params) else {
return Ok((
StatusCode::BAD_REQUEST,
None,
format!("Missing id").into_bytes(),
));
};

match method {
// start mirroring an app
Method::PUT => {
Expand All @@ -382,18 +390,21 @@ fn serve_paths(
_ => Ok((
StatusCode::METHOD_NOT_ALLOWED,
None,
format!("Invalid method {method} for {path}").into_bytes(),
format!("Invalid method {method} for {bound_path}").into_bytes(),
)),
}
}
// start auto-updating a downloaded app: PUT
// stop auto-updating a downloaded app: DELETE
"/apps/:id/auto-update" => {
let package_id = path
.split("/")
.nth(2)
.unwrap_or_default()
.parse::<PackageId>()?;
let Ok(package_id) = get_package_id(url_params) else {
return Ok((
StatusCode::BAD_REQUEST,
None,
format!("Missing id").into_bytes(),
));
};

match method {
// start auto-updating an app
Method::PUT => {
Expand All @@ -408,14 +419,14 @@ fn serve_paths(
_ => Ok((
StatusCode::METHOD_NOT_ALLOWED,
None,
format!("Invalid method {method} for {path}").into_bytes(),
format!("Invalid method {method} for {bound_path}").into_bytes(),
)),
}
}
_ => Ok((
StatusCode::NOT_FOUND,
None,
format!("Path not found: {}", path).into_bytes(),
format!("Path not found: {bound_path}").into_bytes(),
)),
}
}
2 changes: 1 addition & 1 deletion kinode/packages/chess/chess/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
anyhow = "1.0"
base64 = "0.13"
bincode = "1.3.3"
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "12bf9ee" }
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "3232423" }
pleco = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
5 changes: 3 additions & 2 deletions kinode/packages/chess/chess/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn handle_request(our: &Address, message: &Message, state: &mut ChessState) -> a
}
}
}
http::HttpServerRequest::WebSocketOpen { path, channel_id } => {
http::HttpServerRequest::WebSocketOpen { channel_id, .. } => {
// We know this is authenticated and unencrypted because we only
// bound one path, the root path. So we know that client
// frontend opened a websocket and can send updates
Expand Down Expand Up @@ -419,7 +419,7 @@ fn handle_http_request(
state: &mut ChessState,
http_request: &http::IncomingHttpRequest,
) -> anyhow::Result<()> {
if http_request.path()? != "/games" {
if http_request.bound_path(Some(&our.process.to_string())) != "/games" {
http::send_response(
http::StatusCode::NOT_FOUND,
None,
Expand Down Expand Up @@ -454,6 +454,7 @@ fn handle_http_request(
vec![],
));
};

if let Some(game) = state.games.get(game_id)
&& !game.ended
{
Expand Down