mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-03-22 02:51:50 +00:00
protobuf 代码生成
This commit is contained in:
@@ -4,8 +4,9 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
pb "github.com/EDDYCJY/go-grpc-example/proto"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "github.com/p14yground/nezha/proto"
|
||||
)
|
||||
|
||||
// Auth ..
|
||||
@@ -27,20 +28,21 @@ func (a *Auth) RequireTransportSecurity() bool {
|
||||
func main() {
|
||||
auth := Auth{
|
||||
AppKey: "naiba",
|
||||
AppSecret: "nbsecret0",
|
||||
AppSecret: "nbsecret",
|
||||
}
|
||||
conn, err := grpc.Dial(":5555", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := pb.NewSearchServiceClient(conn)
|
||||
resp, err := client.Search(context.Background(), &pb.SearchRequest{
|
||||
Request: "gRPC",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("client.Search err: %v", err)
|
||||
}
|
||||
client := pb.NewNezhaServiceClient(conn)
|
||||
|
||||
log.Printf("resp: %s", resp.GetResponse())
|
||||
for i := 0; i < 3; i++ {
|
||||
resp, err := client.ReportState(context.Background(), &pb.State{})
|
||||
if err != nil {
|
||||
log.Fatalf("client.Search err: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("resp: %s", resp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,31 +2,34 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
pb "github.com/EDDYCJY/go-grpc-example/proto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/p14yground/nezha/proto"
|
||||
)
|
||||
|
||||
// SearchService ..
|
||||
type SearchService struct {
|
||||
// NezhaService ..
|
||||
type NezhaService struct {
|
||||
auth *Auth
|
||||
}
|
||||
|
||||
// Search ..
|
||||
func (s *SearchService) Search(ctx context.Context, r *pb.SearchRequest) (*pb.SearchResponse, error) {
|
||||
// ReportState ..
|
||||
func (s *NezhaService) ReportState(ctx context.Context, r *pb.State) (*pb.Receipt, error) {
|
||||
if err := s.auth.Check(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.SearchResponse{Response: r.GetRequest() + " Token Server"}, nil
|
||||
fmt.Printf("receive: %s\n", r)
|
||||
return &pb.Receipt{}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
server := grpc.NewServer()
|
||||
pb.RegisterSearchServiceServer(server, &SearchService{})
|
||||
pb.RegisterNezhaServiceServer(server, &NezhaService{})
|
||||
|
||||
lis, err := net.Listen("tcp", ":5555")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user