natsrpc

package module
v0.0.0-...-b0358da Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2018 License: MIT Imports: 13 Imported by: 0

README

NATS-RPC Generator

go generate command for creating a client interface, CLI, and serve function for a service interface.

Install

This requires the Protobuf Compiler to be installed.

Install the library and protobuf plugins.

go get github.com/chop-dbhi/nats-rpc/...

Usage

Create a protobuf file with a service definition.

syntax = "proto3";

package example;

message Req {
  int32 left = 1;
  int32 right = 2;
}

message Rep {
  int32 sum = 1;
}

service Service {
  rpc Sum (Req) returns (Rep);
}

Then run:

protoc \
  --go_out=. \
  --nats-rpc_out=. \
  --nats-rpc-cli_out=cmd/cli \
  service.proto

See the example package for the full example and generated output.

service.go contains the implementation of Service and cmd/service/main.go contains the executable code to run.

With a NATS server running on 127.0.0.1:4222, in one terminal run the server.

go run ./cmd/service/main.go

In the other, try the CLI:

go run ./cmd/cli/main.go Sum '{"left": 5, "right": 10}'
{"sum":15}
Parameters

Two parameters are supported for both commands. Parameters are supplied as a set of param-value pairs separated by commas as shown below.

protoc --nats-rpc_out=param1=value1,param2=value2:. service.proto

subject - A Go template string that enables customizing the generated NATS subject prefix. Supported template parameters include:

  • {{.Pkg}} - The name of the package defined in the proto file.
  • {{.Service}} - The name of the service type being generated for.

A subject is produced per method defined on the service and will be appended to this subject prefix in the code. The default subject prefix template is {{.Pkg}}.

outfile - The name of the output file.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseFile

func ParseFile(in *descriptor.FileDescriptorProto, tmpl string, opts Options) (*plugin_go.CodeGeneratorResponse_File, error)

Types

type Options

type Options struct {
	Subject string
	OutFile string
}

func ParseOptions

func ParseOptions(p string) (*Options, error)

type Server

type Server interface {
	Serve(context.Context, ...transport.SubscribeOption) error
}

Directories

Path Synopsis
cmd
Package example is a generated protocol buffer package.
Package example is a generated protocol buffer package.
cmd/cli
Generated by nats-rpc.
Generated by nats-rpc.
Package transport is a generated protocol buffer package.
Package transport is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL