Skip to main content

Crate hyperchad_renderer_html_web_server

Crate hyperchad_renderer_html_web_server 

Source
Expand description

Web server renderer for HyperChad.

This crate provides a web server backend for rendering HyperChad HTML content. It integrates the HyperChad renderer with switchy_web_server to enable server-side rendering of hyperchad views over HTTP.

§Features

  • assets - Enable static asset serving (enabled by default)
  • actix - Use the Actix-web backend
  • simulator - Enable simulator mode for testing
  • debug - Enable debug logging (enabled by default)

§Example

use hyperchad_renderer_html_web_server::{
    WebServerApp, WebServerResponseProcessor, HttpRequest, HttpResponse, WebServerError
};
use hyperchad_renderer::{RendererEvent, Content};
use async_trait::async_trait;
use bytes::Bytes;
use std::sync::Arc;

// Define your request data type
#[derive(Clone)]
struct MyRequestData {
    path: String,
}

// Implement the response processor
#[derive(Clone)]
struct MyProcessor;

#[async_trait]
impl WebServerResponseProcessor<MyRequestData> for MyProcessor {
    fn prepare_request(
        &self,
        req: HttpRequest,
        _body: Option<Arc<Bytes>>,
    ) -> Result<MyRequestData, WebServerError> {
        Ok(MyRequestData {
            path: req.path().to_string(),
        })
    }

    async fn to_response(&self, data: MyRequestData) -> Result<HttpResponse, WebServerError> {
        Ok(HttpResponse::ok().with_body(format!("Path: {}", data.path)))
    }

    async fn to_body(&self, content: Content, _data: MyRequestData) -> Result<(Bytes, String), WebServerError> {
        // Convert content to bytes and content type
        Ok((Bytes::from("example"), "text/html".to_string()))
    }
}

// Create the web server app
let (tx, rx) = flume::unbounded::<RendererEvent>();
let app = WebServerApp::new(MyProcessor, rx);

Re-exports§

pub use switchy_web_server;

Structs§

HttpRequest
HTTP request type.
HttpResponse
HTTP response type.
WebServerApp
Web server application configuration for hyperchad rendering.
WebServerAppRunner
Runtime wrapper for a web server application.

Enums§

WebServerError
Web server error type.

Traits§

WebServerResponseProcessor
Trait for processing web server requests and responses.