-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain_grpc.go
65 lines (55 loc) · 1.23 KB
/
main_grpc.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"crypto/tls"
"errors"
"log"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"golang.org/x/net/context"
)
func mainGRPC(addr string) {
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
config := &tls.Config{}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatal(err)
}
config.Certificates = []tls.Certificate{cert}
s := grpc.NewServer(grpc.Creds(credentials.NewTLS(config)))
RegisterInfoServerServer(s, &server{})
s.Serve(lis)
}
type server struct{}
// SetInfo - implements our InfoServer
func (s *server) SetInfo(ctx context.Context, in *InfoRequest) (*InfoReply, error) {
if err := validate(in); err != nil {
return &InfoReply{
Success: false,
Reason: err.Error(),
}, err
}
return &InfoReply{
Success: true,
}, nil
}
// Validate - implement validatable
func (ir *InfoRequest) Validate() error {
var err validationErrors
if ir.Name == "" {
err = append(err, errors.New("Name must be present"))
}
if ir.Age <= 0 {
err = append(err, errors.New("Age must be real"))
}
if ir.Height <= 0 {
err = append(err, errors.New("Height must be real"))
}
if len(err) == 0 {
return nil
}
return err
}