Skip to content

Commit

Permalink
Add stub endpoint for deposits events
Browse files Browse the repository at this point in the history
  • Loading branch information
NCrashed committed May 5, 2022
1 parent 7903e0b commit 4102e24
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 28 deletions.
2 changes: 1 addition & 1 deletion hexstody-btc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
uuid = { version = "0.8.2", features = ["v4"]}
rocket = { version = "0.5.0-rc.1", default-features = false, features = ["json"] }
rocket_okapi = { git = "https://github.com/GREsau/okapi", branch = "master", features = ["swagger"]}
rocket_okapi = { git = "https://github.com/GREsau/okapi", branch = "master", features = ["swagger", "rapidoc"]}

[dev-dependencies]
maplit = "1.0.2"
3 changes: 2 additions & 1 deletion hexstody-btc/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod public;
pub mod public;
pub mod types;
72 changes: 57 additions & 15 deletions hexstody-btc/src/api/public.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
use rocket::get;
use rocket::response::content;
use rocket_okapi::{openapi, openapi_get_routes, swagger_ui::*};
use std::error::Error;
use super::types::*;
use rocket::figment::{providers::Env, Figment};
use rocket::{get, serde::json::Json, Config};
use rocket_okapi::settings::UrlObject;
use rocket_okapi::{openapi, openapi_get_routes, rapidoc::*, swagger_ui::*};
use rocket::fairing::AdHoc;
use std::net::IpAddr;
use tokio::sync::Notify;
use std::sync::Arc;
use tokio::sync::{Mutex, Notify};

#[openapi(tag = "ping")]
#[openapi(tag = "misc")]
#[get("/ping")]
fn json() -> content::Json<()> {
content::Json(())
fn ping() -> Json<()> {
Json(())
}

pub async fn serve_public_api() -> () {
rocket::build()
.mount("/", openapi_get_routes![json])
#[openapi(tag = "events")]
#[get("/deposit")]
async fn deposit() -> Json<DepositEvents> {
Json(DepositEvents { events: vec![] })
}

pub async fn serve_public_api(address: IpAddr, port: u16, start_notify: Arc<Notify>) -> Result<(), rocket::Error> {
let figment = Figment::from(Config {
address,
port,
..Config::default()
})
.merge(Env::prefixed("HEXSTODY_BTC_").global());

let on_ready = AdHoc::on_liftoff("API Start!", |_| Box::pin(async move {
start_notify.notify_one();
}));

rocket::custom(figment)
.mount("/", openapi_get_routes![ping, deposit])
.mount(
"/swagger/",
make_swagger_ui(&SwaggerUIConfig {
url: "../openapi.json".to_owned(),
..Default::default()
}),
)
.mount(
"/rapidoc/",
make_rapidoc(&RapiDocConfig {
general: GeneralConfig {
spec_urls: vec![UrlObject::new("General", "../openapi.json")],
..Default::default()
},
hide_show: HideShowConfig {
allow_spec_url_load: false,
allow_spec_file_load: false,
..Default::default()
},
..Default::default()
}),
)
.attach(on_ready)
.launch()
.await;
.await?;
Ok(())
}

#[cfg(test)]
Expand All @@ -34,7 +71,7 @@ mod tests {
use hexstody_btc_client::client::BtcClient;
use std::panic::AssertUnwindSafe;

const SERVICE_TEST_PORT: u16 = 8000;
const SERVICE_TEST_PORT: u16 = 8289;
const SERVICE_TEST_HOST: &str = "127.0.0.1";

async fn run_api_test<F, Fut>(test_body: F)
Expand All @@ -43,23 +80,28 @@ mod tests {
Fut: Future<Output = ()>,
{
let _ = env_logger::builder().is_test(true).try_init();
let start_notify = Arc::new(Notify::new());

let (sender, receiver) = tokio::sync::oneshot::channel();
tokio::spawn({
let start_notify = start_notify.clone();
async move {
let serve_task = serve_public_api();
let serve_task =
serve_public_api(SERVICE_TEST_HOST.parse().unwrap(), SERVICE_TEST_PORT, start_notify);
futures::pin_mut!(serve_task);
futures::future::select(serve_task, receiver.map_err(drop)).await;
}
});

start_notify.notified().await;

let res = AssertUnwindSafe(test_body()).catch_unwind().await;

sender.send(()).unwrap();

assert!(res.is_ok());
}

#[tokio::test]
async fn test_public_api_ping() {
run_api_test(|| async {
let client = BtcClient::new(&format!(
Expand Down
19 changes: 19 additions & 0 deletions hexstody-btc/src/api/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
use rocket_okapi::okapi::schemars;
use rocket_okapi::okapi::schemars::JsonSchema;

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct DepositEvents {
pub events: Vec<DepositEvent>,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct DepositEvent {
pub address: String,
/// Sats amount
pub amount: u64,
/// 0 means unconfirmed
pub confirmations: u64,
/// UNIX timestamp when the event occured
pub timestamp: i64,
}
27 changes: 18 additions & 9 deletions hexstody-btc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use clap::Parser;
use futures::future::{AbortHandle, Abortable, Aborted};
use log::*;
use std::error::Error;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, Notify};
use tokio::sync::Notify;
use tokio::time::sleep;

use api::public::*;
Expand All @@ -21,7 +22,17 @@ struct Args {
#[derive(Parser, Debug, Clone)]
enum SubCommand {
/// Start listening incoming API requests
Serve
Serve {
#[clap(long, short, default_value = "8180", env = "HEXSTODY_BTC_API_PORT")]
port: u16,
#[clap(
long,
short,
default_value = "127.0.0.1",
env = "HEXSTODY_BTC_API_ADDRESS"
)]
address: IpAddr,
},
}

#[tokio::main]
Expand All @@ -30,15 +41,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

match args.subcmd.clone() {
SubCommand::Serve => loop {
let args = args.clone();
SubCommand::Serve { address, port } => loop {
let (_abort_api_handle, abort_api_reg) = AbortHandle::new_pair();

info!("Serving API");

let public_api_fut = tokio::spawn(serve_public_api());
let start_notify = Arc::new(Notify::new());
let public_api_fut = tokio::spawn(serve_public_api(address, port, start_notify));
match Abortable::new(public_api_fut, abort_api_reg).await {
Ok(mres) => (),
Ok(_) => (),
Err(Aborted) => {
error!("API thread aborted")
}
Expand All @@ -47,7 +57,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
let restart_dt = Duration::from_secs(5);
info!("Adding {:?} delay before restarting logic", restart_dt);
sleep(restart_dt).await;
}
},
}
Ok(())
}
4 changes: 2 additions & 2 deletions nix/pkgs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
import ((import <nixpkgs> {}).fetchFromGitHub {
owner = "NixOS";
repo = "nixpkgs";
rev = "7ec99ea7cf9616ef4c6e835710202623fcb846e7";
sha256 = "1cp4sb4v1qzb268h2ky7039lf1gwkrs757q6gv2wd2hs65kvf1q7";
rev = "21dcccd97d28520d5d22fb545bcdcc72b2cffcd2";
sha256 = "0h9bwbsvkiqsrkswljnac8jibflfyb1rf3gx867g60qs8nia0n1y";
})

0 comments on commit 4102e24

Please sign in to comment.