|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + |
| 7 | + "github.com/micro/go-micro/v2" |
| 8 | + "github.com/micro/go-micro/v2/client" |
| 9 | + "github.com/micro/go-micro/v2/metadata" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + |
| 12 | + "github.com/golang/protobuf/ptypes/wrappers" |
| 13 | + |
| 14 | + userPB "github.com/xmlking/micro-starter-kit/service/account/proto/user" |
| 15 | + "github.com/xmlking/micro-starter-kit/shared/config" |
| 16 | + "github.com/xmlking/micro-starter-kit/shared/constants" |
| 17 | + _ "github.com/xmlking/micro-starter-kit/shared/logger" |
| 18 | + logWrapper "github.com/xmlking/micro-starter-kit/shared/wrapper/log" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + cfg = config.GetConfig() |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + log.Debug().Msgf("IsProduction? %v", config.IsProduction()) |
| 27 | + //log.Debug().Interface("Dialect", cfg.Database.Dialect).Send() |
| 28 | + //log.Debug().Msg(cfg.Database.Host) |
| 29 | + //log.Debug().Uint32("Port", cfg.Database.Port).Send() |
| 30 | + //log.Debug().Uint64("FlushInterval", cfg.Features.Tracing.FlushInterval).Send() |
| 31 | + //log.Debug().Msgf("cfg is %v", cfg) |
| 32 | + |
| 33 | + username := flag.String("username", "sumo", "username of user to be create") |
| 34 | + email := flag. String( "email", "[email protected]", "email of user to be create") |
| 35 | + limit := flag.Uint64("limit", 10, "Limit number of results") |
| 36 | + flag.Parse() |
| 37 | + |
| 38 | + log.Debug().Str("username", *username).Str("email", *email).Uint64("limit", *limit).Msg("Flags Using:") |
| 39 | + |
| 40 | + userService := userPB.NewUserService(constants.ACCOUNT_SERVICE, client.DefaultClient) |
| 41 | + |
| 42 | + if _, err := userService.Create(context.TODO(), &userPB.CreateRequest{ |
| 43 | + Username: &wrappers.StringValue{Value: *username}, |
| 44 | + FirstName: &wrappers.StringValue{Value: "sumo"}, |
| 45 | + LastName: &wrappers.StringValue{Value: "demo"}, |
| 46 | + Email: &wrappers.StringValue{Value: *email}, |
| 47 | + }); err != nil { |
| 48 | + log.Fatal().Err(err).Msg("Unable to create User") |
| 49 | + } |
| 50 | + |
| 51 | + getUserList(userService, uint32(*limit)) |
| 52 | + getUserList2(uint32(*limit)) |
| 53 | +} |
| 54 | + |
| 55 | +func getUserList(us userPB.UserService, limit uint32) { |
| 56 | + if rsp, err := us.List(context.Background(), &userPB.ListRequest{Limit: &wrappers.UInt32Value{Value: limit}}); err != nil { |
| 57 | + log.Fatal().Err(err).Msg("Unable to List Users") |
| 58 | + } else { |
| 59 | + log.Info().Interface("listRsp", rsp).Send() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Just to showcase usage of generic micro client |
| 64 | +func getUserList2(limit uint32) { |
| 65 | + |
| 66 | + // New Service |
| 67 | + service := micro.NewService( |
| 68 | + micro.Name(constants.ACCOUNT_CLIENT), |
| 69 | + micro.Version(config.Version), |
| 70 | + micro.WrapClient(logWrapper.NewClientWrapper()), // Showcase ClientWrapper usage |
| 71 | + ) |
| 72 | + |
| 73 | + cl := service.Client() |
| 74 | + |
| 75 | + // Create new request to service mkit.service.account, method UserService.List |
| 76 | + listReq := cl.NewRequest(constants.ACCOUNT_SERVICE, "UserService.List", &userPB.ListRequest{ |
| 77 | + Limit: &wrappers.UInt32Value{Value: limit}, |
| 78 | + }) |
| 79 | + listRsp := &userPB.ListResponse{} |
| 80 | + |
| 81 | + // Create context with metadata - (Optional) Just for demonstration |
| 82 | + ctx := metadata.NewContext(context.Background(), map[string]string{ |
| 83 | + "X-User-Id": "john", |
| 84 | + "X-From-Id": "script", |
| 85 | + }) |
| 86 | + |
| 87 | + if err := cl.Call(ctx, listReq, listRsp); err != nil { |
| 88 | + log.Fatal().Err(err).Msg("Unable to List Users") |
| 89 | + } else { |
| 90 | + log.Info().Interface("listRsp", listRsp).Send() |
| 91 | + } |
| 92 | +} |
0 commit comments