Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Add support for queries over HTTP #12

Merged
merged 7 commits into from
Jan 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add proper routing and method matching
  • Loading branch information
MarinPostma committed Jan 10, 2023
commit 67518c16c69b01dcd8f028f4f201caf63c212b0e
15 changes: 13 additions & 2 deletions server/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bytes::{BufMut, Bytes, BytesMut};
use hyper::body::to_bytes;
use hyper::server::conn::AddrStream;
use hyper::service::make_service_fn;
use hyper::{Body, Request, Response};
use hyper::{Body, Method, Request, Response};
use serde::Deserialize;
use serde_json::Number;
use tokio::sync::{mpsc, oneshot};
Expand Down Expand Up @@ -55,13 +55,14 @@ fn query_response_to_json(rows: QueryResponse) -> anyhow::Result<Bytes> {
Ok(buffer.into_inner().freeze())
}

async fn handle_request(
async fn handle_query(
mut req: Request<Body>,
sender: mpsc::Sender<(oneshot::Sender<Result<QueryResponse, BoxError>>, Query)>,
) -> anyhow::Result<Response<Body>> {
let bytes = to_bytes(req.body_mut()).await?;
let req: HttpQueryRequest = serde_json::from_slice(&bytes)?;
let (s, resp) = oneshot::channel();
// TODO: send query batch instead
let _ = sender
.send((s, Query::SimpleQuery(req.statements.join(";"), Vec::new())))
.await;
Expand All @@ -76,6 +77,16 @@ async fn handle_request(
}
}

async fn handle_request(
req: Request<Body>,
sender: mpsc::Sender<(oneshot::Sender<Result<QueryResponse, BoxError>>, Query)>,
) -> anyhow::Result<Response<Body>> {
match (req.method(), req.uri().path()) {
(&Method::POST, "/") => handle_query(req, sender).await,
_ => Ok(Response::builder().status(404).body(Body::empty()).unwrap()),
}
}

pub async fn run_http<F>(addr: SocketAddr, db_factory: F) -> anyhow::Result<()>
where
F: MakeService<(), Query> + Send + 'static,
Expand Down