grpc.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package options
  2. import (
  3. "fmt"
  4. "github.com/spf13/pflag"
  5. )
  6. // GRPCOptions grpc基础配置
  7. type GRPCOptions struct {
  8. BindAddress string `json:"bind-address" mapstructure:"bind-address"`
  9. BindPort int `json:"bind-port" mapstructure:"bind-port"`
  10. MaxMsgSize int `json:"max-msg-size" mapstructure:"max-msg-size"`
  11. }
  12. func NewGRPCOptions() *GRPCOptions {
  13. return &GRPCOptions{
  14. BindAddress: "0.0.0.0",
  15. BindPort: 8081,
  16. MaxMsgSize: 4 * 1024 * 1024,
  17. }
  18. }
  19. func (s *GRPCOptions) Validate() []error {
  20. var errors []error
  21. if s.BindPort < 0 || s.BindPort > 65535 {
  22. errors = append(
  23. errors,
  24. fmt.Errorf(
  25. "--insecure-port %v must be between 0 and 65535, inclusive. 0 for turning off insecure (HTTP) port",
  26. s.BindPort,
  27. ),
  28. )
  29. }
  30. return errors
  31. }
  32. func (s *GRPCOptions) AddFlags(fs *pflag.FlagSet) {
  33. fs.StringVar(&s.BindAddress, "grpc.bind-address", s.BindAddress, ""+
  34. "The IP address on which to serve the --grpc.bind-port(set to 0.0.0.0 for all IPv4 interfaces and :: for all IPv6 interfaces).")
  35. fs.IntVar(&s.BindPort, "grpc.bind-port", s.BindPort, ""+
  36. "The port on which to serve unsecured, unauthenticated grpc access. It is assumed "+
  37. "that firewall rules are set up such that this port is not reachable from outside of "+
  38. "the deployed machine and that port 443 on the iam public address is proxied to this "+
  39. "port. This is performed by nginx in the default setup. Set to zero to disable.")
  40. fs.IntVar(&s.MaxMsgSize, "grpc.max-msg-size", s.MaxMsgSize, "gRPC max message size.")
  41. }