2020-01-22 13:02:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-01-22 20:20:18 +00:00
|
|
|
"gitlab.bertha.cloud/partitio/grpc/service"
|
2020-01-22 13:02:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GreeterHandler struct{}
|
|
|
|
|
|
|
|
func hello(name string) string {
|
|
|
|
return fmt.Sprintf("Hello %s !", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GreeterHandler) SayHello(ctx context.Context, req *HelloRequest) (*HelloReply, error) {
|
|
|
|
return &HelloReply{Message: hello(req.Name)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GreeterHandler) SayHelloStream(req *HelloStreamRequest, s Greeter_SayHelloStreamServer) error {
|
|
|
|
for i := int64(0); i < req.Count; i++ {
|
|
|
|
if err := s.Send(&HelloReply{Message: hello(req.Name)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
2020-01-22 20:20:18 +00:00
|
|
|
var svc service.Service
|
2020-01-22 13:02:06 +00:00
|
|
|
var err error
|
2020-01-22 20:20:18 +00:00
|
|
|
svc, err = service.New(
|
|
|
|
service.WithContext(ctx),
|
|
|
|
service.WithName("Greeting"),
|
|
|
|
service.WithAfterStart(func() error {
|
2020-01-22 13:02:06 +00:00
|
|
|
fmt.Println("Server listening on", svc.Options().Address())
|
|
|
|
return nil
|
|
|
|
}),
|
2020-01-22 20:20:18 +00:00
|
|
|
service.WithAfterStop(func() error {
|
2020-01-22 13:02:06 +00:00
|
|
|
fmt.Println("Stopping server")
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
RegisterGreeterServer(svc.Server(), &GreeterHandler{})
|
|
|
|
if err := svc.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|