-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrpc.go
47 lines (34 loc) · 1.03 KB
/
rpc.go
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
package test
import (
"net/http"
"github.com/alexfalkowski/go-service/meta"
"github.com/alexfalkowski/go-service/net/http/rpc"
v1 "github.com/alexfalkowski/go-service/test/greet/v1"
)
type Request struct {
Name string
}
type Response struct {
Meta meta.Map
Greeting *string
}
type SuccessHandler struct{}
func (*SuccessHandler) Handle(ctx rpc.Context, r *Request) (*Response, error) {
name := ctx.Request().URL.Query().Get("name")
if name == "" {
name = r.Name
}
s := "Hello " + name
return &Response{Greeting: &s}, nil
}
type ProtobufHandler struct{}
func (*ProtobufHandler) Handle(_ rpc.Context, r *v1.SayHelloRequest) (*v1.SayHelloResponse, error) {
return &v1.SayHelloResponse{Message: "Hello " + r.GetName()}, nil
}
func (*ProtobufHandler) Error(_ rpc.Context, err error) *v1.SayHelloResponse {
return &v1.SayHelloResponse{Message: err.Error()}
}
type ErrorHandler struct{}
func (*ErrorHandler) Handle(_ rpc.Context, _ *Request) (*Response, error) {
return nil, rpc.Error(http.StatusServiceUnavailable, "ohh no")
}