Brijesh's Git Server — trying-grpc @ 7cf3ce98e6bd56af5f66a0807211d2e021d295ca

Trying to use GRPC in go using twitchs twirp packge

internal/haberdasherserver/server.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
package haberdasherserver

import (
	"context"
	"math/rand"

	"github.com/twitchtv/twirp"
	pb "github.com/wbrijesh/trying-trpc/rpc/haberdasher"
)

// Server implements the Haberdasher service
type Server struct{}

func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err error) {
	if size.Inches <= 0 {
		return nil, twirp.InvalidArgumentError("inches", "I can't make a hat that small!")
	}
	return &pb.Hat{
		Inches: size.Inches,
		Color:  []string{"white", "black", "brown", "red", "blue"}[rand.Intn(5)],
		Name:   []string{"bowler", "baseball cap", "top hat", "derby"}[rand.Intn(4)],
	}, nil
}