dioxus/packages/liveview/examples/salvo.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

#[cfg(not(feature = "salvo"))]
fn main() {}
2022-09-13 21:24:30 +00:00
#[cfg(feature = "salvo")]
#[tokio::main]
async fn main() {
use std::sync::Arc;
2022-09-13 21:24:30 +00:00
use dioxus_core::{Element, LazyNodes, Scope};
use dioxus_liveview as liveview;
use dioxus_liveview::Liveview;
use salvo::extra::affix;
use salvo::extra::ws::WsHandler;
use salvo::prelude::*;
2022-09-13 21:24:30 +00:00
fn app(cx: Scope) -> Element {
cx.render(LazyNodes::new(|f| f.text(format_args!("hello world!"))))
}
2022-09-13 21:24:30 +00:00
pretty_env_logger::init();
let addr = ([127, 0, 0, 1], 3030);
2022-09-13 21:24:30 +00:00
// todo: compactify this routing under one liveview::app method
let view = liveview::new(addr);
2022-09-13 21:24:30 +00:00
let router = Router::new()
.hoop(affix::inject(Arc::new(view)))
.get(index)
.push(Router::with_path("app").get(connect));
2022-09-13 21:24:30 +00:00
Server::new(TcpListener::bind(addr)).serve(router).await;
#[handler]
fn index(depot: &mut Depot, res: &mut Response) {
let view = depot.obtain::<Arc<Liveview>>().unwrap();
let body = view.body("<title>Dioxus LiveView</title>");
res.render(Text::Html(body));
}
#[handler]
async fn connect(
req: &mut Request,
depot: &mut Depot,
res: &mut Response,
) -> Result<(), StatusError> {
let view = depot.obtain::<Arc<Liveview>>().unwrap().clone();
let fut = WsHandler::new().handle(req, res)?;
let fut = async move {
if let Some(ws) = fut.await {
view.upgrade_salvo(ws, app).await;
}
};
tokio::task::spawn(fut);
Ok(())
}
2022-09-13 21:24:30 +00:00
}