-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a serving multiplexer to server both web and grpc requests on the…
… same port.
- Loading branch information
1 parent
32b8b4a
commit 0759875
Showing
7 changed files
with
246 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use axum::routing::get; | ||
use axum::Router; | ||
use hyper::{Body, StatusCode}; | ||
use std::sync::Arc; | ||
use tonic::Request; | ||
|
||
use crate::keyvalue::keyvalue_proto::key_value_server::KeyValue; | ||
use crate::keyvalue::keyvalue_proto::GetRequest; | ||
|
||
#[derive(Clone)] | ||
pub struct HttpHandler { | ||
service: Arc<dyn KeyValue>, | ||
} | ||
|
||
impl HttpHandler { | ||
pub fn new(service: Arc<dyn KeyValue>) -> Self { | ||
Self { service } | ||
} | ||
|
||
/** | ||
* Installs routes that allow interacting with the keyvalue store. Example queries | ||
* include /get?key=foo. | ||
*/ | ||
pub fn routes(self: Arc<Self>) -> Router { | ||
let hello = move |req: hyper::Request<Body>| async move { self.handle_get(req).await }; | ||
Router::new().route("/get", get(hello)) | ||
} | ||
|
||
fn invalid(message: String) -> hyper::Response<Body> { | ||
hyper::Response::builder() | ||
.status(StatusCode::BAD_REQUEST) | ||
.body(Body::from(format!("{}\n", message))) | ||
.unwrap() | ||
} | ||
|
||
async fn handle_get(&self, request: hyper::Request<Body>) -> hyper::Response<Body> { | ||
let query = match request.uri().query() { | ||
Some(q) => q, | ||
None => return HttpHandler::invalid("must pass query".to_string()), | ||
}; | ||
|
||
let parsed = querystring::querify(query); | ||
let key = match parsed | ||
.iter() | ||
.find(|(a, _)| a.eq(&"key")) | ||
.map(|(_, b)| b.to_string()) | ||
{ | ||
Some(value) => value, | ||
_ => return HttpHandler::invalid("must pass key parameter".to_string()), | ||
}; | ||
|
||
let request = Request::new(GetRequest { | ||
key: key.clone().as_bytes().to_vec(), | ||
version: -1, | ||
}); | ||
|
||
let output = match self.service.get(request).await { | ||
Ok(p) => { | ||
let proto = p.into_inner(); | ||
match proto.entry { | ||
Some(e) => match String::from_utf8(e.value) { | ||
Ok(value) => format!("{}={}, version={}", key, value, proto.version), | ||
_ => format!("failed to parse value as utf8 for key {}", key), | ||
}, | ||
None => format!("no value for key {}", key), | ||
} | ||
} | ||
Err(status) => format!("Failed to query keyvalue store {}", status.to_string()), | ||
}; | ||
|
||
hyper::Response::builder() | ||
.status(StatusCode::OK) | ||
.body(Body::from(output)) | ||
.unwrap() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::keyvalue::grpc::PutRequest; | ||
use crate::keyvalue::keyvalue_proto::{Entry, GetResponse, PutResponse}; | ||
use crate::root; | ||
use tonic::{Response, Status}; | ||
|
||
struct FakeKeyValue {} | ||
|
||
#[tonic::async_trait] | ||
impl KeyValue for FakeKeyValue { | ||
async fn get(&self, request: Request<GetRequest>) -> Result<Response<GetResponse>, Status> { | ||
todo!() | ||
// Ok(Response::new(GetResponse { entry: Some(Entry { | ||
// key: request.into_inner().key.clone(), | ||
// value: vec![], | ||
// }), version: 0 })) | ||
} | ||
|
||
async fn put(&self, request: Request<PutRequest>) -> Result<Response<PutResponse>, Status> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_http() { | ||
assert!(false); | ||
|
||
let kv = Arc::new(FakeKeyValue {}); | ||
let http = Arc::new(HttpHandler { | ||
service: kv.clone(), | ||
}); | ||
|
||
let web_service = Router::new() | ||
.route("/", get(root)) | ||
.nest("/keyvalue", http.routes()) | ||
.into_make_service(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.