mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-06 05:30:05 +00:00
gRPC 上报系统信息
This commit is contained in:
@@ -2,33 +2,21 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "github.com/p14yground/nezha/proto"
|
||||
"github.com/p14yground/nezha/service/handler"
|
||||
"github.com/p14yground/nezha/service/monitor"
|
||||
)
|
||||
|
||||
// Auth ..
|
||||
type Auth struct {
|
||||
AppKey string
|
||||
AppSecret string
|
||||
}
|
||||
|
||||
// GetRequestMetadata ..
|
||||
func (a *Auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
|
||||
return map[string]string{"app_key": a.AppKey, "app_secret": a.AppSecret}, nil
|
||||
}
|
||||
|
||||
// RequireTransportSecurity ..
|
||||
func (a *Auth) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
auth := Auth{
|
||||
auth := handler.AuthHandler{
|
||||
AppKey: "naiba",
|
||||
AppSecret: "nbsecret",
|
||||
AppSecret: "123456",
|
||||
}
|
||||
conn, err := grpc.Dial(":5555", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
|
||||
if err != nil {
|
||||
@@ -36,13 +24,27 @@ func main() {
|
||||
}
|
||||
defer conn.Close()
|
||||
client := pb.NewNezhaServiceClient(conn)
|
||||
ctx := context.Background()
|
||||
|
||||
resp, err := client.Register(ctx, monitor.GetHost().PB())
|
||||
if err != nil {
|
||||
log.Printf("client.Register err: %v", err)
|
||||
}
|
||||
log.Printf("Register resp: %s", resp)
|
||||
|
||||
hc, err := client.Heartbeat(ctx, &pb.Beat{
|
||||
Timestamp: fmt.Sprintf("%v", time.Now()),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("client.Register err: %v", err)
|
||||
}
|
||||
log.Printf("Register resp: %s", hc)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
resp, err := client.ReportState(context.Background(), &pb.State{})
|
||||
resp, err := client.ReportState(ctx, monitor.GetState(3).PB())
|
||||
if err != nil {
|
||||
log.Fatalf("client.Search err: %v", err)
|
||||
log.Printf("client.ReportState err: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("resp: %s", resp)
|
||||
log.Printf("ReportState resp: %s", resp)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user