Skip to main content

Crate cufflink_native

Crate cufflink_native 

Source
Expand description

Native Cufflink services: extensions written in Rust that WASM services call through ext_call.

A native service is a binary whose main names the service and the extensions it provides, each with the services allowed to call it:

use cufflink_native::prelude::*;

struct GoogleToken;

impl Extension for GoogleToken {
    fn call(&self, ctx: &CallContext, payload: Value) -> Result<Value> {
        let scope = payload["scope"].as_str().ok_or("scope is required")?;
        Ok(json!({ "token": scope, "tenant": ctx.tenant }))
    }
}

fn main() -> std::process::ExitCode {
    cufflink_native::service("ext-google")
        .extension("google.token", GoogleToken, ["reports-service"])
        .run()
}

Service::run prints the service’s manifest under --emit-manifest (what cufflink deploy reads) and otherwise serves the Unix socket the extension runner starts it with. Service::run_nats serves the same extensions straight from NATS, as a provider the project runs itself.

Each call runs on the blocking pool with the caller’s CallContext and is given up at its deadline. Besides the codes of an extension’s own ExtErrors, a call fails with not_found (no such extension), timeout (the deadline passed), panic (the extension panicked), invalid (a malformed call, or on NATS one whose body does not match its subject), unsupported_protocol (a caller speaking another protocol), unavailable (the service is shutting down) or too_large (a reply over the NATS server’s maximum payload). On NATS, a call whose deadline has passed gets no reply.

Modules§

prelude
What an extension needs in scope.

Structs§

CallContext
Who calls, and until when the caller waits.
ExtError
Why an extension failed a call: a code its caller can tell apart and a message.
NatsProvider
Where a NATS provider serves: its tenant and the NATS it connects to.
NatsServer
A service subscribed to its calls, ready to serve.
Service
A native service and the extensions it provides.
SocketServer
A service bound to its socket, ready to serve.

Enums§

NatsAuth
How a component authenticates to NATS: NATS_CREDS_FILE (a .creds file), NATS_NKEY_SEED, or NATS_USER with NATS_PASSWORD; at most one. Without any the connection is anonymous, as before.

Traits§

CallDeadline
The deadline of a call as an Instant.
Extension
An extension: what answers the calls made to one name.

Functions§

service
A native service named name: add its extensions, then run it.

Type Aliases§

Result
What an extension answers.