跳到主要内容
本页尚未翻译

下面显示的是英文原文。

App

The #[ruststream::app] attribute turns a builder function into the service's main: a binary that runs the service or prints its AsyncAPI document.

The attribute keeps the function and adds a main that hands it to run_main. The function is synchronous, takes no arguments and returns impl App or a RustStream; any other signature is a compile error. The function is the application builder, unchanged. The binary reads its first argument as a command, the same commands the CLI drives through cargo run. run, or no argument, runs the service until SIGINT or SIGTERM. asyncapi gen prints the AsyncAPI document; -o <file> writes it to a file and --yaml switches the format from JSON. asyncapi gen needs the crate's asyncapi feature. With the logging feature, run installs the console logger first, unless the service installed a subscriber of its own. An error is printed to stderr and the process exits with a failure code. The attribute takes the runtime arguments of #[tokio::main]: flavor = "current_thread" and worker_threads = n. The default is a multi-threaded runtime with one worker per core. A hand-written main calls run_main_on to choose the runtime as an AppRuntime value. A test runs the same builder function on TestApp, as the example does.

Example​

#[subscriber("orders")]
async fn accept(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "accepted");
HandlerOutcome::ack()
}

#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(accept);
})
}