Expand description
§Tauri Axum HTMX
A library for creating interactive UIs using HTMX and Axum within Tauri applications. This crate provides the necessary infrastructure to handle HTMX requests through Tauri’s FFI bridge, process them using an Axum application, and return responses back to the webview.
§Overview
In a typical HTMX application, requests are sent to a server which returns HTML to the client. This crate enables this pattern within Tauri applications by:
- Intercepting HTMX requests in the webview
- Forwarding them through Tauri’s FFI bridge
- Processing them with an Axum application running in the Tauri backend
- Returning the response back to be handled by HTMX in the webview
§Quick Start
- First, initialize the client-side integration in your HTML:
<!doctype html>
<html lang="en">
<head>
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
<script type="module">
import { initialize } from "https://unpkg.com/tauri-axum-htmx";
initialize("/"); // the initial path for the application to start on
</script>
</head>
</html>- Then, set up the Tauri command to handle requests:
use std::sync::Arc;
use tokio::sync::Mutex;
use axum::{Router, routing::get};
use tauri::State;
use tauri_axum_htmx::{LocalRequest, LocalResponse};
struct TauriState {
router: Arc<Mutex<Router>>,
}
#[tauri::command]
async fn local_app_request(
state: State<'_, TauriState>,
local_request: LocalRequest,
) -> Result<LocalResponse, ()> {
let mut router = state.router.lock().await;
let response = local_request.send_to_router(&mut router).await;
Ok(response)
}
fn main() {
let app = Router::new()
.route("/", get(|| async { "Hello, World!" }));
let tauri_state = TauriState {
router: Arc::new(Mutex::new(app)),
};
tauri::Builder::default()
.manage(tauri_state)
.invoke_handler(tauri::generate_handler![local_app_request]);
}Structs§
- Local
Request - Represents an HTTP request that can be processed by an Axum router.
- Local
Response - Represents an HTTP response returned from an Axum router.