Add server side codegen example

This commit is contained in:
Asim 2015-12-19 23:38:46 +00:00
parent be43d827c7
commit b9898ef45f
2 changed files with 88 additions and 5 deletions

View File

@ -0,0 +1,83 @@
package main
import (
log "github.com/golang/glog"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/examples/server/subscriber"
"github.com/micro/go-micro/server"
"golang.org/x/net/context"
example "github.com/micro/go-micro/examples/server/proto/example"
)
type Example struct{}
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
log.Info("Received Example.Call request")
rsp.Msg = server.Config().Id() + ": Hello " + req.Name
return nil
}
func (e *Example) Stream(ctx context.Context, req *example.StreamingRequest, stream example.Example_StreamStream) error {
log.Infof("Received Example.Stream request with count: %d", req.Count)
for i := 0; i < int(req.Count); i++ {
log.Infof("Responding: %d", i)
if err := stream.SendR(&example.StreamingResponse{
Count: int64(i),
}); err != nil {
return err
}
}
return nil
}
func (e *Example) PingPong(ctx context.Context, stream example.Example_PingPongStream) error {
for {
req, err := stream.RecvR()
if err != nil {
return err
}
log.Infof("Got ping %v", req.Stroke)
if err := stream.SendR(&example.Pong{Stroke: req.Stroke}); err != nil {
return err
}
}
return nil
}
func main() {
// optionally setup command line usage
cmd.Init()
// Initialise Server
server.Init(
server.Name("go.micro.srv.example"),
)
// Register Subscribers
server.Subscribe(
server.NewSubscriber(
"topic.go.micro.srv.example",
new(subscriber.Example),
),
)
server.Subscribe(
server.NewSubscriber(
"topic.go.micro.srv.example",
subscriber.Handler,
),
)
// Register Handler
example.RegisterExampleHandler(
server.DefaultServer, new(Example),
)
// Run server
if err := server.Run(); err != nil {
log.Fatal(err)
}
}

View File

@ -219,18 +219,18 @@ type ExampleHandler interface {
} }
func RegisterExampleHandler(s server.Server, hdlr ExampleHandler) { func RegisterExampleHandler(s server.Server, hdlr ExampleHandler) {
s.Handle(s.NewHandler(&exampleHandler{hdlr})) s.Handle(s.NewHandler(&Example{hdlr}))
} }
type exampleHandler struct { type Example struct {
ExampleHandler ExampleHandler
} }
func (h *exampleHandler) Call(ctx context.Context, in *Request, out *Response) error { func (h *Example) Call(ctx context.Context, in *Request, out *Response) error {
return h.ExampleHandler.Call(ctx, in, out) return h.ExampleHandler.Call(ctx, in, out)
} }
func (h *exampleHandler) Stream(ctx context.Context, stream server.Streamer) error { func (h *Example) Stream(ctx context.Context, stream server.Streamer) error {
m := new(StreamingRequest) m := new(StreamingRequest)
if err := stream.Recv(m); err != nil { if err := stream.Recv(m); err != nil {
return err return err
@ -251,7 +251,7 @@ func (x *exampleStreamStream) SendR(m *StreamingResponse) error {
return x.Streamer.Send(m) return x.Streamer.Send(m)
} }
func (h *exampleHandler) PingPong(ctx context.Context, stream server.Streamer) error { func (h *Example) PingPong(ctx context.Context, stream server.Streamer) error {
return h.ExampleHandler.PingPong(ctx, &examplePingPongStream{stream}) return h.ExampleHandler.PingPong(ctx, &examplePingPongStream{stream})
} }