From e941a62b5eb58fdc8979696a0bc073a992a04e63 Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Fri, 14 Jan 2022 19:55:07 +0800 Subject: [PATCH] =?UTF-8?q?feat=20:=20grpc=E5=B0=8Fdemo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/murder/internal/handler/scripts_h.go | 26 ++++++++++++++++ .../service/token/internal/server/rpc.go | 31 +++++++++++++++++++ pkg/proto/brahma/murder/hello.proto | 17 ++++++++++ 3 files changed, 74 insertions(+) create mode 100644 app/brahma/service/token/internal/server/rpc.go create mode 100644 pkg/proto/brahma/murder/hello.proto diff --git a/app/brahma/api/murder/internal/handler/scripts_h.go b/app/brahma/api/murder/internal/handler/scripts_h.go index 8c3af63..2a6c534 100644 --- a/app/brahma/api/murder/internal/handler/scripts_h.go +++ b/app/brahma/api/murder/internal/handler/scripts_h.go @@ -1,14 +1,19 @@ package handler import ( + "context" "fmt" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/model" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/service" + "git.icechen.cn/monorepo/backend/pkg/proto/brahma/murder" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" "go.uber.org/zap" + "google.golang.org/grpc" + "log" "strconv" + "time" ) func GetScriptsH(ctx *fiber.Ctx) error { @@ -75,3 +80,24 @@ func DeleteScriptsH(ctx *fiber.Ctx) error { } return ctx.JSON(success(nil)) } + +func HelloWorld() error { + // Set up a connection to the server. + conn, err := grpc.Dial("localhost:3000", grpc.WithInsecure(), grpc.WithBlock()) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + c := murder.NewHelloWorldClient(conn) + + // Contact the server and print out its response. + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + r, err := c.Login(ctx, &murder.HelloRequest{Code: "lht"}) + + if err != nil { + log.Fatalf("could not greet: %v", err) + } + log.Printf("Greeting: %s", r.Token) + return nil +} diff --git a/app/brahma/service/token/internal/server/rpc.go b/app/brahma/service/token/internal/server/rpc.go new file mode 100644 index 0000000..35f6633 --- /dev/null +++ b/app/brahma/service/token/internal/server/rpc.go @@ -0,0 +1,31 @@ +package servesr + +import ( + "context" + "fmt" + "git.icechen.cn/monorepo/backend/pkg/proto/brahma/murder" + "google.golang.org/grpc" + "log" + "net" +) + +type helloRequet struct { + murder.UnimplementedHelloWorldServer +} + +func (helloRequet) Login(ctx context.Context, name *murder.HelloRequest) (*murder.HelloResponse, error) { + return &murder.HelloResponse{Token: name.Code}, nil +} + +func RpcServer() { + lis, err := net.Listen("tcp", ":3000") + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + murder.RegisterHelloWorldServer(s, &helloRequet{}) + fmt.Println("lark server run in :3000") + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/pkg/proto/brahma/murder/hello.proto b/pkg/proto/brahma/murder/hello.proto new file mode 100644 index 0000000..5c6d3a0 --- /dev/null +++ b/pkg/proto/brahma/murder/hello.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +option go_package = "git.icechen.cn/monorepo/backend/pkg/proto/brahma/murder"; + +package example_service; + +service HelloWorld { + rpc Login (HelloRequest) returns (HelloResponse) {} +} + +message HelloRequest { + string code = 1; +} + +message HelloResponse { + string token = 1; +} \ No newline at end of file