// This is the server implementation of the secured connection (HTTPS) scenario.
import ballerina/grpc;

// Creates a gRPC Listener endpoint with TLS enabled.
listener grpc:Listener securedEp = new (9090, {
    host: "localhost",
    secureSocket: {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
});

@grpc:ServiceDescriptor {
    descriptor: ROOT_DESCRIPTOR,
    descMap: getDescriptorMap()
}
service "HelloWorld" on securedEp {
    remote function hello(string request) returns string|error {
        // Reads the request message and sends a response.
        return "Hello " + request;
    }
}
// This is the client implementation of the secured connection (HTTPS) scenario.
import ballerina/io;

// Creates a gRPC client to interact securely with the remote server.
HelloWorldClient ep = check new ("https://localhost:9090", {
    secureSocket: {
        cert: "../resource/path/to/public.crt"
    }
});

public function main () returns error? {
    // Executes a simple remote call.
    string result = check ep->hello("WSO2");
    // Prints the received result.
    io:println(result);
}

Secured Simple RPC

The gRPC Server Connector is used to expose gRPC services over HTTP/2. This example demonstrates how a gRPC secured simple service interacts with a gRPC simple client.

For more information on the underlying module, see the GRPC module.

syntax = "proto3";
import "google/protobuf/wrappers.proto";
service HelloWorld {
	rpc hello (google.protobuf.StringValue)
			returns (google.protobuf.StringValue);
}

This is the service definition for the secured connection (HTTPS) scenario.

# Create a new Protocol Buffers definition file named `grpc_secured_simple.proto` and add the service definition to it.
# Run the command below in the Ballerina tools distribution for stub generation.
bal grpc --input grpc_secured_simple.proto  --output stubs
# Once you run the command, `grpc_secured_simple_pb.bal` file is generated inside stubs directory.
# For more information on how to use the Ballerina Protocol Buffers tool, see the [Proto To Ballerina](https://ballerina.io/learn/by-example/proto-to-ballerina.html) example.
import ballerina/grpc;

This is the server implementation of the secured connection (HTTPS) scenario.

listener grpc:Listener securedEp = new (9090, {
    host: "localhost",
    secureSocket: {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
});

Creates a gRPC Listener endpoint with TLS enabled.

@grpc:ServiceDescriptor {
    descriptor: ROOT_DESCRIPTOR,
    descMap: getDescriptorMap()
}
service "HelloWorld" on securedEp {
    remote function hello(string request) returns string|error {
        return "Hello " + request;
    }
}

Reads the request message and sends a response.

# Create a Ballerina package.
# Copy the generated `grpc_secured_simple_pb.bal` stub file to the package.
# For example, if you create a package named `service`, copy the stub file to the `service` package.
# Create a new `grpc_secured_simple.bal` Ballerina file inside the `service` package and add the service implementation.
# Execute the command below to build the 'service' package.
`bal build service`
# Run the service using the command below.
bal run service/target/bin/service.jar
import ballerina/io;

This is the client implementation of the secured connection (HTTPS) scenario.

HelloWorldClient ep = check new ("https://localhost:9090", {
    secureSocket: {
        cert: "../resource/path/to/public.crt"
    }
});

Creates a gRPC client to interact securely with the remote server.

public function main () returns error? {
    string result = check ep->hello("WSO2");

Executes a simple remote call.

    io:println(result);
}

Prints the received result.

# Create a Ballerina package.
# Copy the generated `grpc_secured_simple_pb.bal` stub file to the package.
# For example, if you create a package named `client`, copy the stub file to the `client` package.
# Create a new `grpc_secured_simple_client.bal` Ballerina file inside the `client` package and add the client implementation.
# Execute the command below to build the 'client' package.
`bal build client`
# Run the client using the command below.
bal run client/target/bin/client.jar