-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathserver.tmpl
193 lines (167 loc) · 4.21 KB
/
server.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Code generated by GripMock. DO NOT EDIT.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"github.com/golang/protobuf/jsonpb"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/runtime/protoiface"
)
{{ range $package, $alias := .Dependencies }}
import {{$alias}} "{{$package}}"
{{end}}
const (
TCP_ADDRESS = "{{.GrpcAddr}}"
HTTP_PORT = ":{{.AdminPort}}"
)
{{ range .Services }}
{{ template "services" . }}
{{ end }}
func main() {
lis, err := net.Listen("tcp", TCP_ADDRESS)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
{{ range .Services }}
{{ template "register_services" . }}
{{ end }}
reflection.Register(s)
fmt.Println("Serving gRPC on tcp://" + TCP_ADDRESS)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
{{ template "find_stub" }}
{{ define "services" }}
type {{.Name}} struct{}
{{ template "methods" .}}
{{ end }}
{{ define "methods" }}
{{ range .Methods}}
{{ if eq .MethodType "standard"}}
{{ template "standard_method" .}}
{{ else if eq .MethodType "server-stream"}}
{{ template "server_stream_method" .}}
{{ else if eq .MethodType "client-stream"}}
{{ template "client_stream_method" .}}
{{ else if eq .MethodType "bidirectional"}}
{{ template "bidirectional_method" .}}
{{ end }}
{{end}}
{{end}}
{{ define "standard_method" }}
func (s *{{.ServiceName}}) {{.Name}}(ctx context.Context, in *{{.Input}}) (*{{.Output}},error){
out := &{{.Output}}{}
err := findStub("{{.ServiceName}}", "{{.Name}}", in, out)
return out, err
}
{{ end }}
{{ define "server_stream_method" }}
func (s *{{.ServiceName}}) {{.Name}}(in *{{.Input}},srv {{.SvcPackage}}{{.ServiceName}}_{{.Name}}Server) error {
out := &{{.Output}}{}
err := findStub("{{.ServiceName}}", "{{.Name}}", in, out)
if err!=nil {
return err
}
return srv.Send(out)
}
{{ end }}
{{ define "client_stream_method"}}
func (s *{{.ServiceName}}) {{.Name}}(srv {{.SvcPackage}}{{.ServiceName}}_{{.Name}}Server) error {
out := &{{.Output}}{}
for {
input,err := srv.Recv()
if err == io.EOF {
return srv.SendAndClose(out)
}
err = findStub("{{.ServiceName}}","{{.Name}}",input,out)
if err != nil {
return err
}
}
}
{{ end }}
{{ define "bidirectional_method"}}
func (s *{{.ServiceName}}) {{.Name}}(srv {{.SvcPackage}}{{.ServiceName}}_{{.Name}}Server) error {
for {
in, err := srv.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
out := &{{.Output}}{}
err = findStub("{{.ServiceName}}","{{.Name}}",in,out)
if err != nil {
return err
}
if err := srv.Send(out); err != nil{
return err
}
}
}
{{end}}
{{ define "register_services" }}
{{.Package}}Register{{.Name}}Server(s, &{{.Name}}{})
{{ end }}
{{ define "find_stub" }}
type payload struct {
Service string `json:"service"`
Method string `json:"method"`
Data interface{} `json:"data"`
}
type response struct {
Data interface{} `json:"data"`
Error string `json:"error"`
Code *codes.Code `json:"code,omitempty"`
}
func findStub(service, method string, in, out protoiface.MessageV1) error {
url := fmt.Sprintf("http://localhost%s/find", HTTP_PORT)
pyl := payload{
Service: service,
Method: method,
Data: in,
}
byt, err := json.Marshal(pyl)
if err != nil {
return err
}
reader := bytes.NewReader(byt)
resp, err := http.DefaultClient.Post(url, "application/json", reader)
if err != nil {
return fmt.Errorf("Error request to stub server %v",err)
}
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf(string(body))
}
respRPC := new(response)
err = json.NewDecoder(resp.Body).Decode(respRPC)
if err != nil {
return fmt.Errorf("decoding json response %v",err)
}
if respRPC.Error != "" || respRPC.Code != nil {
if respRPC.Code == nil {
abortedCode := codes.Aborted
respRPC.Code = &abortedCode
}
if *respRPC.Code != codes.OK {
return status.Error(*respRPC.Code, respRPC.Error)
}
}
data, _ := json.Marshal(respRPC.Data)
return jsonpb.Unmarshal(bytes.NewReader(data), out)
}
{{ end }}