tauri-plugin-httpd 0.1.0

A Tauri plugin that provides multi-instance HTTP server support.
Documentation
# 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


1. Add the dependency to your `src-tauri/Cargo.toml`:

```toml
[dependencies]
tauri-plugin-httpd = { git = "https://github.com/alwynkalleii/tauri-plugin-httpd", branch = "v2" }
```

2. Modify `lib.rs` to initialize the plugin:

```diff
#[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");
}
```

3. Install the JavaScript Guest bindings using your preferred JavaScript package manager:

```
npm add tauri-plugin-httpd-api
```

## Usage

### 1. Static Path Resolution

The `listen_static` method supports the following path formats:

```rust
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 javascript:

```javascript
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]https://github.com/tauri-apps/tauri/blob/dev/crates/tauri/src/path/mod.rs#L183-L200 such as `$APPDATA`, `$RESOURCE`, `$DOCUMENT`, etc.

### 2. Custom Router


Add `axum` to your dependencies (required to define custom router):

```sh
cargo add axum
```

The `listen` method supports custom your router:

```rust
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-service`
- `allow-get-services`
- `allow-listen-static`

## License


MIT