refactor server a bit

This commit is contained in:
jfmonty2 committed 2026-11-23 10:55:24 -05:00
1 parent e4cfa53bce
commit d5e7de86cb
4 files changed
+69 -7

No files matched your search

Generated
+4 -2
View File
@@ -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",
]
+2
View File
@@ -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"] }
+2 -1
View File
@@ -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,
+61 -4
View File
@@ -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<String>,
session: bool,
}
}
#[derive(Deserialize, Serialize)]
pub struct ErrorResponse {
code: &'static str,
message: String,
}
impl From<CreddyError> 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<Response<Full<Bytes>>, 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<Full<Bytes>> {
let mut response = Response::new(Full::new(Bytes::from(body)));
fn handle_aws_get(uri: Uri) -> Response<Full<Bytes>> {
// 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<Full<Bytes>> {
let mut response = Response::new(Full::new(Bytes::from(body.to_owned())));
*response.status_mut() = status;
response
}