Skip to main content

Using gRPC

  1. Go
package main

import (
	"log"
	"context"
	protoconf_server "github.com/protoconf/protoconf/server/api/proto/v1"
	protoconf_value "github.com/protoconf/protoconf/datatypes/proto/v1"
	server_config "myproject/v1" // replace with your actual import path
	"google.golang.org/grpc"
  "google.golang.org/grpc/credentials/insecure"
	"google.golang.org/protobuf/types/known/anypb"
)

func main() {
	cc, err := grpc.Dial(":4301", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("failed to connect: %v", err)
	}
	defer cc.Close()

	client := protoconf_server.NewProtoconfMutationServiceClient(cc)

	myconfig := &server_config.ServerConfiguration{MaxConnections: 1000, EnableLogging: false}
	valueToSend, err := anypb.New(myconfig)
	if err != nil {
		log.Fatalf("failed to create anypb.New: %v", err)
	}

	request := &protoconf_server.ConfigMutationRequest{Path: "demo/inputs/server_config", Value: &protoconf_value.ProtoconfValue{ProtoFile: "myproject/v1/server_config.proto", Value:valueToSend}}

	if _, err := client.MutateConfig(context.Background(), request); err != nil {
		log.Fatalf("failed to call MutateConfig: %v", err)
	}
}
  1. Python
import grpc
import server_config_pb2
import protoconf_service_v1_pb2
import protoconf_service_v1_pb2_grpc
import protoconf_value_v1_pb2
from google.protobuf import any_pb2

def run():
    with grpc.insecure_channel('localhost:4301') as channel:
        stub = protoconf_service_v1_pb2_grpc.ProtoconfMutationServiceStub(channel)

        myconfig = server_config_pb2.ServerConfiguration(max_connections=1000, enable_logging=False)
        value = any_pb2.Any()
        value.Pack(myconfig)
        protoconf_value = protoconf_value_v1_pb2.ProtoconfValue(proto_file="myproject/v1/server_config.proto", value=value)

        response = stub.MutateConfig(protoconf_service_v1_pb2.ConfigMutationRequest(path="demo/inputs/server_config", value=protoconf_value))
        print("Config updated")

if __name__ == '__main__':
    run()
  1. Node.js
const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
const serverConfig = require("myproject/v1/server_config_pb.js"); // replace with your actual import path

const PROTO_PATH = __dirname + "/protoconf_service_v1.proto";
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
});
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const protoconfService =
  protoDescriptor.protoconf_service_v1.ProtoconfMutationService;

const client = new protoconfService(
  "localhost:4301",
  grpc.credentials.createInsecure()
);

const serverConfiguration = new serverConfig.ServerConfiguration();
serverConfiguration.setMaxConnections(1000);
serverConfiguration.setEnableLogging(false);

const protoconfValue = new protoDescriptor.ProtoconfValue();
protoconfValue.setProtoFile("myproject/v1/server_config.proto");
protoconfValue.setValue(serverConfiguration);

const request = new protoDescriptor.ConfigMutationRequest();
request.setPath("demo/inputs/server_config");
request.setValue(protoconfValue);

client.MutateConfig(request, (err, response) => {
  if (err) {
    console.error;
    console.error("Error: ", err);
  } else {
    console.log("Config updated");
  }
});

Now for the Rust and Java examples.

  1. Rust

Please note that in Rust, you need to use the tonic crate for gRPC and prost crate for Protocol Buffers. Here's a simplified example:

use tonic::transport::Channel;
use protoconf::server::api::v1::protoconf_mutation_service_client::ProtoconfMutationServiceClient;
use protoconf::server::api::v1::ConfigMutationRequest;
use protoconf::datatypes::proto::v1::ProtoconfValue;
use myproject::v1::server_config::ServerConfiguration;
use prost_types::Any;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let channel = Channel::from_static("http://[::1]:4301")
        .connect()
        .await?;

    let mut client = ProtoconfMutationServiceClient::new(channel);

    let myconfig = ServerConfiguration {
        max_connections: 1000,
        enable_logging: false,
        ..Default::default()
    };

    let value = Any {
        type_url: "type.googleapis.com/myproject.v1.ServerConfiguration".into(),
        value: myconfig.encode_to_vec(),
    };

    let protoconf_value = ProtoconfValue {
        proto_file: "myproject/v1/server_config.proto".into(),
        value: Some(value),
        secrets: vec![],
    };

    let request = ConfigMutationRequest {
        path: "demo/inputs/server_config".into(),
        value: Some(protoconf_value),
        script_metadata: "".into(),
    };

    let response = client.mutate_config(request).await?;

    println!("Response: {:?}", response);

    Ok(())
}
  1. Java

In Java, you would use the grpc-java library:

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import com.protoconf.server.api.v1.ProtoconfMutationServiceGrpc;
import com.protoconf.server.api.v1.ConfigMutationRequest;
import com.protoconf.datatypes.proto.v1.ProtoconfValue;
import com.google.protobuf.Any;
import myproject.v1.ServerConfiguration; // replace with your actual import path

public class Main {
    public static void main(String[] args) throws Exception {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 4301)
                .usePlaintext()
                .build();

        ProtoconfMutationServiceGrpc.ProtoconfMutationServiceBlockingStub stub = ProtoconfMutationServiceGrpc.newBlockingStub(channel);

        ServerConfiguration myconfig = ServerConfiguration.newBuilder()
                .setMaxConnections(1000)
                .setEnableLogging(false)
                .build();

        Any any = Any.pack(myconfig);

        ProtoconfValue protoconfValue = ProtoconfValue.newBuilder()
                .setProtoFile("myproject/v1/server_config.proto")
                .setValue(any)
                .build();

        ConfigMutationRequest request = ConfigMutationRequest.newBuilder()
                .setPath("demo/inputs/server_config")
                .setValue(protoconfValue)
                .build();

        stub.mutateConfig(request);

        channel.shutdown();
    }
}