-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve server and testing for lud06
- Loading branch information
Showing
3 changed files
with
139 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,106 @@ | ||
use axum::{http::StatusCode, routing::get, Router}; | ||
use axum::{extract::RawQuery, http::StatusCode, routing::get, Router}; | ||
use std::future::Future; | ||
|
||
#[must_use] | ||
pub fn builder<P>() -> Server<P> { | ||
Server { pay_request: None } | ||
pub struct Server<PQ, PC> { | ||
pay_request_query: PQ, | ||
pay_request_callback: PC, | ||
} | ||
|
||
pub struct Server<P> { | ||
pub pay_request: Option<P>, | ||
impl Default | ||
for Server< | ||
unimplemented::Handler0<crate::core::pay_request::PayRequest>, | ||
unimplemented::Handler1<(u64, Option<String>), crate::core::pay_request::CallbackResponse>, | ||
> | ||
{ | ||
fn default() -> Self { | ||
Server { | ||
pay_request_query: unimplemented::handler0, | ||
pay_request_callback: unimplemented::handler1, | ||
} | ||
} | ||
} | ||
|
||
impl<P, PFut> Server<P> | ||
impl<PQ, PC> Server<PQ, PC> { | ||
pub fn pay_request<PQ2, PC2>( | ||
self, | ||
pay_request_query: PQ2, | ||
pay_request_callback: PC2, | ||
) -> Server<PQ2, PC2> { | ||
Server { | ||
pay_request_query, | ||
pay_request_callback, | ||
} | ||
} | ||
} | ||
|
||
impl<PQ, PQFut, PC, PCFut> Server<PQ, PC> | ||
where | ||
P: 'static + Send + Clone + Fn() -> PFut, | ||
PFut: Send + Future<Output = Result<crate::core::pay_request::PayRequest, StatusCode>>, | ||
PQ: 'static + Send + Clone + Fn() -> PQFut, | ||
PQFut: Send + Future<Output = Result<crate::core::pay_request::PayRequest, StatusCode>>, | ||
PC: 'static + Send + Clone + Fn((u64, Option<String>)) -> PCFut, | ||
PCFut: Send + Future<Output = Result<crate::core::pay_request::CallbackResponse, StatusCode>>, | ||
{ | ||
pub fn build(self) -> Router<()> { | ||
let mut router = Router::new(); | ||
|
||
if let Some(p) = self.pay_request { | ||
router = router.route( | ||
Router::new() | ||
.route( | ||
"/lnurlp", | ||
get(move || { | ||
let p = p.clone(); | ||
async move { p().await.map(|a| a.to_string()) } | ||
let pq = self.pay_request_query.clone(); | ||
async move { pq().await.map(|a| a.to_string()) } | ||
}), | ||
) | ||
.route( | ||
"/lnurlp/callback", | ||
get(move |RawQuery(q): RawQuery| { | ||
let pc = self.pay_request_callback.clone(); | ||
async move { | ||
let q = q.ok_or(StatusCode::BAD_REQUEST)?; | ||
let qs = q | ||
.split('&') | ||
.filter_map(|s| s.split_once('=')) | ||
.collect::<std::collections::BTreeMap<_, _>>(); | ||
|
||
let amount = qs | ||
.get("amount") | ||
.and_then(|s| s.parse().ok()) | ||
.ok_or(StatusCode::BAD_REQUEST)?; | ||
|
||
let comment = qs.get("comment").map(|c| String::from(*c)); | ||
|
||
pc((amount, comment)).await.map(|a| a.to_string()) | ||
} | ||
}), | ||
); | ||
) | ||
} | ||
} | ||
|
||
mod unimplemented { | ||
use axum::http::StatusCode; | ||
use std::{ | ||
future::Future, | ||
marker::PhantomData, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
pub(super) type Handler0<T> = fn() -> Unimplemented<T>; | ||
pub(super) type Handler1<T1, T> = fn(T1) -> Unimplemented<T>; | ||
|
||
pub struct Unimplemented<T>(PhantomData<T>); | ||
|
||
impl<T> Future for Unimplemented<T> { | ||
type Output = Result<T, StatusCode>; | ||
|
||
fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<T, StatusCode>> { | ||
Poll::Ready(Err(StatusCode::NOT_IMPLEMENTED)) | ||
} | ||
} | ||
|
||
pub(super) fn handler0<T>() -> Unimplemented<T> { | ||
Unimplemented(PhantomData) | ||
} | ||
|
||
router | ||
pub(super) fn handler1<T, T1>(_: T1) -> Unimplemented<T> { | ||
Unimplemented(PhantomData) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters