Rewrote DB webserver to use warp
This commit is contained in:
parent
cf99f5a4a1
commit
5ab4e3a221
|
@ -1,29 +1,26 @@
|
||||||
use crate::ExecutorConnection;
|
use crate::ExecutorConnection;
|
||||||
use hyper::service::{make_service_fn, service_fn};
|
|
||||||
use hyper::{Body, Request, Response, Server};
|
|
||||||
use std::convert::Infallible;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use warp::Filter;
|
||||||
|
|
||||||
pub async fn run(db: ExecutorConnection, listen: SocketAddr) -> anyhow::Result<()> {
|
pub async fn run(db: ExecutorConnection, listen: SocketAddr) -> anyhow::Result<()> {
|
||||||
let db = Arc::new(db);
|
let db_filter = warp::any().map(move || db.clone());
|
||||||
|
let filter = warp::any().and(db_filter).and_then(handle);
|
||||||
|
|
||||||
Server::bind(&listen)
|
warp::serve(filter).run(listen).await;
|
||||||
.serve(make_service_fn(|_| {
|
|
||||||
let db = Arc::clone(&db);
|
|
||||||
async move { Ok::<_, Infallible>(service_fn(move |r| handle(r, Arc::clone(&db)))) }
|
|
||||||
}))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle(
|
async fn handle(db: ExecutorConnection) -> Result<impl warp::Reply, warp::Rejection> {
|
||||||
_req: Request<Body>,
|
if let Some((a, b)) = db.get_quote(None).await {
|
||||||
db: Arc<ExecutorConnection>,
|
Ok(warp::reply::with_status(
|
||||||
) -> Result<Response<Body>, Infallible> {
|
format!("{} {}", a, b),
|
||||||
Ok(Response::new(Body::from(format!(
|
warp::http::StatusCode::OK,
|
||||||
"{:?}",
|
))
|
||||||
db.get_quote(None).await
|
} else {
|
||||||
))))
|
Ok(warp::reply::with_status(
|
||||||
|
format!("None"),
|
||||||
|
warp::http::StatusCode::NO_CONTENT,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue