mobility-accounts/examples/grpcclient/main.go

54 lines
1.2 KiB
Go
Executable File

package main
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
"git.coopgo.io/coopgo-platform/mobility-accounts/storage"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
if len(os.Args) < 2 {
log.Error().Msg("missing JSON file path")
return
}
conn, err := grpc.Dial("dns:///localhost:8090", grpc.WithInsecure(), grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy":"round_robin"}`))
if err != nil {
log.Panic().Err(err).Msg("Cannot dial local server")
}
client := grpcapi.NewMobilityAccountsClient(conn)
jsonFile, err := os.Open(os.Args[1])
byteValue, _ := ioutil.ReadAll(jsonFile)
var account storage.Account
json.Unmarshal(byteValue, &account)
accountProto := grpcapi.AccountFromStorageType(&account)
response, err := client.Register(context.Background(), &grpcapi.RegisterRequest{
Account: accountProto,
})
if err != nil {
panic(err)
}
jsonresponse, err := json.Marshal(response)
if err != nil {
panic(err)
}
log.Debug().Str("response", string(jsonresponse)).Msg("JSOn response")
}