use std::sync::Arc;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use unb::{handler, HandlerError, Reply, Request};
use unb_server::{HostConfig, Node, WebTransportConfig};
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(transparent)]
struct EchoPayload(Value);
#[handler]
async fn echo(request: Request<EchoPayload>) -> Result<Reply<Value>, HandlerError> {
Ok(Reply::new(json!({ "echo": request.payload() })))
}
#[tokio::main]
async fn main() {
let node: Arc<Node> = Node::builder("wt-echo")
.service(echo)
.insecure_accept_declared_peer_identities()
.build()
.unwrap();
let hosting = HostConfig::new(([127, 0, 0, 1], 4433))
.with_webtransport(WebTransportConfig::self_signed_for_development(["localhost"]).unwrap())
.start(&node)
.await
.unwrap();
let hash = hosting
.development_cert_hash()
.unwrap()
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<String>();
println!(
"UNB_WT_URL=https://{}",
hosting.webtransport_addr().unwrap()
);
println!("UNB_WT_CERT_HASH={hash}");
node.cancellation().child_token().cancelled().await;
}