How to do it…

  1. Create greeting-api.go inside the api directory by executing the command $ mkdir api && cd api && touch greeting-api.go.
  2. Copy the following content to greeting-api.go:
package main
import
(
"context"
"encoding/json"
"log"
"strings"
hello "../proto"
"github.com/micro/go-micro"
api "github.com/micro/micro/api/proto"
)
type Say struct
{
Client hello.SayClient
}
func (s *Say) Hello(ctx context.Context, req *api.Request,
rsp *api.Response) error
{
log.Print("Received Say.Hello request - Micro Greeter API")
name, ok := req.Get["name"]
if ok
{
response, err := s.Client.Hello
(
ctx, &hello.Request
{
Name: strings.Join(name.Values, " "),
}
)
if err != nil
{
return err
}
message, _ := json.Marshal
(
map[string]string
{
"message": response.Msg,
}
)
rsp.Body = string(message)
}
return nil
}
func main()
{
service := micro.NewService
(
micro.Name("go.micro.api.greeter"),
)
service.Init()
service.Server().Handle
(
service.Server().NewHandler
(
&Say{Client: hello.NewSayClient("go.micro.service.
greeter", service.Client())},
),
)
if err := service.Run(); err != nil
{
log.Fatal("error starting micro api : ", err)
return
}
}

With everything in place, the directory structure should look like the following:

  1. Move to the api directory and run the program with the following command:
$ go run greeting-api.go
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.135.80