From 85770b17abdb66f92a3f7332860dab1132ab076b Mon Sep 17 00:00:00 2001 From: finn-qarik <127736249+finn-qarik@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:46:35 +0100 Subject: [PATCH] feat: add grpc example (#66) --- projects/bzl7/.bazelrc | 2 ++ projects/bzl7/BUILD.bazel | 3 +- projects/bzl7/MODULE.bazel | 13 +++++++ projects/bzl7/go/README.md | 19 +++++++--- projects/bzl7/go/grpc/BUILD.bazel | 0 projects/bzl7/go/grpc/client/BUILD.bazel | 19 ++++++++++ projects/bzl7/go/grpc/client/main.go | 44 +++++++++++++++++++++++ projects/bzl7/go/grpc/go.mod | 13 +++++++ projects/bzl7/go/grpc/go.sum | 13 +++++++ projects/bzl7/go/grpc/proto/BUILD.bazel | 24 +++++++++++++ projects/bzl7/go/grpc/proto/hello.proto | 21 +++++++++++ projects/bzl7/go/grpc/server/BUILD.bazel | 18 ++++++++++ projects/bzl7/go/grpc/server/main.go | 45 ++++++++++++++++++++++++ projects/bzl7/platforms/BUILD.bazel | 20 ++++++++--- 14 files changed, 244 insertions(+), 10 deletions(-) create mode 100644 projects/bzl7/go/grpc/BUILD.bazel create mode 100644 projects/bzl7/go/grpc/client/BUILD.bazel create mode 100644 projects/bzl7/go/grpc/client/main.go create mode 100644 projects/bzl7/go/grpc/go.mod create mode 100644 projects/bzl7/go/grpc/go.sum create mode 100644 projects/bzl7/go/grpc/proto/BUILD.bazel create mode 100644 projects/bzl7/go/grpc/proto/hello.proto create mode 100644 projects/bzl7/go/grpc/server/BUILD.bazel create mode 100644 projects/bzl7/go/grpc/server/main.go diff --git a/projects/bzl7/.bazelrc b/projects/bzl7/.bazelrc index 77e7774..eb5dbcc 100644 --- a/projects/bzl7/.bazelrc +++ b/projects/bzl7/.bazelrc @@ -1,6 +1,8 @@ build --incompatible_enable_cc_toolchain_resolution build --incompatible_strict_action_env +build --host_cxxopt=-std=c++17 + build --host_platform=@rules_nixpkgs_core//platforms:host build:gcc10 --platforms=//platforms:gcc_10 diff --git a/projects/bzl7/BUILD.bazel b/projects/bzl7/BUILD.bazel index ce99ef1..ee3d191 100644 --- a/projects/bzl7/BUILD.bazel +++ b/projects/bzl7/BUILD.bazel @@ -4,6 +4,7 @@ load("@gazelle//:def.bzl", "gazelle") gazelle( name = "gazelle", args = [ - "go/gazelle" + "go/gazelle", + "go/grpc", ], ) diff --git a/projects/bzl7/MODULE.bazel b/projects/bzl7/MODULE.bazel index 06bac3c..f0d5987 100644 --- a/projects/bzl7/MODULE.bazel +++ b/projects/bzl7/MODULE.bazel @@ -6,12 +6,15 @@ RULES_NIX_PKGS_VERSION = "0.11.1" bazel_dep(name = "aspect_rules_py", version = "0.7.1") bazel_dep(name = "gazelle", version = "0.36.0") +bazel_dep(name = "grpc", version = "1.62.1") bazel_dep(name = "rules_cc", version = "0.0.9") bazel_dep(name = "rules_go", version = "0.46.0") bazel_dep(name = "rules_nixpkgs_cc") bazel_dep(name = "rules_nixpkgs_core", version = RULES_NIX_PKGS_VERSION) +bazel_dep(name = "rules_proto", version = "6.0.0-rc3") bazel_dep(name = "rules_python", version = "0.31.0") bazel_dep(name = "platforms", version = "0.0.9") +bazel_dep(name = "protobuf", version = "26.0.bcr.1") # Not yet available in the bazel central registry. archive_override( @@ -101,3 +104,13 @@ pip.parse( requirements_lock = "@bzl7//python/pip:requirements_lock.txt", ) use_repo(pip, "pip_example_3_12") + +go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk") +go_sdk.download(version = "1.22.2") + +go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") +go_deps.from_file(go_mod = "//go/grpc:go.mod") +use_repo( + go_deps, + "org_golang_google_grpc", +) diff --git a/projects/bzl7/go/README.md b/projects/bzl7/go/README.md index 8ac5f79..f11877f 100644 --- a/projects/bzl7/go/README.md +++ b/projects/bzl7/go/README.md @@ -1,9 +1,18 @@ # go -We use Gazelle to generate the `BUILD.bazel` files for this project. Simply do: +We use Gazelle to generate the `BUILD.bazel` files for this project. You'll notice for the `gazelle/` example, there are no `BUILD.bazel` files in there so simply do the following to create them: +```bash +bazel run //:gazelle ``` -# generates the BUILD files -bazel run gazelle -# executes the binary -bazel run //go/gazelle + +## grpc + +For the `grpc` examples, you'll need two terminals open. One to run the server: +```bash +bazel run //go/grpc/server +``` + +Another to act as the client: +```bash +bazel run //go/grpc/client -- --echo="hello" ``` diff --git a/projects/bzl7/go/grpc/BUILD.bazel b/projects/bzl7/go/grpc/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/projects/bzl7/go/grpc/client/BUILD.bazel b/projects/bzl7/go/grpc/client/BUILD.bazel new file mode 100644 index 0000000..9e90e7b --- /dev/null +++ b/projects/bzl7/go/grpc/client/BUILD.bazel @@ -0,0 +1,19 @@ +load("@rules_go//go:def.bzl", "go_binary", "go_library") + +go_library( + name = "client_lib", + srcs = ["main.go"], + importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/client", + visibility = ["//visibility:private"], + deps = [ + "//go/grpc/proto:grpc", + "@org_golang_google_grpc//:go_default_library", + "@org_golang_google_grpc//credentials/insecure", + ], +) + +go_binary( + name = "client", + embed = [":client_lib"], + visibility = ["//visibility:public"], +) diff --git a/projects/bzl7/go/grpc/client/main.go b/projects/bzl7/go/grpc/client/main.go new file mode 100644 index 0000000..31f5df5 --- /dev/null +++ b/projects/bzl7/go/grpc/client/main.go @@ -0,0 +1,44 @@ +// Package main implements a client for the Echoer service. +package main + +import ( + "context" + "flag" + "log" + "time" + + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + + pb "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto" +) + +var ( + address = flag.String("address", "localhost:50051", "connection address") + echo = flag.String("echo", "", "Echo") +) + +func main() { + flag.Parse() + + connection, err := grpc.Dial( + *address, + grpc.WithTransportCredentials( + insecure.NewCredentials(), + ), + ) + if err != nil { + log.Fatalln("error: ", err) + } + defer connection.Close() + + echoClient := pb.NewEchoerClient(connection) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + response, err := echoClient.SendEcho(ctx, &pb.EchoRequest{Request: *echo}) + if err != nil { + log.Fatalln("error: ", err) + } + log.Println("Response: ", response.GetMessage()) +} diff --git a/projects/bzl7/go/grpc/go.mod b/projects/bzl7/go/grpc/go.mod new file mode 100644 index 0000000..6969b1a --- /dev/null +++ b/projects/bzl7/go/grpc/go.mod @@ -0,0 +1,13 @@ +module github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc + +go 1.22.2 + +require google.golang.org/grpc v1.63.2 + +require ( + golang.org/x/net v0.21.0 // indirect + golang.org/x/sys v0.17.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect + google.golang.org/protobuf v1.33.0 // indirect +) diff --git a/projects/bzl7/go/grpc/go.sum b/projects/bzl7/go/grpc/go.sum new file mode 100644 index 0000000..875983f --- /dev/null +++ b/projects/bzl7/go/grpc/go.sum @@ -0,0 +1,13 @@ +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY= +google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= +google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= diff --git a/projects/bzl7/go/grpc/proto/BUILD.bazel b/projects/bzl7/go/grpc/proto/BUILD.bazel new file mode 100644 index 0000000..c0f7482 --- /dev/null +++ b/projects/bzl7/go/grpc/proto/BUILD.bazel @@ -0,0 +1,24 @@ +load("@rules_go//go:def.bzl", "go_library") +load("@rules_go//proto:def.bzl", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "grpc_proto", + srcs = ["hello.proto"], + visibility = ["//visibility:public"], +) + +go_proto_library( + name = "grpc_go_proto", + compilers = ["@rules_go//proto:go_grpc"], + importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto", + proto = ":grpc_proto", + visibility = ["//visibility:public"], +) + +go_library( + name = "grpc", + embed = [":grpc_go_proto"], + importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto", + visibility = ["//visibility:public"], +) diff --git a/projects/bzl7/go/grpc/proto/hello.proto b/projects/bzl7/go/grpc/proto/hello.proto new file mode 100644 index 0000000..0798648 --- /dev/null +++ b/projects/bzl7/go/grpc/proto/hello.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option go_package = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc"; + +package echo; + +// The echo service definition. +service Echoer { + // Sends an echo request. + rpc SendEcho (EchoRequest) returns (EchoReply) {} +} + +// The request echo. +message EchoRequest { + string request = 1; +} + +// The response message containing the echo +message EchoReply { + string message = 1; +} diff --git a/projects/bzl7/go/grpc/server/BUILD.bazel b/projects/bzl7/go/grpc/server/BUILD.bazel new file mode 100644 index 0000000..bed1751 --- /dev/null +++ b/projects/bzl7/go/grpc/server/BUILD.bazel @@ -0,0 +1,18 @@ +load("@rules_go//go:def.bzl", "go_binary", "go_library") + +go_library( + name = "server_lib", + srcs = ["main.go"], + importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/server", + visibility = ["//visibility:private"], + deps = [ + "//go/grpc/proto:grpc", + "@org_golang_google_grpc//:go_default_library", + ], +) + +go_binary( + name = "server", + embed = [":server_lib"], + visibility = ["//visibility:public"], +) diff --git a/projects/bzl7/go/grpc/server/main.go b/projects/bzl7/go/grpc/server/main.go new file mode 100644 index 0000000..87aec26 --- /dev/null +++ b/projects/bzl7/go/grpc/server/main.go @@ -0,0 +1,45 @@ +// Package main implements a server for the Echoer service. +package main + +import ( + "context" + "flag" + "fmt" + "log" + "net" + + pb "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto" + "google.golang.org/grpc" +) + +var ( + port = flag.Int("port", 50051, "The server port") +) + +type server struct { + pb.UnimplementedEchoerServer +} + +// SendEcho implements echo.EchoerServer +func (s *server) SendEcho(ctx context.Context, in *pb.EchoRequest) (*pb.EchoReply, error) { + request := in.GetRequest() + log.Printf("Received: %v", request) + return &pb.EchoReply{Message: request}, nil +} + +func main() { + flag.Parse() + + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + + grpcServer := grpc.NewServer() + pb.RegisterEchoerServer(grpcServer, &server{}) + log.Printf("server listening at %v", listener.Addr()) + + if err := grpcServer.Serve(listener); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/projects/bzl7/platforms/BUILD.bazel b/projects/bzl7/platforms/BUILD.bazel index 5bd59a9..adb41aa 100644 --- a/projects/bzl7/platforms/BUILD.bazel +++ b/projects/bzl7/platforms/BUILD.bazel @@ -3,27 +3,39 @@ package(default_visibility = ["//visibility:public"]) platform( name = "gcc_10", constraint_values = [ - "//platforms/cpp:gcc_10" + "//platforms/cpp:gcc_10", + ], + parents = [ + "@rules_nixpkgs_core//platforms:host", ], ) platform( name = "gcc_11", constraint_values = [ - "//platforms/cpp:gcc_11" + "//platforms/cpp:gcc_11", + ], + parents = [ + "@rules_nixpkgs_core//platforms:host", ], ) platform( name = "gcc_12", constraint_values = [ - "//platforms/cpp:gcc_12" + "//platforms/cpp:gcc_12", + ], + parents = [ + "@rules_nixpkgs_core//platforms:host", ], ) platform( name = "gcc_13", constraint_values = [ - "//platforms/cpp:gcc_13" + "//platforms/cpp:gcc_13", + ], + parents = [ + "@rules_nixpkgs_core//platforms:host", ], )