Giter VIP home page Giter VIP logo

nats-protobuf's Introduction

GoDoc

nats-protobuf

nats-protobuf is a protoc plugin that generates client, server, and json over http implementations of the services defined in the .proto file

The library is functional, but given how new it is, expect breaking changes.

Installation

go get github.com/savaki/nats-protobuf/...

Usage

The simplest usage is to use nats-protobuf along side your protoc go:generate line.

//go:generate protoc --go_out=. service.proto
//go:generate protoc --nats_out=. service.proto

Example

For example, given this protoc file, service.proto:

syntax = "proto3";

package simple;

message In {
    string input = 1;
}

message Out {
    string output = 1;
}

service RPC {
    rpc InOut (In) returns (Out);
}

The following is a complete example that illustrates a round trip using client and server transports generated by the nats-rpc plugin.

package main

import (
	"context"
	"fmt"

	"github.com/nats-io/nats"
)

//go:generate protoc --go_out=. service.proto
//go:generate protoc --nats_out=. service.proto

type Service struct {
}

func (s Service) InOut(ctx context.Context, in *In) (*Out, error) {
	return &Out{Output: "Hello " + in.Input}, nil
}

func main() {
	ctx := context.Background()
	nc, _ := nats.Connect(nats.DefaultURL)

	// Server
	cancel, _ := SubscribeRPC(ctx, nc, "subject", "id", Service{})
	defer cancel()

	// Client
	client := NewRPC(nc, "subject")

	out, _ := client.InOut(ctx, &In{Input: "Joe"})
	fmt.Println(out.Output)
}

Additional examples can be found in the examples directory.

Middleware

Both Listen and New* accept an optional list of middleware filters to apply. The filters need to implement:

func(fn func(ctx context.Context, subject string, m *Message) error) func(ctx context.Context, subject string, m *Message) error

For example, if we want to print out the elapsed execution time, we could write a timer as follows:

func Timer(fn nats_protobuf.HandlerFunc) nats_protobuf.HandlerFunc {
	return func(ctx context.Context, subject string, msg *nats_protobuf.Message) error {
		started := time.Now()
		defer func() {
			fmt.Println("elapsed:", time.Now().Sub(started))
		}()
		return fn(ctx, msg)
	}
}

For a more detailed example of middleware in use see the middleware package in the examples directory.

Enjoy!

nats-protobuf's People

Contributors

savaki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nats-protobuf's Issues

Support for nested Request/Response types

Hello,

This is a great project, thanks! I just want to share a limitation that could be of interest. To keep my API logically organised, I've been using nested Request/Response types, and these are not correct generated in the pb.nats.go files. For example:

main.proto:

syntax = "proto3";
package api;

service Main {
    rpc DoThing (Thing.Request) returns (Thing.Response);
}

message Thing {
    message Request {
        string val = 1;
    }
    message Response {
        string val = 1;
    }
}

The generated main.pb.nats.go has references to type Request, not Thing.Request, and does not compile.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.