Add warp adapter
This commit is contained in:
1 parent
0b80d32d18
commit
976d4ab960
9 files changed
+225
-24
No files matched your search
@@ -2,3 +2,5 @@
|
||||
pub mod axum_adapter;
|
||||
#[cfg(feature = "salvo")]
|
||||
pub mod salvo_adapter;
|
||||
#[cfg(feature = "warp")]
|
||||
pub mod warp_adapter;
|
||||
@@ -132,12 +132,12 @@ impl ServerFnHandler {
|
||||
x-www-form-urlencoded",
|
||||
),
|
||||
);
|
||||
res.render(data);
|
||||
res.write_body(data).unwrap();
|
||||
}
|
||||
Payload::Json(data) => {
|
||||
res.headers_mut()
|
||||
.insert("Content-Type", HeaderValue::from_static("application/json"));
|
||||
res.render(data);
|
||||
res.write_body(data).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,136 @@
|
||||
use crate::{LiveViewError, LiveViewSocket};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use warp::ws::{Message, WebSocket};
|
||||
use std::{error::Error, sync::Arc};
|
||||
|
||||
/// Convert a warp websocket into a LiveViewSocket
|
||||
///
|
||||
/// This is required to launch a LiveView app using the warp web framework
|
||||
pub fn warp_socket(ws: WebSocket) -> impl LiveViewSocket {
|
||||
ws.map(transform_rx)
|
||||
.with(transform_tx)
|
||||
.sink_map_err(|_| LiveViewError::SendingFailed)
|
||||
use server_fn::{Payload, ServerFunctionRegistry};
|
||||
use tokio::task::spawn_blocking;
|
||||
use warp::{
|
||||
filters::BoxedFilter,
|
||||
http::{Response, StatusCode},
|
||||
hyper::{body::Bytes, HeaderMap},
|
||||
path, Filter, Reply,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
dioxus_ssr_html,
|
||||
serve::ServeConfig,
|
||||
server_fn::{DioxusServerContext, DioxusServerFnRegistry, ServerFnTraitObj},
|
||||
};
|
||||
|
||||
pub fn register_server_fns(server_fn_route: &'static str) -> BoxedFilter<(impl Reply,)> {
|
||||
let mut filter: Option<BoxedFilter<(_,)>> = None;
|
||||
for server_fn_path in DioxusServerFnRegistry::paths_registered() {
|
||||
let func = DioxusServerFnRegistry::get(server_fn_path).unwrap();
|
||||
let full_route = format!("{server_fn_route}/{server_fn_path}")
|
||||
.trim_start_matches('/')
|
||||
.to_string();
|
||||
let route = path(full_route)
|
||||
.and(warp::post())
|
||||
.and(warp::header::headers_cloned())
|
||||
.and(warp::body::bytes())
|
||||
.and_then(move |headers: HeaderMap, body| {
|
||||
let func = func.clone();
|
||||
async move { server_fn_handler(DioxusServerContext {}, func, headers, body).await }
|
||||
})
|
||||
.boxed();
|
||||
if let Some(boxed_filter) = filter.take() {
|
||||
filter = Some(boxed_filter.or(route).unify().boxed());
|
||||
} else {
|
||||
filter = Some(route.boxed());
|
||||
}
|
||||
}
|
||||
filter.expect("No server functions found")
|
||||
}
|
||||
|
||||
fn transform_rx(message: Result<Message, warp::Error>) -> Result<String, LiveViewError> {
|
||||
// destructure the message into the buffer we got from warp
|
||||
let msg = message
|
||||
.map_err(|_| LiveViewError::SendingFailed)?
|
||||
.into_bytes();
|
||||
pub fn serve_dioxus_application<P: Clone + Send + Sync + 'static>(
|
||||
cfg: ServeConfig<P>,
|
||||
) -> BoxedFilter<(impl Reply,)> {
|
||||
// Serve the dist folder and the index.html file
|
||||
let serve_dir = warp::fs::dir("./dist");
|
||||
|
||||
// transform it back into a string, saving us the allocation
|
||||
let msg = String::from_utf8(msg).map_err(|_| LiveViewError::SendingFailed)?;
|
||||
|
||||
Ok(msg)
|
||||
register_server_fns(cfg.server_fn_route.unwrap_or_default())
|
||||
.or(warp::path::end()
|
||||
.and(warp::get())
|
||||
.map(move || warp::reply::html(dioxus_ssr_html(&cfg))))
|
||||
.or(serve_dir)
|
||||
.boxed()
|
||||
}
|
||||
|
||||
async fn transform_tx(message: String) -> Result<Message, warp::Error> {
|
||||
Ok(Message::text(message))
|
||||
#[derive(Debug)]
|
||||
struct FailedToReadBody(String);
|
||||
|
||||
impl warp::reject::Reject for FailedToReadBody {}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RecieveFailed(String);
|
||||
|
||||
impl warp::reject::Reject for RecieveFailed {}
|
||||
|
||||
async fn server_fn_handler(
|
||||
server_context: DioxusServerContext,
|
||||
function: Arc<ServerFnTraitObj>,
|
||||
headers: HeaderMap,
|
||||
body: Bytes,
|
||||
) -> Result<Box<dyn warp::Reply>, warp::Rejection> {
|
||||
// Because the future returned by `server_fn_handler` is `Send`, and the future returned by this function must be send, we need to spawn a new runtime
|
||||
let (resp_tx, resp_rx) = tokio::sync::oneshot::channel();
|
||||
spawn_blocking({
|
||||
move || {
|
||||
tokio::runtime::Runtime::new()
|
||||
.expect("couldn't spawn runtime")
|
||||
.block_on(async {
|
||||
let resp = match function(server_context, &body).await {
|
||||
Ok(serialized) => {
|
||||
// if this is Accept: application/json then send a serialized JSON response
|
||||
let accept_header =
|
||||
headers.get("Accept").and_then(|value| value.to_str().ok());
|
||||
let mut res = Response::builder();
|
||||
if accept_header == Some("application/json")
|
||||
|| accept_header
|
||||
== Some(
|
||||
"application/\
|
||||
x-www-form-urlencoded",
|
||||
)
|
||||
|| accept_header == Some("application/cbor")
|
||||
{
|
||||
res = res.status(StatusCode::OK);
|
||||
}
|
||||
|
||||
let resp = match serialized {
|
||||
Payload::Binary(data) => res
|
||||
.header("Content-Type", "application/cbor")
|
||||
.body(Bytes::from(data)),
|
||||
Payload::Url(data) => res
|
||||
.header(
|
||||
"Content-Type",
|
||||
"application/\
|
||||
x-www-form-urlencoded",
|
||||
)
|
||||
.body(Bytes::from(data)),
|
||||
Payload::Json(data) => res
|
||||
.header("Content-Type", "application/json")
|
||||
.body(Bytes::from(data)),
|
||||
};
|
||||
|
||||
Box::new(resp.unwrap())
|
||||
}
|
||||
Err(e) => report_err(e),
|
||||
};
|
||||
|
||||
if resp_tx.send(resp).is_err() {
|
||||
eprintln!("Error sending response");
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
resp_rx.await.map_err(|err| {
|
||||
warp::reject::custom(RecieveFailed(format!("Failed to recieve response {err}")))
|
||||
})
|
||||
}
|
||||
|
||||
fn report_err<E: Error>(e: E) -> Box<dyn warp::Reply> {
|
||||
Box::new(
|
||||
Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(format!("Error: {}", e))
|
||||
.unwrap(),
|
||||
) as Box<dyn warp::Reply>
|
||||
}
|
||||
@@ -11,6 +11,8 @@ pub mod prelude {
|
||||
pub use crate::adapters::axum_adapter::*;
|
||||
#[cfg(feature = "salvo")]
|
||||
pub use crate::adapters::salvo_adapter::*;
|
||||
#[cfg(feature = "warp")]
|
||||
pub use crate::adapters::warp_adapter::*;
|
||||
#[cfg(feature = "ssr")]
|
||||
pub use crate::serve::ServeConfig;
|
||||
pub use crate::server_fn::{DioxusServerContext, ServerFn};
|
||||
|
||||
Reference in new issue
Block a user