Skip to content

Instantly share code, notes, and snippets.

@amitz
Last active December 14, 2021 16:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitz/0ef01b22df39c4a8156bab58541bdeab to your computer and use it in GitHub Desktop.
Save amitz/0ef01b22df39c4a8156bab58541bdeab to your computer and use it in GitHub Desktop.
Building gRPC Gateway Binary Using Bazel using 'grpc-gateway'
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
##########
# Common #
##########
proto_library(
name = "project_proto",
srcs = [
"project.proto",
],
deps = [
"@com_github_googleapis_googleapis//google/api:annotations_proto",
],
visibility = ["//visibility:public"],
)
######
# Go #
######
go_proto_library(
name = "project_go_proto",
compilers = [
"@io_bazel_rules_go//proto:go_grpc",
"@com_github_grpc_ecosystem_grpc_gateway//protoc-gen-grpc-gateway:go_gen_grpc_gateway",
],
importpath = "github.com/user/project",
proto = ":project_proto",
deps = [
"@com_github_googleapis_googleapis//google/api:annotations_go_proto",
"@com_github_golang_protobuf//descriptor:go_default_library_gen",
],
)
go_binary(
name = "project_grpc_gateway",
srcs = [
"main.go",
],
visibility = ["//visibility:public"],
deps = [
"@com_github_grpc_ecosystem_grpc_gateway//runtime:go_default_library",
":project_go_proto",
],
)
http_archive(
name = "io_bazel_rules_go",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.19.3/rules_go-0.19.3.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/0.19.3/rules_go-0.19.3.tar.gz",
],
sha256 = "313f2c7a23fecc33023563f082f381a32b9b7254f727a7dd2d6380ccc6dfe09b",
)
grpc_gateway_version = "9d0c596203d5ceb50d4071f4f67020873e60d17a" # Change it to the Commit you would like to work with.
http_archive(
name = "com_github_grpc_ecosystem_grpc_gateway",
url = "https://github.com/grpc-ecosystem/grpc-gateway/archive/%s.tar.gz" % grpc_gateway_version,
strip_prefix = "grpc-gateway-%s" % grpc_gateway_version,
)
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@com_github_grpc_ecosystem_grpc_gateway//:repositories.bzl", "go_repositories")
go_repositories()
@Kernald
Copy link

Kernald commented Aug 17, 2021

As someone who doesn't know the first thing about Go but was hoping to get a gateway running for services written in another language, what would the main.go look like?

@amitz
Copy link
Author

amitz commented Aug 18, 2021

There's a minimal example here:
https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/creating_main.go/

I suggest checking all the "Tutorial" section, as this will give the main.go file more context. Also note this gist is a bit out-dated. The BUILD file is probably the same, but the WORKSPACE can be update as gRPC Gateway now running v2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment