refactor server a bit
This commit is contained in:
1 parent
e4cfa53bce
commit
d5e7de86cb
4 files changed
+69
-7
No files matched your search
Generated
+4
-2
@@ -1437,6 +1437,7 @@ dependencies = [
|
|||||||
"base64ct",
|
"base64ct",
|
||||||
"dirs 7.0.0",
|
"dirs 7.0.0",
|
||||||
"duckdb",
|
"duckdb",
|
||||||
|
"form_urlencoded",
|
||||||
"getrandom 0.4.3",
|
"getrandom 0.4.3",
|
||||||
"gpui-kit",
|
"gpui-kit",
|
||||||
"gpui-omarchy",
|
"gpui-omarchy",
|
||||||
@@ -1450,6 +1451,7 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
"smol",
|
"smol",
|
||||||
"smol-hyper",
|
"smol-hyper",
|
||||||
|
"strum 0.28.0",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"testresult",
|
"testresult",
|
||||||
"thiserror 2.0.21",
|
"thiserror 2.0.21",
|
||||||
@@ -2131,9 +2133,9 @@ checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "form_urlencoded"
|
name = "form_urlencoded"
|
||||||
version = "1.2.2"
|
version = "1.2.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
|
checksum = "afa489199eaffb1211795df1c6236ce1ad0d41a6fa45250add73e3b02420f06e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ argon2 = { version = "0.6.0", features = ["parallel", "zeroize"] }
|
|||||||
base64ct = "1.8.3"
|
base64ct = "1.8.3"
|
||||||
dirs = "7.0.0"
|
dirs = "7.0.0"
|
||||||
duckdb = { version = "1.20000.1", features = ["bundled", "uuid"] }
|
duckdb = { version = "1.20000.1", features = ["bundled", "uuid"] }
|
||||||
|
form_urlencoded = "1.2.3"
|
||||||
getrandom = { version = "0.4.3", features = ["sys_rng"] }
|
getrandom = { version = "0.4.3", features = ["sys_rng"] }
|
||||||
gpui-kit = "0.7.0"
|
gpui-kit = "0.7.0"
|
||||||
gpui-omarchy = "0.2.1"
|
gpui-omarchy = "0.2.1"
|
||||||
@@ -26,6 +27,7 @@ serde = "1.0.229"
|
|||||||
serde_json = "1.0.154"
|
serde_json = "1.0.154"
|
||||||
smol = "2.0.2"
|
smol = "2.0.2"
|
||||||
smol-hyper = "0.1.1"
|
smol-hyper = "0.1.1"
|
||||||
|
strum = { version = "0.28.0", features = ["derive"] }
|
||||||
testresult = "0.4.2"
|
testresult = "0.4.2"
|
||||||
thiserror = "2.0.21"
|
thiserror = "2.0.21"
|
||||||
uuid = { version = "1.29.3", features = ["v4", "serde"] }
|
uuid = { version = "1.29.3", features = ["v4", "serde"] }
|
||||||
|
|||||||
+2
-1
@@ -1,7 +1,8 @@
|
|||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
use strum::IntoStaticStr;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug, IntoStaticStr)]
|
||||||
pub enum CreddyError {
|
pub enum CreddyError {
|
||||||
#[error("Couldn't determine location for app state")]
|
#[error("Couldn't determine location for app state")]
|
||||||
DirNotFound,
|
DirNotFound,
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
|
use crate::credentials::{AwsCredential, AwsSessionCredential};
|
||||||
use crate::error::CreddyError;
|
use crate::error::CreddyError;
|
||||||
use gpui_kit::AsyncApp;
|
use gpui_kit::AsyncApp;
|
||||||
use http_body_util::Full;
|
use http_body_util::Full;
|
||||||
use hyper::body::{Bytes, Incoming};
|
use hyper::body::{Bytes, Incoming};
|
||||||
|
use hyper::http::Uri;
|
||||||
use hyper::server::conn::http1;
|
use hyper::server::conn::http1;
|
||||||
use hyper::service::service_fn;
|
use hyper::service::service_fn;
|
||||||
use hyper::{Request, Response, StatusCode};
|
use hyper::{Method, Request, Response, StatusCode};
|
||||||
use rustix::net::sockopt::socket_peercred;
|
use rustix::net::sockopt::socket_peercred;
|
||||||
use rustix::process::Pid;
|
use rustix::process::Pid;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use smol::Timer;
|
use smol::Timer;
|
||||||
use smol::net::unix::{UnixListener, UnixStream};
|
use smol::net::unix::{UnixListener, UnixStream};
|
||||||
use smol_hyper::rt::FuturesIo;
|
use smol_hyper::rt::FuturesIo;
|
||||||
@@ -16,6 +19,30 @@ use std::io;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::time::Duration;
|
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> {
|
pub fn start_creddy_server(path: &Path, app: AsyncApp) -> Result<(), CreddyError> {
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
fs::remove_file(path)?;
|
fs::remove_file(path)?;
|
||||||
@@ -62,11 +89,41 @@ async fn handle(
|
|||||||
pid: Pid,
|
pid: Pid,
|
||||||
app: AsyncApp,
|
app: AsyncApp,
|
||||||
) -> Result<Response<Full<Bytes>>, Infallible> {
|
) -> 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>> {
|
fn handle_aws_get(uri: Uri) -> Response<Full<Bytes>> {
|
||||||
let mut response = Response::new(Full::new(Bytes::from(body)));
|
// 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.status_mut() = status;
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user