Skip to content

Commit

Permalink
Return metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-jones committed Dec 6, 2024
1 parent da60a6c commit 3aed1a5
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 30 deletions.
6 changes: 6 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"os"
"os/signal"
"strings"
"syscall"

_ "github.com/joho/godotenv/autoload"
Expand Down Expand Up @@ -119,5 +120,10 @@ func run(ctx context.Context, cfg config, message string) error {

fmt.Printf("response from server: %s\n", res.Message)

fmt.Println("metadata from server:")
for _, item := range res.Metadata.Items {
fmt.Printf("%s: %s\n", item.Key, strings.Join(item.Values, ", "))
}

return nil
}
23 changes: 22 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

echov1 "github.com/e-flux-platform/echo-grpc/gen/go/road/echo/v1"
)
Expand Down Expand Up @@ -66,5 +69,23 @@ func run(ctx context.Context, listenAddr string) error {
type server struct{}

func (s *server) Echo(ctx context.Context, request *echov1.EchoRequest) (*echov1.EchoResponse, error) {
return &echov1.EchoResponse{Message: request.Message}, nil
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.DataLoss, "failed to get metadata")
}

items := make([]*echov1.MetadataItem, 0, len(md))
for key, values := range md {
items = append(items, &echov1.MetadataItem{
Key: key,
Values: values,
})
}

return &echov1.EchoResponse{
Message: request.Message,
Metadata: &echov1.Metadata{
Items: items,
},
}, nil
}
179 changes: 150 additions & 29 deletions gen/go/road/echo/v1/echo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions proto/road/echo/v1/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ message EchoRequest {

message EchoResponse {
string message = 1;
Metadata metadata = 2;
}

message Metadata {
repeated MetadataItem items = 1;
}

message MetadataItem {
string key = 1;
repeated string values = 2;
}

0 comments on commit 3aed1a5

Please sign in to comment.