Skip to content

Commit aec36d2

Browse files
finn-qarikFinn Ball
authored andcommitted
feat: add grpc example (#66)
1 parent 2eac036 commit aec36d2

File tree

14 files changed

+244
-10
lines changed

14 files changed

+244
-10
lines changed

projects/bzl7/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
build --incompatible_enable_cc_toolchain_resolution
22
build --incompatible_strict_action_env
33

4+
build --host_cxxopt=-std=c++17
5+
46
build --host_platform=@rules_nixpkgs_core//platforms:host
57

68
build:gcc10 --platforms=//platforms:gcc_10

projects/bzl7/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@gazelle//:def.bzl", "gazelle")
44
gazelle(
55
name = "gazelle",
66
args = [
7-
"go/gazelle"
7+
"go/gazelle",
8+
"go/grpc",
89
],
910
)

projects/bzl7/MODULE.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ RULES_NIX_PKGS_VERSION = "0.11.1"
66

77
bazel_dep(name = "aspect_rules_py", version = "0.7.1")
88
bazel_dep(name = "gazelle", version = "0.36.0")
9+
bazel_dep(name = "grpc", version = "1.62.1")
910
bazel_dep(name = "rules_cc", version = "0.0.9")
1011
bazel_dep(name = "rules_go", version = "0.46.0")
1112
bazel_dep(name = "rules_nixpkgs_cc")
1213
bazel_dep(name = "rules_nixpkgs_core", version = RULES_NIX_PKGS_VERSION)
14+
bazel_dep(name = "rules_proto", version = "6.0.0-rc3")
1315
bazel_dep(name = "rules_python", version = "0.31.0")
1416
bazel_dep(name = "platforms", version = "0.0.9")
17+
bazel_dep(name = "protobuf", version = "26.0.bcr.1")
1518

1619
# Not yet available in the bazel central registry.
1720
archive_override(
@@ -101,3 +104,13 @@ pip.parse(
101104
requirements_lock = "@bzl7//python/pip:requirements_lock.txt",
102105
)
103106
use_repo(pip, "pip_example_3_12")
107+
108+
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
109+
go_sdk.download(version = "1.22.2")
110+
111+
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
112+
go_deps.from_file(go_mod = "//go/grpc:go.mod")
113+
use_repo(
114+
go_deps,
115+
"org_golang_google_grpc",
116+
)

projects/bzl7/go/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# go
22

3-
We use Gazelle to generate the `BUILD.bazel` files for this project. Simply do:
3+
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:
4+
```bash
5+
bazel run //:gazelle
46
```
5-
# generates the BUILD files
6-
bazel run gazelle
7-
# executes the binary
8-
bazel run //go/gazelle
7+
8+
## grpc
9+
10+
For the `grpc` examples, you'll need two terminals open. One to run the server:
11+
```bash
12+
bazel run //go/grpc/server
13+
```
14+
15+
Another to act as the client:
16+
```bash
17+
bazel run //go/grpc/client -- --echo="hello"
918
```

projects/bzl7/go/grpc/BUILD.bazel

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("@rules_go//go:def.bzl", "go_binary", "go_library")
2+
3+
go_library(
4+
name = "client_lib",
5+
srcs = ["main.go"],
6+
importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/client",
7+
visibility = ["//visibility:private"],
8+
deps = [
9+
"//go/grpc/proto:grpc",
10+
"@org_golang_google_grpc//:go_default_library",
11+
"@org_golang_google_grpc//credentials/insecure",
12+
],
13+
)
14+
15+
go_binary(
16+
name = "client",
17+
embed = [":client_lib"],
18+
visibility = ["//visibility:public"],
19+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Package main implements a client for the Echoer service.
2+
package main
3+
4+
import (
5+
"context"
6+
"flag"
7+
"log"
8+
"time"
9+
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/credentials/insecure"
12+
13+
pb "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto"
14+
)
15+
16+
var (
17+
address = flag.String("address", "localhost:50051", "connection address")
18+
echo = flag.String("echo", "", "Echo")
19+
)
20+
21+
func main() {
22+
flag.Parse()
23+
24+
connection, err := grpc.Dial(
25+
*address,
26+
grpc.WithTransportCredentials(
27+
insecure.NewCredentials(),
28+
),
29+
)
30+
if err != nil {
31+
log.Fatalln("error: ", err)
32+
}
33+
defer connection.Close()
34+
35+
echoClient := pb.NewEchoerClient(connection)
36+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
37+
defer cancel()
38+
39+
response, err := echoClient.SendEcho(ctx, &pb.EchoRequest{Request: *echo})
40+
if err != nil {
41+
log.Fatalln("error: ", err)
42+
}
43+
log.Println("Response: ", response.GetMessage())
44+
}

projects/bzl7/go/grpc/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc
2+
3+
go 1.22.2
4+
5+
require google.golang.org/grpc v1.63.2
6+
7+
require (
8+
golang.org/x/net v0.21.0 // indirect
9+
golang.org/x/sys v0.17.0 // indirect
10+
golang.org/x/text v0.14.0 // indirect
11+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
12+
google.golang.org/protobuf v1.33.0 // indirect
13+
)

projects/bzl7/go/grpc/go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
2+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
3+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
4+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
6+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
7+
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY=
8+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
9+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
10+
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
11+
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
12+
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
13+
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("@rules_go//go:def.bzl", "go_library")
2+
load("@rules_go//proto:def.bzl", "go_proto_library")
3+
load("@rules_proto//proto:defs.bzl", "proto_library")
4+
5+
proto_library(
6+
name = "grpc_proto",
7+
srcs = ["echo.proto"],
8+
visibility = ["//visibility:public"],
9+
)
10+
11+
go_proto_library(
12+
name = "grpc_go_proto",
13+
compilers = ["@rules_go//proto:go_grpc"],
14+
importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto",
15+
proto = ":grpc_proto",
16+
visibility = ["//visibility:public"],
17+
)
18+
19+
go_library(
20+
name = "grpc",
21+
embed = [":grpc_go_proto"],
22+
importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto",
23+
visibility = ["//visibility:public"],
24+
)

0 commit comments

Comments
 (0)