github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/server/grpc/options.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package grpc
    18  
    19  import (
    20  	"github.com/google/uuid"
    21  	"github.com/improbable-eng/grpc-web/go/grpcweb"
    22  	"google.golang.org/grpc"
    23  
    24  	"crypto/tls"
    25  	"time"
    26  )
    27  
    28  const (
    29  	defaultPort             = 9000
    30  	defaultName             = "go.toolkit.server"
    31  	defaultRegisterInterval = time.Second * 30
    32  	defaultRegisterTTL      = time.Second * 90
    33  )
    34  
    35  type Config struct {
    36  	ID   string
    37  	Name string `env:"GRPC_SERVER_NAME" envDefault:"" comment:"Set GRPC server name"`
    38  
    39  	Host string `env:"GRPC_SERVER_LISTEN" envDefault:"0.0.0.0" comment:"Set GRPC server listen host"`
    40  	Port int    `env:"GRPC_SERVER_PORT" envDefault:"9000" comment:"Set GRPC server listen port"`
    41  
    42  	MaxConnSize    int `env:"GRPC_SERVER_MAX_CONNECTION_SIZE"   comment:"Sets the max simultaneous connections for server (default unlimited)"`
    43  	MaxRecvMsgSize int `env:"GRPC_SERVER_MAX_RECEIVE_MESSAGE_SIZE" envDefault:"16777216" comment:"Sets the max message size in bytes the server can receive (default 16 MB)"`
    44  	MaxSendMsgSize int `env:"GRPC_SERVER_MAX_SEND_MESSAGE_SIZE" envDefault:"16777216" comment:"Sets the max message size in bytes the server can send (default 16 MB)"`
    45  
    46  	IsDisable bool `env:"GRPC_SERVER_DISABLED" envDefault:"false" comment:"GRPC server disable (default: false)"`
    47  
    48  	GrpcOptions []grpc.ServerOption `env:"GRPC_SERVER_OPTIONS" envSeparator:"," comment:"Set GRPC server additional options (key=value,key2=value2)"`
    49  	TLSConfig   *tls.Config
    50  
    51  	GRPCWebHost string `env:"GRPC_WEB_SERVER_LISTEN" envDefault:"0.0.0.0" comment:"Set GRPC WEB server listen host"`
    52  	GRPCWebPort int    `env:"GRPC_WEB_SERVER_PORT" comment:"Set GRPC WEB server listen host"`
    53  
    54  	GrpcWebOptions []grpcweb.Option
    55  
    56  	RegisterInterval time.Duration
    57  	RegisterTTL      time.Duration
    58  }
    59  
    60  func defaultOptions() Config {
    61  	return Config{
    62  		ID:               uuid.New().String(),
    63  		Name:             defaultName,
    64  		Port:             defaultPort,
    65  		RegisterInterval: defaultRegisterInterval,
    66  		RegisterTTL:      defaultRegisterTTL,
    67  	}
    68  }