@ioka-technologies/asyncapi-rust-server-template
v0.0.45
Published
AsyncAPI template for generating production-ready Rust servers
Maintainers
Readme
AsyncAPI Rust Server Template
⚠️ Experimental: This template is still a work in progress and until we reach a 0.1.0 version, assume this is experimental and is not production ready.
Generate production-ready Rust servers from your AsyncAPI specifications using a trait-based library architecture that separates infrastructure concerns from business logic.
Overview
This template generates Rust libraries (not applications) that provide a clean separation between generated infrastructure code and your business logic. When you regenerate from an updated AsyncAPI spec, your business implementations remain untouched while the infrastructure evolves.
Key Benefits:
- Regeneration Safe: Your business logic is never overwritten
- Protocol Agnostic: Same business logic works across WebSocket, HTTP, MQTT, Kafka
- Production Ready: Built-in authentication, error handling, monitoring
- Type Safe: Full Rust type safety with generated message structs
Technical Requirements
- Rust 1.70+
- AsyncAPI CLI 1.0+
Supported Protocols
- WebSocket
- HTTP
- MQTT
- Kafka
- AMQP
Quick Start
Installation
# Install AsyncAPI CLI
npm install -g @asyncapi/cli
# Generate your Rust server library
asyncapi generate fromTemplate asyncapi.yaml @ioka-technologies/asyncapi-rust-server-template -o my-server
cd my-serverBasic Usage
The generated code provides traits that you implement with your business logic:
// Your business logic (never touched by regeneration)
use async_trait::async_trait;
use my_server::*;
pub struct MyService {
database: Database,
}
#[async_trait]
impl UserService for MyService {
async fn handle_user_signup(&self, request: SignupRequest, ctx: &MessageContext) -> Result<User> {
// Your business logic here
let user = self.database.create_user(&request).await?;
Ok(user)
}
}
// Start the server
#[tokio::main]
async fn main() -> Result<()> {
let service = Arc::new(MyService::new());
Server::builder()
.with_user_service(service)
.build()
.start()
.await
}Code Regeneration
Update your AsyncAPI spec and regenerate safely:
# Your business logic remains untouched
asyncapi generate fromTemplate updated-asyncapi.yaml @ioka-technologies/asyncapi-rust-server-template -o my-server --force-writeTemplate Configuration
Configure the template with parameters:
asyncapi generate fromTemplate asyncapi.yaml @ioka-technologies/asyncapi-rust-server-template \
-o my-server \
-p packageName=my-awesome-service \
-p packageVersion=1.0.0 \
-p author="Your Name"| Parameter | Default | Description |
|-----------|---------|-------------|
| packageName | "asyncapi-service" | Name of the generated Rust package |
| packageVersion | "0.1.0" | Version of the generated package |
| author | "AsyncAPI Generator" | Package author |
| license | "Apache-2.0" | Package license |
| includeProfiles | false | Include Cargo profiles; leave disabled when the generated crate is inside another workspace |
| natsQueueGroup | "" | Fallback NATS queue group when bindings do not specify one |
Cargo workspace profiles
Generated crates omit [profile.*] sections by default because Cargo only honors profiles in the workspace root. This avoids warnings when the generated server is nested below another Cargo.toml. Use -p includeProfiles=true only for a standalone generated project.
NATS queue groups
For NATS services, set queue in the NATS binding in your asyncapi.yaml. The generator supports queue groups on servers, channels, and operations:
servers:
production:
host: nats.example.com
protocol: nats
bindings:
nats:
queue: user-service
channels:
userGet:
address: user.get
bindings:
nats:
queue: user-service
operations:
getUser:
action: send
channel:
$ref: '#/channels/userGet'
bindings:
nats:
queue: user-readersThe most specific value wins: operation binding, then channel binding, then server binding, then the natsQueueGroup generator parameter. Services using the same subject and queue name share requests so that only one available instance responds. Channels without a queue remain ordinary broadcast subscriptions.
Architecture
The template generates a library with clear separation of concerns:
- Generated Infrastructure (
src/): Protocol handling, routing, authentication - Your Business Logic (
services/): Trait implementations you create - Generated Traits (
src/handlers.rs): Interfaces you implement
Examples
See the examples directory for sample AsyncAPI specifications and generated code.
Development
# Clone and test locally
git clone https://github.com/Ioka-Technologies/asyncapi-template.git
cd asyncapi-template/rust-server
# Run tests
npm testContributing
- Fork the repository
- Create a feature branch
- Make your changes and add tests
- Run the test suite:
npm test - Submit a pull request
License
Apache-2.0
