|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "os" |
| 6 | + |
| 7 | + pb "github.com/rahul2393/rahul-microservice/user-service/proto/user" |
| 8 | + microclient "github.com/micro/go-micro/client" |
| 9 | + "github.com/micro/go-micro/cmd" |
| 10 | + "golang.org/x/net/context" |
| 11 | + "github.com/micro/cli" |
| 12 | + "github.com/micro/go-micro" |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +func main() { |
| 17 | + |
| 18 | + cmd.Init() |
| 19 | + |
| 20 | + // Create new greeter client |
| 21 | + client := pb.NewUserServiceClient("go.micro.srv.user", microclient.DefaultClient) |
| 22 | + |
| 23 | + // Define our flags |
| 24 | + service := micro.NewService( |
| 25 | + micro.Flags( |
| 26 | + cli.StringFlag{ |
| 27 | + Name: "name", |
| 28 | + Usage: "You full name", |
| 29 | + }, |
| 30 | + cli.StringFlag{ |
| 31 | + Name: "email", |
| 32 | + Usage: "Your email", |
| 33 | + }, |
| 34 | + cli.StringFlag{ |
| 35 | + Name: "password", |
| 36 | + Usage: "Your password", |
| 37 | + }, |
| 38 | + cli.StringFlag{ |
| 39 | + Name: "company", |
| 40 | + Usage: "Your company", |
| 41 | + }, |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + // Start as service |
| 46 | + service.Init( |
| 47 | + |
| 48 | + micro.Action(func(c *cli.Context) { |
| 49 | + |
| 50 | + name := c.String("name") |
| 51 | + email := c.String("email") |
| 52 | + password := c.String("password") |
| 53 | + company := c.String("company") |
| 54 | + |
| 55 | + // Call our user service |
| 56 | + r, err := client.Create(context.TODO(), &pb.User{ |
| 57 | + Name: name, |
| 58 | + Email: email, |
| 59 | + Password: password, |
| 60 | + Company: company, |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + log.Fatalf("Could not create: %v", err) |
| 64 | + } |
| 65 | + log.Printf("Created: %s", r.User.Id) |
| 66 | + |
| 67 | + getAll, err := client.GetAll(context.Background(), &pb.Request{}) |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("Could not list users: %v", err) |
| 70 | + } |
| 71 | + for _, v := range getAll.Users { |
| 72 | + log.Println(v) |
| 73 | + } |
| 74 | + |
| 75 | + os.Exit(0) |
| 76 | + }), |
| 77 | + ) |
| 78 | + |
| 79 | + // Run the server |
| 80 | + if err := service.Run(); err != nil { |
| 81 | + log.Println(err) |
| 82 | + } |
| 83 | +} |
0 commit comments