66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-sms/api/pb"
|
|
)
|
|
|
|
type requestFile struct {
|
|
Config json.RawMessage `json:"config"`
|
|
Params json.RawMessage `json:"params"`
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
log.Fatalf("usage: go run ./examples/grpc_client <request.json> [host:port]")
|
|
}
|
|
address := "127.0.0.1:11200"
|
|
if len(os.Args) > 2 {
|
|
address = os.Args[2]
|
|
}
|
|
|
|
data, err := os.ReadFile(os.Args[1])
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
var input requestFile
|
|
if err := json.Unmarshal(data, &input); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
response, err := pb.NewNoticeClient(conn).Send(ctx, &pb.SendReq{
|
|
Head: &pb.HeadReq{
|
|
RouterId: time.Now().UnixNano(),
|
|
TraceId: fmt.Sprintf("grpc-example-%d", time.Now().Unix()),
|
|
},
|
|
Config: input.Config,
|
|
Params: input.Params,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
output, err := protojson.MarshalOptions{Indent: " "}.Marshal(response)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(string(output))
|
|
}
|