axum::routing::postでPOSTデータを受け取って、そのデータをクローンしたものをシリアライズしてstring型にし、reqwestで外部に送る。
なお、reqwestで外部に送る処理は関数にして外出しする。
let app = axum::Router::new()
.route("/", axum::routing::get(handle_index))
.route("/home", axum::routing::get(handle_top))
.route("/account", axum::routing::get(handle_account))
.route("/withdrawal", axum::routing::get(handle_withdrawal))
.route("/sent", axum::routing::post(handle_sent))
.route("/post", axum::routing::post(handle_post));
//
async fn handle_sent(axum::Form(unsignedtransaction): axum::Form<UnsignedTransaction>)
-> axum::response::Html<String> {
let s: UnsignedTransaction = unsignedtransaction.clone();
post(s).await;
let tera = tera::Tera::new("templates/*").unwrap();
let mut context = tera::Context::new();
context.insert("time", &unsignedtransaction.time);
context.insert("receiver", &unsignedtransaction.receiver);
context.insert("amount", &unsignedtransaction.amount);
let output = tera.render("sent.html", &context);
axum::response::Html(output.unwrap())
}
async fn post(unsignedtransaction: UnsignedTransaction) -> Result<(), Box<dyn std::error::Error>> {
let serialized: String = serde_json::to_string(&unsignedtransaction).unwrap();
let client = reqwest::Client::new();
let resp = client.post("http://httpbin.org/post")
.body(serialized)
.send()
.await?;
let body = resp.text().await?;
let json: serde_json::Value = serde_json::from_str(&body)?;
let obj = json.as_object().unwrap();
for (key,value) in obj.iter() {
println!("{}\t{}",key,value);
}
Ok(())
}
$ cargo run
//
Finished `dev` profile [unoptimized + debuginfo] target(s) in 4.81s
Running `target/debug/axum_basic`
args {}
data “{\”time\”:\”2025-01-02T10:53:00.295Z\”,\”sender\”:\”12sSVCmfi7kgsdG4ZmaargPppDRvkE5zvD\”,\”receiver\”:\”12sSVCmfi7kgsdG4ZmaargPppDRvkE5zvD\”,\”amount\”:1000}”
files {}
form {}
headers {“Accept”:”*/*”,”Content-Length”:”143″,”Host”:”httpbin.org”,”X-Amzn-Trace-Id”:”Root=1-67767470-5a86f0272c93fb6a0a20a060″}
json {“amount”:1000,”receiver”:”12sSVCmfi7kgsdG4ZmaargPppDRvkE5zvD”,”sender”:”12sSVCmfi7kgsdG4ZmaargPppDRvkE5zvD”,”time”:”2025-01-02T10:53:00.295Z”}
origin “*.*.*”
url “http://httpbin.org/post”
うん、ちゃんとエラーなく送れてますね。
OK, wallet側はアドレスの作成とコインのPOST処理ができてある程度形になってきたので、一旦トランザクションからBlockを作成するところに戻ります。やっぱりUIがあると楽しいですな。。