pub fn server_fn_by_path(
    path: &str
) -> Option<Arc<dyn Fn(&[u8]) -> Pin<Box<dyn Future<Output = Result<String, ServerFnError>>>> + Send + Sync>>
Expand description

Attempts to find a server function registered at the given path.

This can be used by a server to handle the requests, as in the following example (using actix-web)

#[post("{tail:.*}")]
async fn handle_server_fns(
    req: HttpRequest,
    params: web::Path<String>,
    body: web::Bytes,
) -> impl Responder {
    let path = params.into_inner();
    let accept_header = req
        .headers()
        .get("Accept")
        .and_then(|value| value.to_str().ok());

    if let Some(server_fn) = server_fn_by_path(path.as_str()) {
        let body: &[u8] = &body;
        match server_fn(&body).await {
            Ok(serialized) => {
                // if this is Accept: application/json then send a serialized JSON response
                if let Some("application/json") = accept_header {
                    HttpResponse::Ok().body(serialized)
                }
                // otherwise, it's probably a <form> submit or something: redirect back to the referrer
                else {
                    HttpResponse::SeeOther()
                        .insert_header(("Location", "/"))
                        .content_type("application/json")
                        .body(serialized)
                }
            }
            Err(e) => {
                eprintln!("server function error: {e:#?}");
                HttpResponse::InternalServerError().body(e.to_string())
            }
        }
    } else {
        HttpResponse::BadRequest().body(format!("Could not find a server function at that route."))
    }
}