refactor server a bit
This commit is contained in:
1 parent
e4cfa53bce
commit
d5e7de86cb
4 files changed
+69
-7
No files matched your search
+2
-1
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user