-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
71 lines (61 loc) · 1.53 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
package main
import (
"log"
"net"
"os"
"github.com/mhausenblas/yages/yages"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
var (
release string
)
func main() {
ipnport := "0.0.0.0:9000"
if ie := os.Getenv("YAGES_BIND"); ie != "" {
ipnport = ie
}
err := New(ipnport).Listen()
if err != nil {
log.Fatalf("Failed to launch the YAGES due to %v", err)
}
}
// Server wraps the gRPC server and implements yages.EchoServer
type Server struct {
bind string
}
// New creates a new rpc server.
func New(bind string) *Server {
return &Server{bind}
}
// Listen binds the server to the indicated interface:port.
func (s *Server) Listen() error {
ln, err := net.Listen("tcp", s.bind)
if err != nil {
return err
}
gs := grpc.NewServer()
yages.RegisterEchoServer(gs, s)
reflection.Register(gs)
if release == "" {
release = "dev"
}
log.Printf("YAGES in version %v serving on %v is ready for gRPC clients …", release, s.bind)
return gs.Serve(ln)
}
// Ping returns a "pong" (constant message).
func (s *Server) 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 *Server) 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
}