Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead committed May 17, 2024
1 parent 9c92e41 commit 71a1256
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ fjall = "0.8"
http-body-util = "0.1"
hyper = { version = "1", features = ["full"] }
hyper-util = { version = "0.1", features = ["full"] }
path-tree = "0.7.7"
scru128 = { version = "3", features = ["serde"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
69 changes: 39 additions & 30 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::error::Error;
use std::str::FromStr;

use tokio::io::AsyncWriteExt;
use tokio::net::UnixListener;
Expand All @@ -17,46 +17,55 @@ use hyper::service::service_fn;
use hyper::{Method, Request, Response, StatusCode};
use hyper_util::rt::TokioIo;

use path_tree::PathTree;

use crate::store::Store;

type BoxError = Box<dyn std::error::Error + Send + Sync>;
type HTTPResult = Result<Response<BoxBody<Bytes, BoxError>>, BoxError>;

enum Routes {
Root,
CasGet,
CasGet(ssri::Integrity),
NotFound,
}

async fn get(store: Store, req: Request<hyper::body::Incoming>) -> HTTPResult {
let mut tree = PathTree::new();
let _ = tree.insert("/", Routes::Root);
let _ = tree.insert("/cas/:hash+", Routes::CasGet);

eprintln!("path: {:?}", req.uri().path());

match tree.find(req.uri().path()) {
Some((h, p)) => match h {
Routes::Root => {
let rx = store.subscribe().await;
let stream = ReceiverStream::new(rx);
let stream = stream.map(|frame| {
eprintln!("streaming");
let mut encoded = serde_json::to_vec(&frame).unwrap();
encoded.push(b'\n');
Ok(hyper::body::Frame::data(bytes::Bytes::from(encoded)))
});
let body = StreamBody::new(stream).boxed();
Ok(Response::new(body))
fn match_route(path: &str) -> Routes {
match path {
"/" => Routes::Root,
p if p.starts_with("/cas/") => {
if let Some(hash) = p.strip_prefix("/cas/") {
if let Ok(integrity) = ssri::Integrity::from_str(hash) {
return Routes::CasGet(integrity);
}
}

Routes::CasGet => Ok(Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(full("let's go"))?),
},
None => response_404(),
Routes::NotFound
}
_ => Routes::NotFound,
}
}

async fn get(store: Store, req: Request<hyper::body::Incoming>) -> HTTPResult {
eprintln!("path: {:?}", req.uri().path());
match match_route(req.uri().path()) {
Routes::Root => {
let rx = store.subscribe().await;
let stream = ReceiverStream::new(rx);
let stream = stream.map(|frame| {
eprintln!("streaming");
let mut encoded = serde_json::to_vec(&frame).unwrap();
encoded.push(b'\n');
Ok(hyper::body::Frame::data(bytes::Bytes::from(encoded)))
});
let body = StreamBody::new(stream).boxed();
Ok(Response::new(body))
}

Routes::CasGet(hash) => Ok(Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(full(format!("let's go: {:?}", &hash)))?),

Routes::NotFound => response_404(),
}
}

Expand Down

0 comments on commit 71a1256

Please sign in to comment.