Skip to content

Commit b7588d4

Browse files
finn-qarikFinn Ball
authored and
Finn Ball
committed
feat: add grpc example (#66)
1 parent 2eac036 commit b7588d4

File tree

14 files changed

+244
-10
lines changed

14 files changed

+244
-10
lines changed

projects/bzl7/.bazelrc

+2
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

+2-1
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

+13
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

+14-5
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.
+19
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+
)

projects/bzl7/go/grpc/client/main.go

+44
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

+13
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

+13
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=
+24
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+
)
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
syntax = "proto3";
2+
3+
option go_package = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc";
4+
5+
package echo;
6+
7+
// The echo service definition.
8+
service Echoer {
9+
// Sends an echo request.
10+
rpc SendEcho (EchoRequest) returns (EchoReply) {}
11+
}
12+
13+
// The request echo.
14+
message EchoRequest {
15+
string request = 1;
16+
}
17+
18+
// The response message containing the echo
19+
message EchoReply {
20+
string message = 1;
21+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@rules_go//go:def.bzl", "go_binary", "go_library")
2+
3+
go_library(
4+
name = "server_lib",
5+
srcs = ["main.go"],
6+
importpath = "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/server",
7+
visibility = ["//visibility:private"],
8+
deps = [
9+
"//go/grpc/proto:grpc",
10+
"@org_golang_google_grpc//:go_default_library",
11+
],
12+
)
13+
14+
go_binary(
15+
name = "server",
16+
embed = [":server_lib"],
17+
visibility = ["//visibility:public"],
18+
)

projects/bzl7/go/grpc/server/main.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Package main implements a server for the Echoer service.
2+
package main
3+
4+
import (
5+
"context"
6+
"flag"
7+
"fmt"
8+
"log"
9+
"net"
10+
11+
pb "github.com/Qarik-Group/disruptor/projects/bzl7/go/grpc/proto"
12+
"google.golang.org/grpc"
13+
)
14+
15+
var (
16+
port = flag.Int("port", 50051, "The server port")
17+
)
18+
19+
type server struct {
20+
pb.UnimplementedEchoerServer
21+
}
22+
23+
// SendEcho implements echo.EchoerServer
24+
func (s *server) SendEcho(ctx context.Context, in *pb.EchoRequest) (*pb.EchoReply, error) {
25+
request := in.GetRequest()
26+
log.Printf("Received: %v", request)
27+
return &pb.EchoReply{Message: request}, nil
28+
}
29+
30+
func main() {
31+
flag.Parse()
32+
33+
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
34+
if err != nil {
35+
log.Fatalf("failed to listen: %v", err)
36+
}
37+
38+
grpcServer := grpc.NewServer()
39+
pb.RegisterEchoerServer(grpcServer, &server{})
40+
log.Printf("server listening at %v", listener.Addr())
41+
42+
if err := grpcServer.Serve(listener); err != nil {
43+
log.Fatalf("failed to serve: %v", err)
44+
}
45+
}

projects/bzl7/platforms/BUILD.bazel

+16-4
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,39 @@ package(default_visibility = ["//visibility:public"])
33
platform(
44
name = "gcc_10",
55
constraint_values = [
6-
"//platforms/cpp:gcc_10"
6+
"//platforms/cpp:gcc_10",
7+
],
8+
parents = [
9+
"@rules_nixpkgs_core//platforms:host",
710
],
811
)
912

1013
platform(
1114
name = "gcc_11",
1215
constraint_values = [
13-
"//platforms/cpp:gcc_11"
16+
"//platforms/cpp:gcc_11",
17+
],
18+
parents = [
19+
"@rules_nixpkgs_core//platforms:host",
1420
],
1521
)
1622

1723
platform(
1824
name = "gcc_12",
1925
constraint_values = [
20-
"//platforms/cpp:gcc_12"
26+
"//platforms/cpp:gcc_12",
27+
],
28+
parents = [
29+
"@rules_nixpkgs_core//platforms:host",
2130
],
2231
)
2332

2433
platform(
2534
name = "gcc_13",
2635
constraint_values = [
27-
"//platforms/cpp:gcc_13"
36+
"//platforms/cpp:gcc_13",
37+
],
38+
parents = [
39+
"@rules_nixpkgs_core//platforms:host",
2840
],
2941
)

0 commit comments

Comments
 (0)