Skip to content

bwolf1/grpc-rest-service

Repository files navigation

grpc-rest-service

Containerized gRPC service with a REST gateway

Goals

  • Learn how to set up gRPC-gateway
  • Learn how to set up branch restrictions and automated test runs
  • Have a template to use for something else

Generating the protobuf code

protoc \
-I. \
-I  $GOPATH/pkg/mod/github.com/grpc-ecosystem/[email protected]/third_party/googleapis/ \
--go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
--grpc-gateway_out=logtostderr=true:. --grpc-gateway_opt=paths=source_relative \
proto/service.proto

Running the server natively

go run main.go grpcServer

Running the gRPC client natively

go run main.go grpcClient --word abcdef

Example gRPC request and response

➜ go run main.go grpcClient --word abcdef
2022/10/19 15:49:43 echo:"abcdef" timestamp:"2022-10-19 20:49:43.293029 +0000 UTC"

Running the REST proxy natively

go run main.go restProxy

Example REST request and response

➜ curl http://localhost:8080/echo/abcdef
{"echo":"abcdef","timestamp":"2022-10-20 22:26:46.416726 +0000 UTC"}%

Using JQ to make the response more readable

➜ curl http://localhost:8080/echo/abcdefg | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    69  100    69    0     0   5978      0 --:--:-- --:--:-- --:--:-- 69000
{
  "echo": "abcdefg",
  "timestamp": "2022-10-20 22:29:21.706632 +0000 UTC"
}

Running the gRPC server and REST proxy in Docker with docker-compose

docker-compose up --force-recreate --remove-orphans --build

Example REST request and response using Docker

✗ curl http://localhost:8080/echo/abcdefg
{"echo":"abcdefg","timestamp":"2023-06-06 20:29:06.774947963 +0000 UTC"}%

Using JQ to make the response more readable with Docker

✗ curl http://localhost:8080/echo/abcdefg | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    72  100    72    0     0   4489      0 --:--:-- --:--:-- --:--:--  7200
{
  "echo": "abcdefg",
  "timestamp": "2023-06-06 20:25:26.109105521 +0000 UTC"
}

About

Containerized gRPC service with a REST gateway

Resources

Stars

Watchers

Forks

Packages

No packages published