Skip to content

Commit

Permalink
feat: add grpc example
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-qarik authored and Finn Ball committed Apr 19, 2024
1 parent 2eac036 commit d966af4
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 10 deletions.
2 changes: 2 additions & 0 deletions projects/bzl7/.bazelrc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion projects/bzl7/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@gazelle//:def.bzl", "gazelle")
gazelle(
name = "gazelle",
args = [
"go/gazelle"
"go/gazelle",
"go/grpc",
],
)
13 changes: 13 additions & 0 deletions projects/bzl7/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
)
19 changes: 14 additions & 5 deletions projects/bzl7/go/README.md
Original file line number Diff line number Diff line change
@@ -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"
```
Empty file.
19 changes: 19 additions & 0 deletions projects/bzl7/go/grpc/client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
44 changes: 44 additions & 0 deletions projects/bzl7/go/grpc/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Package main implements a client for Greeter 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())
}
13 changes: 13 additions & 0 deletions projects/bzl7/go/grpc/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
13 changes: 13 additions & 0 deletions projects/bzl7/go/grpc/go.sum
Original file line number Diff line number Diff line change
@@ -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=
24 changes: 24 additions & 0 deletions projects/bzl7/go/grpc/proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
21 changes: 21 additions & 0 deletions projects/bzl7/go/grpc/proto/hello.proto
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions projects/bzl7/go/grpc/server/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
45 changes: 45 additions & 0 deletions projects/bzl7/go/grpc/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Package main implements a server for Greeter 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)
}
}
20 changes: 16 additions & 4 deletions projects/bzl7/platforms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

0 comments on commit d966af4

Please sign in to comment.