Skip to content
This repository was archived by the owner on Jul 4, 2024. It is now read-only.

Commit 2f3285a

Browse files
committed
.
0 parents  commit 2f3285a

File tree

14 files changed

+5195
-0
lines changed

14 files changed

+5195
-0
lines changed

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Generator
2+
3+
This is code generator for go-micro
4+
5+
## Install
6+
7+
```
8+
go install github.com/go-micro/generator/cmd/protoc-gen-micro
9+
```
10+
11+
Also required:
12+
13+
- [protoc](https://github.com/google/protobuf)
14+
- [protoc-gen-go](https://google.golang.org/protobuf)
15+
16+
## Usage
17+
18+
Define your service as `greeter.proto`
19+
20+
```
21+
syntax = "proto3";
22+
23+
package greeter;
24+
option go_package = "/proto;greeter";
25+
26+
service Greeter {
27+
rpc Hello(Request) returns (Response) {}
28+
}
29+
30+
message Request {
31+
string name = 1;
32+
}
33+
34+
message Response {
35+
string msg = 1;
36+
}
37+
```
38+
39+
Generate the code
40+
41+
```
42+
protoc --proto_path=. --micro_out=. --go_out=. greeter.proto
43+
```
44+
45+
Your output result should be:
46+
47+
```
48+
./
49+
greeter.proto # original protobuf file
50+
greeter.pb.go # auto-generated by protoc-gen-go
51+
greeter.micro.go # auto-generated by protoc-gen-micro
52+
```
53+
54+
The micro generated code includes clients and handlers which reduce boiler plate code
55+
56+
### Server
57+
58+
Register the handler with your micro server
59+
60+
```go
61+
type Greeter struct{}
62+
63+
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
64+
rsp.Msg = "Hello " + req.Name
65+
return nil
66+
}
67+
68+
proto.RegisterGreeterHandler(service.Server(), &Greeter{})
69+
```
70+
71+
### Client
72+
73+
Create a service client with your micro client
74+
75+
```go
76+
client := proto.NewGreeterService("greeter", service.Client())
77+
```
78+
79+
### Errors
80+
81+
If you see an error about `protoc-gen-micro` not being found or executable, it's likely your environment may not be configured correctly. If you've already installed `protoc`, `protoc-gen-go`, and `protoc-gen-micro` ensure you've included `$GOPATH/bin` in your `PATH`.
82+
83+
Alternative specify the Go plugin paths as arguments to the `protoc` command
84+
85+
```
86+
protoc --plugin=protoc-gen-go=$GOPATH/bin/protoc-gen-go --plugin=protoc-gen-micro=$GOPATH/bin/protoc-gen-micro --proto_path=. --micro_out=. --go_out=. greeter.proto
87+
```
88+
89+
## LICENSE
90+
91+
protoc-gen-micro is a liberal reuse of protoc-gen-go hence we maintain the original license
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Run tests
2+
on: [push, pull_request]
3+
4+
jobs:
5+
6+
test:
7+
name: Test repo
8+
runs-on: ubuntu-latest
9+
steps:
10+
11+
- name: Set up Go 1.13
12+
uses: actions/setup-go@v1
13+
with:
14+
go-version: 1.13
15+
id: go
16+
17+
- name: Check out code into the Go module directory
18+
uses: actions/checkout@v2
19+
20+
- name: Get dependencies
21+
run: |
22+
go get -v -t -d ./...
23+
24+
- name: Run tests
25+
id: tests
26+
env:
27+
IN_TRAVIS_CI: yes
28+
run: go test -v ./...
29+
30+
- name: Notify of test failure
31+
if: failure()
32+
uses: rtCamp/action-slack-notify@v2.0.0
33+
env:
34+
SLACK_CHANNEL: build
35+
SLACK_COLOR: '#BF280A'
36+
SLACK_ICON: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
37+
SLACK_TITLE: Tests Failed
38+
SLACK_USERNAME: GitHub Actions
39+
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
40+
41+
- name: Notify of test success
42+
if: success()
43+
uses: rtCamp/action-slack-notify@v2.0.0
44+
env:
45+
SLACK_CHANNEL: build
46+
SLACK_COLOR: '#1FAD2B'
47+
SLACK_ICON: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
48+
SLACK_TITLE: Tests Passed
49+
SLACK_USERNAME: GitHub Actions
50+
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
51+

cmd/protoc-gen-micro/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
_build
10+
11+
# Architecture specific extensions/prefixes
12+
*.[568vq]
13+
[568vq].out
14+
15+
*.cgo1.go
16+
*.cgo2.c
17+
_cgo_defun.c
18+
_cgo_gotypes.go
19+
_cgo_export.*
20+
21+
_testmain.go
22+
23+
*.exe
24+
*.test
25+
*.prof
26+
27+
# ignore go build and test outputs
28+
coverage.txt
29+
coverage.out
30+
31+
# ignore locally built binaries
32+
protoc-gen-micro
33+
dist

cmd/protoc-gen-micro/README.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# protoc-gen-micro
2+
3+
This is protobuf code generation for go-micro. We use protoc-gen-micro to reduce boilerplate code.
4+
5+
## Install
6+
7+
```
8+
go install github.com/go-micro/generator/cmd/protoc-gen-micro
9+
```
10+
11+
Also required:
12+
13+
- [protoc](https://github.com/google/protobuf)
14+
- [protoc-gen-go](https://google.golang.org/protobuf)
15+
16+
## Usage
17+
18+
Define your service as `greeter.proto`
19+
20+
```
21+
syntax = "proto3";
22+
23+
package greeter;
24+
option go_package = "/proto;greeter";
25+
26+
service Greeter {
27+
rpc Hello(Request) returns (Response) {}
28+
}
29+
30+
message Request {
31+
string name = 1;
32+
}
33+
34+
message Response {
35+
string msg = 1;
36+
}
37+
```
38+
39+
Generate the code
40+
41+
```
42+
protoc --proto_path=. --micro_out=. --go_out=. greeter.proto
43+
```
44+
45+
Your output result should be:
46+
47+
```
48+
./
49+
greeter.proto # original protobuf file
50+
greeter.pb.go # auto-generated by protoc-gen-go
51+
greeter.micro.go # auto-generated by protoc-gen-micro
52+
```
53+
54+
The micro generated code includes clients and handlers which reduce boiler plate code
55+
56+
### Server
57+
58+
Register the handler with your micro server
59+
60+
```go
61+
type Greeter struct{}
62+
63+
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
64+
rsp.Msg = "Hello " + req.Name
65+
return nil
66+
}
67+
68+
proto.RegisterGreeterHandler(service.Server(), &Greeter{})
69+
```
70+
71+
### Client
72+
73+
Create a service client with your micro client
74+
75+
```go
76+
client := proto.NewGreeterService("greeter", service.Client())
77+
```
78+
79+
### Errors
80+
81+
If you see an error about `protoc-gen-micro` not being found or executable, it's likely your environment may not be configured correctly. If you've already installed `protoc`, `protoc-gen-go`, and `protoc-gen-micro` ensure you've included `$GOPATH/bin` in your `PATH`.
82+
83+
Alternative specify the Go plugin paths as arguments to the `protoc` command
84+
85+
```
86+
protoc --plugin=protoc-gen-go=$GOPATH/bin/protoc-gen-go --plugin=protoc-gen-micro=$GOPATH/bin/protoc-gen-micro --proto_path=. --micro_out=. --go_out=. greeter.proto
87+
```
88+
89+
### Endpoint
90+
91+
Add a micro API endpoint which routes directly to an RPC method
92+
93+
Usage:
94+
95+
1. Clone `github.com/googleapis/googleapis` to use this feature as it requires http annotations.
96+
2. The protoc command must include `-I$GOPATH/src/github.com/googleapis/googleapis` for the annotations import.
97+
98+
```diff
99+
syntax = "proto3";
100+
101+
package greeter;
102+
option go_package = "/proto;greeter";
103+
104+
import "google/api/annotations.proto";
105+
106+
service Greeter {
107+
rpc Hello(Request) returns (Response) {
108+
option (google.api.http) = { post: "/hello"; body: "*"; };
109+
}
110+
}
111+
112+
message Request {
113+
string name = 1;
114+
}
115+
116+
message Response {
117+
string msg = 1;
118+
}
119+
```
120+
121+
The proto generates a `RegisterGreeterHandler` function with a [api.Endpoint](https://godoc.org/go-micro.dev/v3/api#Endpoint).
122+
123+
```diff
124+
func RegisterGreeterHandler(s server.Server, hdlr GreeterHandler, opts ...server.HandlerOption) error {
125+
type greeter interface {
126+
Hello(ctx context.Context, in *Request, out *Response) error
127+
}
128+
type Greeter struct {
129+
greeter
130+
}
131+
h := &greeterHandler{hdlr}
132+
opts = append(opts, api.WithEndpoint(&api.Endpoint{
133+
Name: "Greeter.Hello",
134+
Path: []string{"/hello"},
135+
Method: []string{"POST"},
136+
Handler: "rpc",
137+
}))
138+
return s.Handle(s.NewHandler(&Greeter{h}, opts...))
139+
}
140+
```
141+
142+
## LICENSE
143+
144+
protoc-gen-micro is a liberal reuse of protoc-gen-go hence we maintain the original license

0 commit comments

Comments
 (0)