Skip to main content

glory_salvo/
lib.rs

1//! Native Salvo HTTP integration. No HTTP types enter Glory's UI or SSR core.
2#![cfg(not(target_arch = "wasm32"))]
3
4pub use glory_ssr as ssr;
5#[cfg(feature = "routing")]
6pub mod routing;
7use glory_ui::{Scope, Widget};
8use salvo::Response;
9use salvo::http::{HeaderValue, StatusCode, header};
10
11#[cfg(feature = "stream")]
12mod streaming;
13#[cfg(feature = "stream")]
14pub use streaming::{StreamError, StreamPage, StreamRenderer, StreamTimeout, StreamTimeouts};
15
16/// Wrap already-rendered HTML, including explicitly assembled resource snapshots.
17pub fn html_response(html: impl Into<String>) -> Response {
18    let mut response = html_headers();
19    response
20        .write_body(html.into())
21        .expect("fresh response body");
22    response
23}
24
25/// Render a semantic widget on the calling thread. No runtime survives the call.
26pub fn render_response(widget: impl Widget) -> glory_ui::Result<Response> {
27    ssr::render(widget).map(html_response)
28}
29
30/// Construct reactive state under this request's Owner on the calling thread.
31pub fn render_response_with<W: Widget>(
32    factory: impl FnOnce(&Scope) -> W,
33) -> glory_ui::Result<Response> {
34    ssr::render_with(factory).map(html_response)
35}
36
37pub fn document_response(
38    widget: impl Widget,
39    document: &ssr::Document,
40) -> glory_ui::Result<Response> {
41    ssr::render_document(widget, document).map(html_response)
42}
43
44fn html_headers() -> Response {
45    let mut response = Response::new();
46    response.status_code(StatusCode::OK);
47    response.headers_mut().insert(
48        header::CONTENT_TYPE,
49        HeaderValue::from_static("text/html; charset=utf-8"),
50    );
51    response
52}