From d5e7de86cbb3271ecf60734e8efa113afa6a43f2 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Mon, 23 Nov 2026 10:55:24 -0500 Subject: [PATCH] refactor server a bit --- Cargo.lock | 6 ++-- Cargo.toml | 2 ++ src/error.rs | 3 +- src/srv/creddy_server.rs | 65 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 301c3ec..499baaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1437,6 +1437,7 @@ dependencies = [ "base64ct", "dirs 7.0.0", "duckdb", + "form_urlencoded", "getrandom 0.4.3", "gpui-kit", "gpui-omarchy", @@ -1450,6 +1451,7 @@ dependencies = [ "serde_json", "smol", "smol-hyper", + "strum 0.28.0", "tempfile", "testresult", "thiserror 2.0.21", @@ -2131,9 +2133,9 @@ checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" [[package]] name = "form_urlencoded" -version = "1.2.2" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +checksum = "afa489199eaffb1211795df1c6236ce1ad0d41a6fa45250add73e3b02420f06e" dependencies = [ "percent-encoding", ] diff --git a/Cargo.toml b/Cargo.toml index e7fe22e..653e710 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ argon2 = { version = "0.6.0", features = ["parallel", "zeroize"] } base64ct = "1.8.3" dirs = "7.0.0" duckdb = { version = "1.20000.1", features = ["bundled", "uuid"] } +form_urlencoded = "1.2.3" getrandom = { version = "0.4.3", features = ["sys_rng"] } gpui-kit = "0.7.0" gpui-omarchy = "0.2.1" @@ -26,6 +27,7 @@ serde = "1.0.229" serde_json = "1.0.154" smol = "2.0.2" smol-hyper = "0.1.1" +strum = { version = "0.28.0", features = ["derive"] } testresult = "0.4.2" thiserror = "2.0.21" uuid = { version = "1.29.3", features = ["v4", "serde"] } diff --git a/src/error.rs b/src/error.rs index 03fc6a2..9b6623f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,8 @@ use std::ffi::OsString; +use strum::IntoStaticStr; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, IntoStaticStr)] pub enum CreddyError { #[error("Couldn't determine location for app state")] DirNotFound, diff --git a/src/srv/creddy_server.rs b/src/srv/creddy_server.rs index b3ad366..97faef3 100644 --- a/src/srv/creddy_server.rs +++ b/src/srv/creddy_server.rs @@ -1,12 +1,15 @@ +use crate::credentials::{AwsCredential, AwsSessionCredential}; use crate::error::CreddyError; use gpui_kit::AsyncApp; use http_body_util::Full; use hyper::body::{Bytes, Incoming}; +use hyper::http::Uri; use hyper::server::conn::http1; use hyper::service::service_fn; -use hyper::{Request, Response, StatusCode}; +use hyper::{Method, Request, Response, StatusCode}; use rustix::net::sockopt::socket_peercred; use rustix::process::Pid; +use serde::{Deserialize, Serialize}; use smol::Timer; use smol::net::unix::{UnixListener, UnixStream}; use smol_hyper::rt::FuturesIo; @@ -16,6 +19,30 @@ use std::io; use std::path::Path; use std::time::Duration; +pub enum CredentialRequest { + AwsGet { + name: Option, + session: bool, + } +} + + +#[derive(Deserialize, Serialize)] +pub struct ErrorResponse { + code: &'static str, + message: String, +} + +impl From for ErrorResponse { + fn from(e: CreddyError) -> ErrorResponse { + let message = e.to_string(); + ErrorResponse { + code: e.into(), + message, + } + } +} + pub fn start_creddy_server(path: &Path, app: AsyncApp) -> Result<(), CreddyError> { if path.exists() { fs::remove_file(path)?; @@ -62,11 +89,41 @@ async fn handle( pid: Pid, app: AsyncApp, ) -> Result>, Infallible> { - Ok(Response::new(Full::new(Bytes::from("hello world")))) + let (parts, _body) = req.into_parts(); + let response = match (parts.method, parts.uri.path()) { + (Method::GET, "/ping") => text(StatusCode::OK, "pong"), + (Method::GET, "/aws") => handle_aws_get(parts.uri), + (_, p) => text(StatusCode::NOT_FOUND, &format!("Not found: {p}")), + }; + + Ok(response) } -fn text(status: StatusCode, body: &'static str) -> Response> { - let mut response = Response::new(Full::new(Bytes::from(body))); +fn handle_aws_get(uri: Uri) -> Response> { + // let params: std::collections::HashMap<&str, &str> = form_urlencoded::parse(uri.query().unwrap_or("").as_bytes()).collect(); + let qs = uri.query().unwrap_or("").as_bytes(); + let params: std::collections::HashMap<_, _> = form_urlencoded::parse(qs).collect(); + let req = CredentialRequest::AwsGet{ + name: params.get("name").map(|n| n.to_string()), + session: params.get("session").map(|s| s == "true").unwrap_or(false), + }; + + // todo: actually get credentials from the rest of the app + + let creds = AwsCredential { + id: uuid::uuid!("b448d315-fe82-41a9-9070-e0977e8dfa0c"), + name: "test".into(), + access_key_id: "AKIAWHATEVER".into(), + secret_access_key: "VERYSECRETSECUREVALUE".into(), + is_default: true, + }; + + let body = serde_json::to_vec(&creds).unwrap(); + Response::new(Full::new(Bytes::from(body))) +} + +fn text(status: StatusCode, body: &str) -> Response> { + let mut response = Response::new(Full::new(Bytes::from(body.to_owned()))); *response.status_mut() = status; response }