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 backendsimulator- Enable simulator mode for testingdebug- 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§
- Http
Request - HTTP request type.
- Http
Response - HTTP response type.
- WebServer
App - Web server application configuration for hyperchad rendering.
- WebServer
AppRunner - Runtime wrapper for a web server application.
Enums§
- WebServer
Error - Web server error type.
Traits§
- WebServer
Response Processor - Trait for processing web server requests and responses.