networking/grpc-gke-nlb-tutorial/reverse-grpc/api/reverse.go (28 lines of code) (raw):

// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative reverse.proto package api import ( "context" "log" "os" "google.golang.org/grpc" "google.golang.org/grpc/metadata" ) // Server for the Reverse gRPC API type Server struct { UnimplementedReverseServer } // Reverse the content of the request func (s *Server) Reverse(ctx context.Context, in *ReverseRequest) (*ReverseResponse, error) { log.Printf("Handling Reverse request [%v] with context %v", in, ctx) hostname, err := os.Hostname() if err != nil { log.Printf("Unable to get hostname %v", err) hostname = "" } grpc.SendHeader(ctx, metadata.Pairs("hostname", hostname)) return &ReverseResponse{Content: reverse(in.Content)}, nil } func reverse(input string) string { runes := []rune(input) for i, j := 0, len(runes)-1; i < len(runes)/2; i, j = i+1, j-1 { runes[i], runes[j] = runes[j], runes[i] } return string(runes) }