Skip to content

Rust Rust Client

Gate provides a Rust API for integrating with your Rust applications. You can use the API to interact with Gate programmatically using gRPC.

Environment Setup

First, make sure you have Rust and Cargo installed. If not, install them using rustup:

bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Registry Configuration

  1. Configure Cargo to use the Buf registry. Add the following to your ~/.cargo/config.toml:
toml
[registries.buf]
index = "sparse+https://buf.build/gen/cargo/"
credential-provider = "cargo:token"
  1. Configure authentication (required even for public repositories):
    1. Go to Gate SDKs on Buf
    2. Select the Rust SDK
    3. Scroll down to generate a token
    4. Use the token to authenticate:
    bash
     cargo login --registry buf "Bearer YOUR_TOKEN"

Installation

Add the Gate SDK to your project:

bash
cargo add --registry buf minekube_gate_community_neoeinstein-prost
cargo add --registry buf minekube_gate_community_neoeinstein-tonic
cargo add tonic --features tls-roots # Enable the features we need
cargo add tokio --features macros,rt-multi-thread # Async runtime

This is the sample Cargo.toml file from the docs/developers/api/rust directory:

toml
[package]
name = "rust"
version = "0.1.0"
edition = "2021"

[dependencies]
minekube_gate_community_neoeinstein-prost = { version = "0.4.0-20241118150055-50fffb007499.1", registry = "buf" }
minekube_gate_community_neoeinstein-tonic = { version = "0.4.1-20241118150055-50fffb007499.1", registry = "buf" }
tokio = { version = "1.41.1", features = ["macros", "rt-multi-thread"] }
tonic = "0.12.3"

Usage Example

Here's a basic example of using the Gate Rust API to connect to Gate and list servers:

rust
use minekube_gate_community_neoeinstein_prost::minekube::gate::v1::ListServersRequest;
use minekube_gate_community_neoeinstein_tonic::minekube::gate::v1::tonic::gate_service_client::GateServiceClient;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a runtime for async operations
    let rt = tokio::runtime::Runtime::new()?;
    rt.block_on(async {
        // Create a gRPC channel
        let channel = tonic::transport::Channel::from_static("http://localhost:8080")
            .connect()
            .await?;

        // Create the client
        let mut client = GateServiceClient::new(channel);

        // Make the request
        let request = tonic::Request::new(ListServersRequest {});
        let response = client.list_servers(request).await?;

        // Print the response
        println!("{:#?}", response.get_ref().servers);

        Ok(())
    })
}

Running the Example

  1. Make sure Gate is running with the API enabled
  2. Run the example:
bash
cargo run
[
    Server {
        name: "server1",
        address: "localhost:25566",
        players: 0,
    },
    Server {
        name: "server2",
        address: "localhost:25567",
        players: 0,
    },
    Server {
        name: "server3",
        address: "localhost:25568",
        players: 0,
    },
    Server {
        name: "server4",
        address: "localhost:25569",
        players: 0,
    },
]

Learn More

Refer to the Buf Blog for more information about using the generated Rust SDKs.

Released under the Apache 2.0 License. (web version: ffedb830)