2020-12-26 17:17:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2021-10-13 14:31:23 +02:00
|
|
|
proto "github.com/asim/go-micro/examples/v4/helloworld/proto"
|
|
|
|
"github.com/asim/go-micro/examples/v4/mocking/mock"
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4"
|
2021-08-31 10:05:08 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-12-26 17:17:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var c proto.GreeterService
|
|
|
|
|
|
|
|
service := micro.NewService(
|
|
|
|
micro.Flags(&cli.StringFlag{
|
|
|
|
Name: "environment",
|
|
|
|
Value: "testing",
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
service.Init(
|
|
|
|
micro.Action(func(ctx *cli.Context) error {
|
|
|
|
env := ctx.String("environment")
|
|
|
|
// use the mock when in testing environment
|
|
|
|
if env == "testing" {
|
|
|
|
c = mock.NewGreeterService()
|
|
|
|
} else {
|
|
|
|
c = proto.NewGreeterService("helloworld", service.Client())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
// call hello service
|
|
|
|
rsp, err := c.Hello(context.TODO(), &proto.Request{
|
|
|
|
Name: "John",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(rsp.Greeting)
|
|
|
|
}
|