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 --- Cargo.lock | 334 +++++++++++++++++++-------------- adenosine-pds/Cargo.toml | 2 +- adenosine-pds/src/bin/adenosine-pds.rs | 5 +- adenosine-pds/src/lib.rs | 65 +++---- 4 files changed, 225 insertions(+), 181 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8043d4b..e944657 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,13 +42,13 @@ dependencies = [ "libipld", "log", "pretty_env_logger", + "rouille", "rusqlite", "schemafy", "serde_json", "sha256", "structopt", "tokio", - "warp", ] [[package]] @@ -121,6 +121,15 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "ansi_term" version = "0.12.1" @@ -148,6 +157,12 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "async-trait" version = "0.1.58" @@ -469,6 +484,24 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" +dependencies = [ + "iana-time-zone", + "num-integer", + "num-traits", + "winapi", +] + +[[package]] +name = "chunked_transfer" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fff857943da45f546682664a79488be82e69e43c1a7a2307679ab9afb3a66d2e" + [[package]] name = "cid" version = "0.8.6" @@ -574,6 +607,16 @@ dependencies = [ "objc", ] +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "color_quant" version = "1.1.0" @@ -754,6 +797,50 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" +[[package]] +name = "cxx" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b7d4e43b25d3c994662706a1d4fcfc32aaa6afd287502c111b237093bb23f3a" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84f8829ddc213e2c1368e51a2564c552b65a8cb6a28f31e576270ac81d5e5827" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e72537424b474af1460806647c41d4b6d35d09ef7fe031c5c2fa5766047cc56a" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "309e4fb93eed90e1e14bea0da16b209f81813ba9fc7830c20ed151dd7bc0a4d7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "darling" version = "0.13.4" @@ -836,6 +923,16 @@ dependencies = [ "byteorder", ] +[[package]] +name = "deflate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f" +dependencies = [ + "adler32", + "gzip-header", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -1451,6 +1548,15 @@ dependencies = [ "syn", ] +[[package]] +name = "gzip-header" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95cc527b92e6029a62960ad99aa8a6660faa4555fe5f731aab13aa6a921795a2" +dependencies = [ + "crc32fast", +] + [[package]] name = "h2" version = "0.3.15" @@ -1494,31 +1600,6 @@ dependencies = [ "hashbrown 0.11.2", ] -[[package]] -name = "headers" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" -dependencies = [ - "base64", - "bitflags", - "bytes", - "headers-core", - "http", - "httpdate", - "mime", - "sha1", -] - -[[package]] -name = "headers-core" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" -dependencies = [ - "http", -] - [[package]] name = "heck" version = "0.3.3" @@ -1655,6 +1736,30 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "ico" version = "0.1.0" @@ -2069,6 +2174,15 @@ dependencies = [ "safemem", ] +[[package]] +name = "link-cplusplus" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369" +dependencies = [ + "cc", +] + [[package]] name = "lock_api" version = "0.4.9" @@ -2843,26 +2957,6 @@ dependencies = [ "siphasher", ] -[[package]] -name = "pin-project" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "pin-project-lite" version = "0.2.9" @@ -2902,7 +2996,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0b0cabbbd20c2d7f06dbf015e06aad59b6ca3d9ed14848783e98af9aaf19925" dependencies = [ "bitflags", - "deflate", + "deflate 0.7.20", "inflate", "num-iter", ] @@ -3276,6 +3370,31 @@ dependencies = [ "windows 0.37.0", ] +[[package]] +name = "rouille" +version = "3.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f86e4c51a773f953f02bbab5fd049f004bfd384341d62da2a079aff812ab176" +dependencies = [ + "base64", + "brotli", + "chrono", + "deflate 1.0.0", + "filetime", + "multipart", + "num_cpus", + "percent-encoding", + "rand 0.8.5", + "serde", + "serde_derive", + "serde_json", + "sha1", + "threadpool", + "time", + "tiny_http", + "url", +] + [[package]] name = "rusqlite" version = "0.26.3" @@ -3309,15 +3428,6 @@ dependencies = [ "semver 1.0.14", ] -[[package]] -name = "rustls-pemfile" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eebeaeb360c87bfb72e84abdb3447159c0eaececf1bef2aecd65a8be949d1c9" -dependencies = [ - "base64", -] - [[package]] name = "rustversion" version = "1.0.9" @@ -3410,6 +3520,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" + [[package]] name = "security-framework" version = "2.7.0" @@ -3588,17 +3704,6 @@ dependencies = [ "stable_deref_trait", ] -[[package]] -name = "sha-1" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest 0.10.5", -] - [[package]] name = "sha1" version = "0.10.5" @@ -4211,6 +4316,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "threadpool" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +dependencies = [ + "num_cpus", +] + [[package]] name = "time" version = "0.3.16" @@ -4240,6 +4354,18 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny_http" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82" +dependencies = [ + "ascii", + "chunked_transfer", + "httpdate", + "log", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -4296,29 +4422,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-stream" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d660770404473ccd7bc9f8b28494a811bc18542b915c0855c51e8f419d5223ce" -dependencies = [ - "futures-core", - "pin-project-lite", - "tokio", -] - -[[package]] -name = "tokio-tungstenite" -version = "0.17.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f714dd15bead90401d77e04243611caec13726c2408afd5b31901dfcdcb3b181" -dependencies = [ - "futures-util", - "log", - "tokio", - "tungstenite", -] - [[package]] name = "tokio-util" version = "0.7.4" @@ -4355,7 +4458,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if", - "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -4426,25 +4528,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" -[[package]] -name = "tungstenite" -version = "0.17.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e27992fd6a8c29ee7eef28fc78349aa244134e10ad447ce3b9f0ac0ed0fa4ce0" -dependencies = [ - "base64", - "byteorder", - "bytes", - "http", - "httparse", - "log", - "rand 0.8.5", - "sha-1", - "thiserror", - "url", - "utf-8", -] - [[package]] name = "twoway" version = "0.1.8" @@ -4620,37 +4703,6 @@ dependencies = [ "try-lock", ] -[[package]] -name = "warp" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed7b8be92646fc3d18b06147664ebc5f48d222686cb11a8755e561a735aacc6d" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "headers", - "http", - "hyper", - "log", - "mime", - "mime_guess", - "multipart", - "percent-encoding", - "pin-project", - "rustls-pemfile", - "scoped-tls", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-stream", - "tokio-tungstenite", - "tokio-util", - "tower-service", - "tracing", -] - [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/adenosine-pds/Cargo.toml b/adenosine-pds/Cargo.toml index f08d999..7f6e84a 100644 --- a/adenosine-pds/Cargo.toml +++ b/adenosine-pds/Cargo.toml @@ -23,7 +23,7 @@ ipfs-sqlite-block-store = "*" rusqlite = { version = "*", features = ["bundled"] } jsonschema = "*" schemafy = "*" -warp = "*" +rouille = "*" iroh-car = { version = "0.1.0-vendored.0", path = "../iroh-car" } tokio = { version = "1", features = ["full"] } futures = "0.3" 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