Skip to content

Commit

Permalink
Protoc gen stormrpc (#6)
Browse files Browse the repository at this point in the history
* adding protobuf generator for stormrpc servers and clients using protobuf definitions

* adding installation instructions to readme

* fix comment
  • Loading branch information
actatum authored Jul 1, 2022
1 parent e5c95cd commit 11a4071
Show file tree
Hide file tree
Showing 15 changed files with 864 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cmd
.idea
.idea
prototest/gen_out
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ It provides some convenient features including:

Responses have an `Error` attribute and these are propagated across the wire without needing to tweak your request/response schemas.

## Installation

### Runtime Library

The runtime library package ```github.com/actatum/stormrpc``` contains common types like ```stormrpc.Error```, ```stormrpc.Client``` and ```stormrpc.Server```. If you aren't generating servers and clients from protobuf definitions you only need to import the stormrpc package.

```bash
$ go get github.com/actatum/stormrpc
```

### Code Generator

You need to install ```go``` and the ```protoc``` compiler on your system. Then, install the protoc plugins ```protoc-gen-stormrpc``` and ```protoc-gen-go``` to generate Go code.

```bash
$ go install github.com/actatum/stormrpc/protoc-gen-stormrpc
$ go install google.golang.org/protobuf/cmd/[email protected]
```
Code generation examples can be found [here](https://github.com/actatum/stormrpc/tree/main/examples/protogen)

## Basic Usage

### Server
Expand Down
31 changes: 31 additions & 0 deletions examples/protogen/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/actatum/stormrpc"
"github.com/actatum/stormrpc/examples/protogen/pb"
)

func main() {
client, err := stormrpc.NewClient("nats://0.0.0.0:40897")
if err != nil {
log.Fatal(err)
}
defer client.Close()

c := pb.NewEchoerClient(client)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

out, err := c.Echo(ctx, &pb.EchoRequest{Message: "protogen"})
if err != nil {
log.Fatal(err)
}

fmt.Printf("Response: %s\n", out.GetMessage())
}
2 changes: 2 additions & 0 deletions examples/protogen/genproto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go install ../../protoc-gen-stormrpc
protoc --go_out=./pb --stormrpc_out=./pb pb/echo.proto
210 changes: 210 additions & 0 deletions examples/protogen/pb/echo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/protogen/pb/echo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package protogen;

option go_package = ".;pb";

service Echoer {
rpc Echo (EchoRequest) returns (EchoResponse);
}

message EchoRequest {
string message = 1;
}

message EchoResponse {
string message = 1;
}
95 changes: 95 additions & 0 deletions examples/protogen/pb/echo_stormrpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 11a4071

Please sign in to comment.