forked from mhausenblas/yages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (60 loc) · 1.97 KB
/
main.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
66
67
68
69
70
71
72
package main
import (
"context"
"log"
"net"
"os"
"github.com/projectcontour/yages/yages"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
func main() {
ipnport := "0.0.0.0:9000"
if ie := os.Getenv("YAGES_BIND"); ie != "" {
ipnport = ie
}
ln, err := net.Listen("tcp", ipnport)
if err != nil {
log.Fatalf("Failed to listen on %s due to %v", ipnport, err)
}
server := grpc.NewServer()
yages.RegisterEchoServer(server, &EchoService{})
grpc_health_v1.RegisterHealthServer(server, &HealthCheckService{})
reflection.Register(server)
log.Printf("Starting YAGES serve on %s\n", ipnport)
if err := server.Serve(ln); err != nil {
log.Fatalf("YAGES serve failed due to %v", err)
}
}
// HealthCheckService implements grpc_health_v1.HealthServer
type HealthCheckService struct{}
func (s *HealthCheckService) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
return &grpc_health_v1.HealthCheckResponse{
Status: grpc_health_v1.HealthCheckResponse_SERVING,
}, nil
}
func (s *HealthCheckService) Watch(req *grpc_health_v1.HealthCheckRequest, server grpc_health_v1.Health_WatchServer) error {
return server.Send(&grpc_health_v1.HealthCheckResponse{
Status: grpc_health_v1.HealthCheckResponse_SERVING,
})
}
// EchoService implements yages.EchoServer
type EchoService struct {
yages.UnimplementedEchoServer
}
// Ping returns a "pong" (constant message).
func (s *EchoService) Ping(ctx context.Context, _ *yages.Empty) (*yages.Content, error) {
return &yages.Content{Text: "pong"}, nil
}
// Reverse returns the message it received in reverse order.
func (s *EchoService) Reverse(ctx context.Context, msg *yages.Content) (*yages.Content, error) {
revstr := func(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
return &yages.Content{Text: revstr(msg.Text)}, nil
}