2020-12-26 17:17:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2020-12-26 17:21:29 +02:00
|
|
|
hello "github.com/micro/go-micro/examples/greeter/srv/proto/hello"
|
2020-12-26 17:17:20 +02:00
|
|
|
"github.com/micro/go-micro/v2"
|
|
|
|
"github.com/micro/go-micro/v2/errors"
|
|
|
|
roundrobin "github.com/micro/go-plugins/wrapper/select/roundrobin/v2"
|
|
|
|
api "github.com/micro/micro/v2/api/proto"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Say struct {
|
|
|
|
Client hello.SayService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Say) Hello(ctx context.Context, req *api.Request, rsp *api.Response) error {
|
|
|
|
log.Print("Received Say.Hello API request")
|
|
|
|
|
|
|
|
name, ok := req.Get["name"]
|
|
|
|
if !ok || len(name.Values) == 0 {
|
|
|
|
return errors.BadRequest("go.micro.api.greeter", "Name cannot be blank")
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s.Client.Hello(ctx, &hello.Request{
|
|
|
|
Name: strings.Join(name.Values, " "),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.StatusCode = 200
|
|
|
|
b, _ := json.Marshal(map[string]string{
|
|
|
|
"message": response.Msg,
|
|
|
|
})
|
|
|
|
rsp.Body = string(b)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
wrapper := roundrobin.NewClientWrapper()
|
|
|
|
|
|
|
|
service := micro.NewService(
|
|
|
|
micro.Name("go.micro.api.greeter"),
|
|
|
|
micro.WrapClient(wrapper),
|
|
|
|
)
|
|
|
|
|
|
|
|
// parse command line flags
|
|
|
|
service.Init()
|
|
|
|
|
|
|
|
service.Server().Handle(
|
|
|
|
service.Server().NewHandler(
|
|
|
|
&Say{Client: hello.NewSayService("go.micro.srv.greeter", service.Client())},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := service.Run(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|