From 10a90a353a3bb46652a42b0e01834fd158fd600d Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 31 Oct 2022 15:14:36 -0700 Subject: PDS: early progress --- adenosine-pds/src/bin/adenosine-pds.rs | 90 ++++++++++++++++++++++++++++++++++ adenosine-pds/src/lib.rs | 42 ++++++++++++++++ adenosine-pds/src/main.rs | 3 -- 3 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 adenosine-pds/src/bin/adenosine-pds.rs create mode 100644 adenosine-pds/src/lib.rs delete mode 100644 adenosine-pds/src/main.rs (limited to 'adenosine-pds/src') diff --git a/adenosine-pds/src/bin/adenosine-pds.rs b/adenosine-pds/src/bin/adenosine-pds.rs new file mode 100644 index 0000000..3a00fac --- /dev/null +++ b/adenosine-pds/src/bin/adenosine-pds.rs @@ -0,0 +1,90 @@ +use adenosine_pds::*; +use anyhow::Result; + +use log::{self, debug}; +use structopt::StructOpt; + + +#[derive(StructOpt)] +#[structopt( + rename_all = "kebab-case", + about = "personal digital server (PDS) implementation for AT protocol (atproto.com)" +)] +struct Opt { + // TODO: different path type for structopt? + + /// File path of sqlite database for storing IPLD blocks (aka, repository content) + #[structopt( + parse(from_os_str), + global = true, + long = "--block-db", + env = "ATP_BLOCK_DB", + default_value = "adenosine_pds_blockstore.sqlite" + )] + blockstore_db_path: std::path::PathBuf, + + /// File path of sqlite database for ATP service (user accounts, indices, etc) + #[structopt( + parse(from_os_str), + global = true, + long = "--atp-db", + env = "ATP_ATP_DB", + default_value = "adenosine_pds_atp.sqlite" + )] + atp_db_path: std::path::PathBuf, + + /// Log more messages. Pass multiple times for ever more verbosity + #[structopt(global = true, long, short = "v", parse(from_occurrences))] + verbose: i8, + + #[structopt(subcommand)] + cmd: Command, +} + +#[derive(StructOpt)] +enum Command { + /// Start ATP server as a foreground process + Serve, + + /// Import a CAR file (TODO) + Import, + + /// Dump info from databases (TODO) + Inspect, +} + +#[tokio::main] +async fn main() -> Result<()> { + let opt = Opt::from_args(); + + let log_level = match opt.verbose { + std::i8::MIN..=-1 => "none", + 0 => "warn", + 1 => "info", + 2 => "debug", + 3..=std::i8::MAX => "trace", + }; + // hyper logging is very verbose, so crank that down even if everything else is more verbose + let cli_filter = format!("{},hyper=error", log_level); + // defer to env var config, fallback to CLI settings + let log_filter = std::env::var("RUST_LOG").unwrap_or(cli_filter); + pretty_env_logger::formatted_timed_builder() + .parse_filters(&log_filter) + .init(); + + debug!("config parsed, starting up"); + + match opt.cmd { + Command::Serve {} => { + // TODO: log some config stuff? + run_server().await + }, + Command::Import {} => { + unimplemented!() + }, + Command::Inspect {} => { + unimplemented!() + }, + } +} + diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs new file mode 100644 index 0000000..bb34c80 --- /dev/null +++ b/adenosine-pds/src/lib.rs @@ -0,0 +1,42 @@ + +use anyhow::Result; +use log::{self, debug}; +use warp::Filter; +use warp::reply::Response; +use std::collections::HashMap; + +pub async fn run_server() -> Result<()> { + + // GET / + let homepage = warp::path::end().map(|| "Not much to see here yet!"); + + // 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(()) +} + +// TODO: tokio::task::spawn_blocking diff --git a/adenosine-pds/src/main.rs b/adenosine-pds/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/adenosine-pds/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} -- cgit v1.2.3