Ниже показан английский оригинал.
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
- Macros
- Manual
#[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);
})
}
struct Accept;
impl Handle<Order> for Accept {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
tracing::info!(order.id, "accepted");
ready(Ok(()))
}
}
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(subscriber("orders", Accept).build());
})
}
// What `#[ruststream::app]` writes: the command line (`run`, `asyncapi gen`) on a multi-threaded
// Tokio runtime.
fn main() -> ExitCode {
run_main(app)
}