micro/examples/server/handler/example.go

60 lines
1.4 KiB
Go
Raw Normal View History

2015-01-14 02:31:27 +03:00
package handler
import (
2016-03-14 14:01:10 +03:00
"log"
2015-11-20 19:17:33 +03:00
example "github.com/micro/go-micro/examples/server/proto/example"
2016-01-28 20:55:28 +03:00
"github.com/micro/go-micro/metadata"
2015-11-20 19:17:33 +03:00
"github.com/micro/go-micro/server"
2015-05-23 13:53:40 +03:00
"golang.org/x/net/context"
2015-01-14 02:31:27 +03:00
)
type Example struct{}
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
2016-01-28 20:55:28 +03:00
md, _ := metadata.FromContext(ctx)
2016-03-14 14:01:10 +03:00
log.Printf("Received Example.Call request with metadata: %v", md)
rsp.Msg = server.DefaultOptions().Id + ": Hello " + req.Name
2015-01-14 02:31:27 +03:00
return nil
}
2015-12-18 04:01:59 +03:00
func (e *Example) Stream(ctx context.Context, stream server.Streamer) error {
2016-03-14 14:01:10 +03:00
log.Print("Executing streaming handler")
2015-12-18 04:01:59 +03:00
req := &example.StreamingRequest{}
// We just want to receive 1 request and then process here
if err := stream.Recv(req); err != nil {
2016-03-14 14:01:10 +03:00
log.Printf("Error receiving streaming request: %v", err)
2015-12-18 04:01:59 +03:00
return err
}
2016-03-14 14:01:10 +03:00
log.Printf("Received Example.Stream request with count: %d", req.Count)
2015-12-18 04:01:59 +03:00
for i := 0; i < int(req.Count); i++ {
2016-03-14 14:01:10 +03:00
log.Printf("Responding: %d", i)
2015-12-18 04:01:59 +03:00
if err := stream.Send(&example.StreamingResponse{
Count: int64(i),
2015-12-18 04:01:59 +03:00
}); err != nil {
return err
}
}
return nil
}
func (e *Example) PingPong(ctx context.Context, stream server.Streamer) error {
for {
req := &example.Ping{}
if err := stream.Recv(req); err != nil {
return err
}
2016-03-14 14:01:10 +03:00
log.Printf("Got ping %v", req.Stroke)
if err := stream.Send(&example.Pong{Stroke: req.Stroke}); err != nil {
return err
}
}
return nil
}