github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/helloworld/helloworld.go (about)

     1  // Copyright 2018-2021 CERN
     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  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package helloworld
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  
    25  	"github.com/cs3org/reva/v2/internal/grpc/services/helloworld/proto"
    26  	"github.com/cs3org/reva/v2/pkg/rgrpc"
    27  	"github.com/mitchellh/mapstructure"
    28  	"github.com/pkg/errors"
    29  	"github.com/rs/zerolog"
    30  	"google.golang.org/grpc"
    31  )
    32  
    33  func init() {
    34  	rgrpc.Register("helloworld", New)
    35  }
    36  
    37  type conf struct {
    38  	Message string `mapstructure:"message"`
    39  }
    40  type service struct {
    41  	conf *conf
    42  }
    43  
    44  // New returns a new PreferencesServiceServer
    45  // It can be tested like this:
    46  // prototool grpc --address 0.0.0.0:9999 --method 'revad.helloworld.HelloWorldService/Hello' --data '{"name": "Alice"}'
    47  func New(m map[string]interface{}, ss *grpc.Server, _ *zerolog.Logger) (rgrpc.Service, error) {
    48  	c := &conf{}
    49  	if err := mapstructure.Decode(m, c); err != nil {
    50  		err = errors.Wrap(err, "helloworld: error decoding conf")
    51  		return nil, err
    52  	}
    53  
    54  	if c.Message == "" {
    55  		c.Message = "Hello"
    56  	}
    57  	service := &service{conf: c}
    58  	return service, nil
    59  }
    60  
    61  func (s *service) Close() error {
    62  	return nil
    63  }
    64  
    65  func (s *service) UnprotectedEndpoints() []string {
    66  	return []string{}
    67  }
    68  
    69  func (s *service) Register(ss *grpc.Server) {
    70  	proto.RegisterHelloWorldServiceServer(ss, s)
    71  }
    72  
    73  func (s *service) Hello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloResponse, error) {
    74  	if req.Name == "" {
    75  		req.Name = "Mr. Nobody"
    76  	}
    77  	message := fmt.Sprintf("%s %s", s.conf.Message, req.Name)
    78  	res := &proto.HelloResponse{
    79  		Message: message,
    80  	}
    81  	return res, nil
    82  }