mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-06 08:16:03 +02:00
46 lines
859 B
Go
46 lines
859 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/micro/cli/v2"
|
|
proto "github.com/asim/go-micro/examples/v3/helloworld/proto"
|
|
"github.com/asim/go-micro/examples/v3/mocking/mock"
|
|
"github.com/asim/go-micro/v3"
|
|
)
|
|
|
|
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)
|
|
}
|