From b80360a2cfe87b4e4dd24f06010642976b858f26 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 31 Oct 2022 16:26:42 -0700 Subject: pds: switch to blocking web framework --- adenosine-pds/src/bin/adenosine-pds.rs | 5 ++- adenosine-pds/src/lib.rs | 65 +++++++++++++++------------------- 2 files changed, 31 insertions(+), 39 deletions(-) (limited to 'adenosine-pds/src') diff --git a/adenosine-pds/src/bin/adenosine-pds.rs b/adenosine-pds/src/bin/adenosine-pds.rs index 3a00fac..5d0e638 100644 --- a/adenosine-pds/src/bin/adenosine-pds.rs +++ b/adenosine-pds/src/bin/adenosine-pds.rs @@ -53,8 +53,7 @@ enum Command { Inspect, } -#[tokio::main] -async fn main() -> Result<()> { +fn main() -> Result<()> { let opt = Opt::from_args(); let log_level = match opt.verbose { @@ -77,7 +76,7 @@ async fn main() -> Result<()> { match opt.cmd { Command::Serve {} => { // TODO: log some config stuff? - run_server().await + run_server() }, Command::Import {} => { unimplemented!() diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs index bb34c80..f321d3f 100644 --- a/adenosine-pds/src/lib.rs +++ b/adenosine-pds/src/lib.rs @@ -1,42 +1,35 @@ use anyhow::Result; -use log::{self, debug}; -use warp::Filter; -use warp::reply::Response; -use std::collections::HashMap; +use log::{info, error}; +use rouille::{Request, Response, router}; -pub async fn run_server() -> Result<()> { +pub fn run_server() -> Result<()> { - // GET / - let homepage = warp::path::end().map(|| "Not much to see here yet!"); + // TODO: log access requests + // TODO: some static files? https://github.com/tomaka/rouille/blob/master/examples/static-files.rs - // GET /xrpc/some.method w/ query params - let xrpc_some_get = warp::get() - .and(warp::path!("xrpc" / "some.method")) - .and(warp::query::>()) - .map(|query_params: HashMap| { - println!("query params: {:?}", query_params); - // return query params as a JSON map object - warp::reply::json(&query_params) - }); - - // POST /xrpc/other.method w/ query params - let xrpc_other_post = warp::post() - .and(warp::path!("xrpc" / "other.method")) - .and(warp::query::>()) - .and(warp::body::json()) - .map(|query_params: HashMap, body_val: serde_json::Value| { - println!("query params: {:?}", query_params); - println!("body JSON: {}", body_val); - // echo it back - warp::reply::json(&body_val) - }); - - let routes = homepage.or(xrpc_some_get).or(xrpc_other_post).with(warp::log("adenosine-pds")); - warp::serve(routes) - .run(([127, 0, 0, 1], 3030)) - .await; - Ok(()) + let log_ok = |req: &Request, resp: &Response, elap: std::time::Duration| { + info!("{} {} ({:?})", req.method(), req.raw_url(), elap); + }; + let log_err = |req: &Request, elap: std::time::Duration| { + error!("HTTP handler panicked: {} {} ({:?})", req.method(), req.raw_url(), elap); + }; + rouille::start_server("localhost:3030", move |request| { + rouille::log_custom(request, log_ok, log_err, || { + router!(request, + (GET) ["/"] => { + Response::text("Not much to see here yet!") + }, + (GET) ["/xrpc/some.method"] => { + Response::text("didn't get a thing") + // TODO: reply with query params as a JSON body + }, + (POST) ["/xrpc/other.method"] => { + Response::text("didn't get other thing") + // TODO: parse and echo back JSON body + }, + _ => rouille::Response::empty_404() + ) + }) + }); } - -// TODO: tokio::task::spawn_blocking -- cgit v1.2.3