tauri-plugin-httpd-api
v0.1.0
Published
A Tauri plugin that provides multi-instance HTTP server support.
Readme
Tauri Plugin httpd
A Tauri plugin that provides multi-instance HTTP server support. Built on Axum, it allows you to run multiple dynamic API routers and static file servers on different ports simultaneously, with native support for Tauri's path variables (e.g., $APPDATA).
Features
Multi-port Listening: Spin up multiple HTTP instances in one plugin instance.
Hybrid Modes: Host an Axum Router on one port and static assets on another.
Path Resolution: Native support for $APPDATA, $RESOURCE, and other Tauri's path variables.
Async Performance: Powered by the Tauri async runtime; won't block your main thread.
Installation
- Add the dependency to your
src-tauri/Cargo.toml:
[dependencies]
tauri-plugin-httpd = { git = "https://github.com/alwynkalleii/tauri-plugin-httpd", branch = "v2" }- Modify
lib.rsto initialize the plugin:
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
+ .plugin(tauri_plugin_httpd::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}- Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm add tauri-plugin-httpd-apiUsage
1. Static Path Resolution
The listen_static method supports the following path formats:
use tauri_plugin_httpd::HttpdExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_httpd::init())
.setup(|app| {
let app_handle = app.handle().clone();
tauri::async_runtime::block_on(async move {
app_handle.httpd().listen_static("static_service", 12345, "$APPDATA").await.expect("Failed to start static service");
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
or use javescript:
import { getServices, stopService, listenStatic } from "tauri-plugin-httpd-api";
await listenStatic("static_service", 12345, "$APPDATA");
const services = await getServices();
console.log(services);
await stopService("static_service");Supported Formats:
Absolute Paths: e.g.,
C:\Users\Name\Documents(Windows) or/var/www(Linux).Tauri Variables: Supports all Tauri Path Variables such as
$APPDATA,$RESOURCE,$DOCUMENT, etc.
2. Custom Router
Add axum to your dependencies (required to define custom router):
cargo add axumThe listen method supports custom your router:
use axum::{routing::get, Router};
use tauri_plugin_httpd::HttpdExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_httpd::init())
.setup(|app| {
let app_handle = app.handle().clone();
tauri::async_runtime::block_on(async move {
let router = Router::new()
.route("/", get(|| async { "Hello!" }))
.route("/test", get(|| async { "Success" }));
app_handle.httpd().listen("custom_router", 12345, router).await.expect("Failed to start static service");
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}Permissions
This default permission set includes the following:
allow-stop-serviceallow-get-servicesallow-listen-static
License
MIT
