Skip to content

Commit 24edf64

Browse files
authored
Add Echo example (#109)
1 parent 9adc381 commit 24edf64

18 files changed

Lines changed: 902 additions & 0 deletions

File tree

examples/echo/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
common --enable_bzlmod --experimental_proto_descriptor_sets_include_source_info

examples/echo/BUILD.bazel

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2021-2025 Buf Technologies, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@gazelle//:def.bzl", "gazelle", "gazelle_binary")
16+
load("@rules_buf//buf:defs.bzl", "buf_format")
17+
18+
exports_files(
19+
["buf.yaml"],
20+
visibility = ["//visibility:public"],
21+
)
22+
23+
gazelle(
24+
name = "gazelle",
25+
gazelle = ":gazelle-buf",
26+
)
27+
28+
# gazelle:prefix echo
29+
# gazelle:go_generate_proto false
30+
gazelle_binary(
31+
name = "gazelle-buf",
32+
languages = [
33+
"@gazelle//language/proto", # Built-in rule from gazelle for Protos.
34+
# Any languages that depend on Gazelle's proto plugin must come after it.
35+
"@rules_buf//gazelle/buf:buf", # Loads the Buf extension
36+
"@gazelle//language/go",
37+
],
38+
visibility = ["//visibility:public"],
39+
)
40+
41+
buf_format(
42+
name = "buf_format",
43+
)

examples/echo/MODULE.bazel

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2021-2025 Buf Technologies, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"Bazel dependencies"
16+
17+
bazel_dep(name = "rules_buf", version = "0.0.0", dev_dependency = True)
18+
bazel_dep(name = "gazelle", version = "0.36.0", dev_dependency = True)
19+
bazel_dep(name = "protobuf", version = "29.1", repo_name = "com_google_protobuf")
20+
bazel_dep(name = "rules_proto", version = "7.0.2")
21+
bazel_dep(name = "rules_go", version = "0.54.1")
22+
23+
local_path_override(
24+
module_name = "rules_buf",
25+
path = "../..",
26+
)
27+
28+
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
29+
30+
go_sdk.from_file(go_mod = "//:go.mod")
31+
32+
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
33+
go_deps.from_file(go_mod = "//:go.mod")
34+
35+
36+
use_repo(
37+
go_deps,
38+
"org_golang_google_protobuf",
39+
"com_connectrpc_connect",
40+
"org_golang_x_net"
41+
)
42+
43+
buf = use_extension("@rules_buf//buf:extensions.bzl", "buf")
44+
45+
# Override the default version of buf
46+
buf.toolchains(version = "v1.47.2", sha256 = "1b37b75dc0a777a0cba17fa2604bc9906e55bb4c578823d8b7a8fe3fc9fe4439")
47+
48+
# Allow people to use `bazel run @rules_buf_toolchains//:buf -- --version`
49+
use_repo(buf, "rules_buf_toolchains")

examples/echo/MODULE.bazel.lock

Lines changed: 261 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/echo/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Echo
2+
3+
A minimal but complete example of using buf with bazel via rules_buf.
4+
5+
6+
## Generation
7+
8+
We generate the files using `buf` directly, this lets us use remote plugins and managed mode. We can do so from within bazel:
9+
10+
```sh
11+
bazel run @rules_buf_toolchains//:buf -- generate
12+
```
13+
14+
Everytime we generate the files we can run, gazelle to auto generate the BUILD files:
15+
16+
```sh
17+
bazel run //:gazelle
18+
```
19+
20+
This will generate the language rules based on the configured gazelle plugins.
21+
22+
## Running the server
23+
24+
The server can be started using `bazel run //cmd/echo:echo` and we can use `buf curl` to test:
25+
26+
```sh
27+
buf curl http://localhost:8080/echo.v1.EchoService/Echo --schema
28+
```

examples/echo/WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Marker that this folder is the root of a Bazel workspace

examples/echo/buf.gen.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: v2
2+
clean: true
3+
managed:
4+
enabled: true
5+
override:
6+
- file_option: go_package_prefix
7+
value: echo/gen
8+
plugins:
9+
- remote: buf.build/protocolbuffers/go:v1.36.6
10+
out: gen
11+
opt:
12+
- paths=source_relative
13+
- remote: buf.build/connectrpc/go:v1.18.1
14+
out: gen
15+
opt:
16+
- paths=source_relative

examples/echo/buf.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: v2
2+
modules:
3+
- path: proto
4+
lint:
5+
use:
6+
- STANDARD
7+
breaking:
8+
use:
9+
- FILE

examples/echo/cmd/echo/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@rules_go//go:def.bzl", "go_binary", "go_library")
2+
3+
go_library(
4+
name = "echo_lib",
5+
srcs = ["main.go"],
6+
importpath = "echo/cmd/echo",
7+
visibility = ["//visibility:private"],
8+
deps = [
9+
"//gen/echo/v1:echo",
10+
"//gen/echo/v1/echov1connect",
11+
"@com_connectrpc_connect//:connect",
12+
"@org_golang_x_net//http2",
13+
"@org_golang_x_net//http2/h2c",
14+
],
15+
)
16+
17+
go_binary(
18+
name = "echo",
19+
embed = [":echo_lib"],
20+
visibility = ["//visibility:public"],
21+
)

examples/echo/cmd/echo/main.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"echo/gen/echo/v1"
6+
"echo/gen/echo/v1/echov1connect"
7+
"errors"
8+
"log"
9+
"net/http"
10+
"os"
11+
"os/signal"
12+
"syscall"
13+
"time"
14+
15+
"connectrpc.com/connect"
16+
"golang.org/x/net/http2"
17+
"golang.org/x/net/http2/h2c"
18+
)
19+
20+
var _ echov1connect.EchoServiceHandler = (*echoServer)(nil)
21+
22+
type echoServer struct {
23+
}
24+
25+
// Echo implements echov1connect.EchoServiceHandler.
26+
func (e *echoServer) Echo(ctx context.Context, req *connect.Request[echov1.EchoRequest]) (*connect.Response[echov1.EchoResponse], error) {
27+
return connect.NewResponse(&echov1.EchoResponse{
28+
Message: req.Msg.GetMessage(),
29+
}), nil
30+
}
31+
32+
func main() {
33+
mux := http.NewServeMux()
34+
mux.Handle(echov1connect.NewEchoServiceHandler(&echoServer{}))
35+
addr := "localhost:8080"
36+
if port := os.Getenv("PORT"); port != "" {
37+
addr = ":" + port
38+
}
39+
srv := &http.Server{
40+
Addr: addr,
41+
Handler: h2c.NewHandler(
42+
mux,
43+
&http2.Server{},
44+
),
45+
ReadHeaderTimeout: time.Second,
46+
ReadTimeout: 5 * time.Minute,
47+
WriteTimeout: 5 * time.Minute,
48+
MaxHeaderBytes: 8 * 1024, // 8KiB
49+
}
50+
signals := make(chan os.Signal, 1)
51+
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
52+
go func() {
53+
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
54+
log.Fatalf("HTTP listen and serve: %v", err)
55+
}
56+
}()
57+
58+
<-signals
59+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
60+
defer cancel()
61+
if err := srv.Shutdown(ctx); err != nil {
62+
log.Fatalf("HTTP shutdown: %v", err) //nolint:gocritic
63+
}
64+
}

0 commit comments

Comments
 (0)