Source package · Trusted Rust host
Học CoAkka Runtime qua Rust + Tauri
Rust sở hữu runtime và typed outcomes. Với Tauri, WebView chỉ gửi intent đã giới hạn; trusted Rust host authorize, admit và gọi CoAkka Runtime. Hai archive 3.3.2 là checksum-pinned source packages, chưa phải crates.io packages.
Beginner · 01 / 03
Cài Rust source package đã kiểm tra
Archive mang crate source, lockfile và năm Core image. Giữ --locked; không đổi nó thành dependency crates.io không tồn tại. Project học nằm cạnh archive và dùng path dependency để package gốc luôn nguyên vẹn.
rustc --version
cargo --versioncurl -fL -o coakka-runtime-rs-3.3.2.tar.gz \
'https://raw.githubusercontent.com/phuong-tran/coakka-publish/2698310797bc1b95e3820382ea17e446ed1f7310/coakka-runtime/rust/releases/3.3.2%2B16bdd692faf918d1a959c073ad20318392659f4b/coakka-runtime-rs-3.3.2.tar.gz'
printf '%s %s\n' \
'aa0be816a4876887b386c329f4659dbc60e1e8f4e689fa2b19f36e56eeb5f1e0' \
'coakka-runtime-rs-3.3.2.tar.gz' | shasum -a 256 -c -
tar -xzf coakka-runtime-rs-3.3.2.tar.gz[package]
name = "coakka-rust-demo"
version = "0.1.0"
edition = "2021"
rust-version = "1.77.2"
publish = false
[dependencies]
coakka-runtime-rs = { path = "../coakka-runtime-rs-3.3.2" }
Package 3.3.2 dùng Core 3.3, connector revision 2; MSRV khai báo là Rust 1.77.2.
Beginner · 02 / 03
Đăng ký handler và ask trong một process
Tạo thư mục coakka-rust-demo/src/bin cạnh thư mục archive đã giải nén. Lưu Cargo.toml ở thư mục dự án và lưu first_run.rs vào src/bin. Chạy lệnh từ thư mục chứa dự án. RuntimeHost là owner; khi owner cuối cùng bị drop, connector dừng native runtime.
use std::time::Duration;
use coakka_runtime::{ConnectorStartSpec, DeliveryHint, PayloadIdentity, RouteSpec, RuntimeHost};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let target = "hello.accept";
let mut spec = ConnectorStartSpec::new("hello-rust", "hello-rust-1");
spec.routes = vec![RouteSpec::single_local(target, "127.0.0.1", 19_191)];
let runtime = RuntimeHost::start(spec)?;
runtime.register_handler(target, |request| {
RuntimeHost::make_json_reply_from_request_identity(
&request,
"hello.accept",
r#"{"accepted":true}"#,
)
.ok()
})?;
let reply = runtime.ask_json(
"hello-client",
target,
r#"{"message":"hello"}"#,
PayloadIdentity::json("hello.accept.request.v1", 1),
Duration::from_secs(2),
"accept",
DeliveryHint::RequireLocal,
)?;
println!("{}", reply.payload_utf8());
Ok(())
}
cd coakka-rust-demo
cargo generate-lockfile --offline
case "$(uname -s)-$(uname -m)" in
Darwin-arm64) COAKKA_PLATFORM=macos-aarch64 ;;
Linux-aarch64|Linux-arm64) COAKKA_PLATFORM=linux-aarch64 ;;
Linux-x86_64) COAKKA_PLATFORM=linux-x86_64 ;;
*) echo "Use the platform recipe in CONSUMING.md"; exit 1 ;;
esac
export CARGO_TARGET_DIR="$(pwd)/target"
cargo build --locked --release --bin first_run
if [ ! -d "$CARGO_TARGET_DIR/release/native/$COAKKA_PLATFORM" ]; then
python3 ../coakka-runtime-rs-3.3.2/tools/package_tools.py deploy \
../coakka-runtime-rs-3.3.2 "$CARGO_TARGET_DIR/release/first_run" \
"$COAKKA_PLATFORM"
fi
"$CARGO_TARGET_DIR/release/first_run"Kết quả mong đợi
{"accepted":true}PayloadIdentity::json- Message type, schema version và JSON format đi cùng payload.
DeliveryHint::RequireLocal- Intent của bài Embedded; không phải route strategy hay retry policy.
Duration- Wait window dùng monotonic time ở boundary; terminal matching vẫn thuộc Core.
Beginner · 03 / 03
Đọc typed deadletter
Không parse error text. Match CoAkkaError::Deadletter, rồi đọc reason và original envelope do Core cấp.
use std::time::Duration;
use coakka_runtime::{CoAkkaError, ConnectorStartSpec, DeliveryHint, PayloadIdentity, RuntimeHost};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let runtime = RuntimeHost::start(ConnectorStartSpec::new("orders-rust", "orders-rust-1"))?;
let result = runtime.ask_json(
"checkout",
"orders.missing",
r#"{"quantity":2}"#,
PayloadIdentity::json("orders.quote.request.v1", 1),
Duration::from_secs(2),
"quote",
DeliveryHint::RequireLocal,
);
match result {
Err(CoAkkaError::Deadletter(deadletter)) => {
println!(
"deadletter reason={} target={}",
deadletter.reason, deadletter.original.target
);
Ok(())
}
Err(error) => Err(error.into()),
Ok(_) => Err("expected route-miss deadletter".into()),
}
}
cd coakka-rust-demo
cargo generate-lockfile --offline
case "$(uname -s)-$(uname -m)" in
Darwin-arm64) COAKKA_PLATFORM=macos-aarch64 ;;
Linux-aarch64|Linux-arm64) COAKKA_PLATFORM=linux-aarch64 ;;
Linux-x86_64) COAKKA_PLATFORM=linux-x86_64 ;;
*) echo "Use the platform recipe in CONSUMING.md"; exit 1 ;;
esac
export CARGO_TARGET_DIR="$(pwd)/target"
cargo build --locked --release --bin route_miss
if [ ! -d "$CARGO_TARGET_DIR/release/native/$COAKKA_PLATFORM" ]; then
python3 ../coakka-runtime-rs-3.3.2/tools/package_tools.py deploy \
../coakka-runtime-rs-3.3.2 "$CARGO_TARGET_DIR/release/route_miss" \
"$COAKKA_PLATFORM"
fi
"$CARGO_TARGET_DIR/release/route_miss"Kết quả mong đợi
deadletter reason=DEADLETTER_REASON_ROUTE_MISS target=orders.missing
Advanced · 04
Đặt Tauri WebView sau intent boundary
Tải và kiểm checksum của archive Tauri. Tạo coakka-tauri-demo/src cạnh thư mục đã giải nén, lưu Cargo.toml ở thư mục dự án và lưu ví dụ vào src/main.rs. Chạy lệnh từ thư mục chứa dự án. WebView không nhận raw runtime handle, file path hay bearer grant.
curl -fL -o coakka-runtime-tauri-intents-3.3.2-source.tar.gz \
'https://raw.githubusercontent.com/phuong-tran/coakka-publish/cedac82c4346b1a219cb5428474984fb2afaeb88/coakka-runtime/tauri/releases/3.3.2%2B16bdd692faf918d1a959c073ad20318392659f4b/coakka-runtime-tauri-intents-3.3.2-source.tar.gz'
printf '%s %s\n' \
'ffe3a7621b2f3522b0a175063e4141f9e8369dd26a11d4c562be8fbee472f29d' \
'coakka-runtime-tauri-intents-3.3.2-source.tar.gz' | shasum -a 256 -c -
tar -xzf coakka-runtime-tauri-intents-3.3.2-source.tar.gz[package]
name = "coakka-tauri-demo"
version = "0.1.0"
edition = "2021"
rust-version = "1.77.2"
publish = false
[dependencies]
coakka-runtime-rs = { path = "../coakka-runtime-tauri-intents-3.3.2-source/rust" }
coakka-tauri-intents = { path = "../coakka-runtime-tauri-intents-3.3.2-source/tauri-intents" }
serde_json = "=1.0.151"
use coakka_runtime::{ConnectorStartSpec, RouteSpec};
use coakka_tauri_intents::{
coakka_ask_intent_command, IntentEnvelope, IntentPayloadIdentity, RuntimeIntentDispatcher,
};
use serde_json::json;
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let target = "desktop.quote";
let mut spec = ConnectorStartSpec::new("desktop", "desktop-1");
spec.routes = vec![RouteSpec::single_local(target, "127.0.0.1", 19_411)];
let dispatcher = RuntimeIntentDispatcher::start(spec)?;
dispatcher.register_json_handler(target, |request| {
let quantity = request.payload["quantity"].as_u64().unwrap_or(0);
Ok(json!({"quote": quantity * 120_000, "currency": "VND"}))
})?;
let result = coakka_ask_intent_command(
&dispatcher,
IntentEnvelope {
intent_id: "quote-1".into(),
source: "trusted-main-window".into(),
target: target.into(),
operation: "quote".into(),
payload: json!({"quantity": 2}),
payload_identity: IntentPayloadIdentity::json("desktop.quote.request.v1", 1),
timeout_ms: Some(2_000),
headers: HashMap::new(),
},
)?;
println!("{}", result.payload);
Ok(())
}
cp coakka-runtime-tauri-intents-3.3.2-source/tauri-intents/Cargo.lock \
coakka-tauri-demo/Cargo.lock
cd coakka-tauri-demo
cargo update --workspace
case "$(uname -s)-$(uname -m)" in
Darwin-arm64) COAKKA_PLATFORM=macos-aarch64 ;;
Linux-aarch64|Linux-arm64) COAKKA_PLATFORM=linux-aarch64 ;;
Linux-x86_64) COAKKA_PLATFORM=linux-x86_64 ;;
*) echo "Use the platform recipe in rust/CONSUMING.md"; exit 1 ;;
esac
export CARGO_TARGET_DIR="$(pwd)/target"
cargo build --locked --release
python3 ../coakka-runtime-tauri-intents-3.3.2-source/rust/tools/package_tools.py deploy \
../coakka-runtime-tauri-intents-3.3.2-source/rust \
"$CARGO_TARGET_DIR/release/coakka-tauri-demo" "$COAKKA_PLATFORM"
"$CARGO_TARGET_DIR/release/coakka-tauri-demo"Kết quả mong đợi
{"currency":"VND","quote":240000}Trong app thật, command authorize window/frame, giới hạn concurrent operations, chạy blocking ask ngoài UI thread và join accepted operations trước khi drop dispatcher.
Advanced · 05
Đọc đúng support tuple
Rust execution đã có evidence trên macOS ARM64, Pi Linux ARM64, WSL2 Linux x64, Windows x64 và Windows ARM64 UTM. Bài Tauri trên Portal gọi lệnh từ chương trình Rust. Kết quả kiểm thử WebView và từng nền tảng nằm trong ghi chú phát hành Tauri 3.3.2.
File Lane và Stream Lane chỉ dành cho trusted Rust host. WebView intent API không mở path, callback hay owner grant.