code.vegaprotocol.io/vega@v0.79.0/blockexplorer/api/config.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package api
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/blockexplorer/api/grpc"
    20  	"code.vegaprotocol.io/vega/libs/config/encoding"
    21  	libhttp "code.vegaprotocol.io/vega/libs/http"
    22  	"code.vegaprotocol.io/vega/logging"
    23  
    24  	"github.com/inhies/go-bytesize"
    25  )
    26  
    27  var (
    28  	portalNamedLogger  = "api.portal"
    29  	gatewayNamedLogger = "api.gateway"
    30  	restNamedLogger    = "api.rest"
    31  	grpcUINamedLogger  = "api.grpc-ui"
    32  )
    33  
    34  type Config struct {
    35  	GRPC          grpc.Config   `group:"grpc api"                                           namespace:"grpc"`
    36  	GRPCUI        GRPCUIConfig  `group:"grpc web ui"                                        namespace:"grpcui"`
    37  	REST          RESTConfig    `group:"rest api"                                           namespace:"rest"`
    38  	Gateway       GatewayConfig `group:"gateway"                                            namespace:"grpcui"`
    39  	ListenAddress string        `description:"the IP address that our sever will listen on" long:"listen-address"`
    40  	ListenPort    uint16        `description:"the port that our sever will listen on"       long:"listen-port"`
    41  }
    42  
    43  func NewDefaultConfig() Config {
    44  	return Config{
    45  		GRPC:          grpc.NewDefaultConfig(),
    46  		GRPCUI:        NewDefaultGRPCUIConfig(),
    47  		REST:          NewDefaultRESTConfig(),
    48  		Gateway:       NewDefaultGatewayConfig(),
    49  		ListenAddress: "0.0.0.0",
    50  		ListenPort:    1515,
    51  	}
    52  }
    53  
    54  type GRPCUIConfig struct {
    55  	Enabled        encoding.Bool     `long:"enabled"`
    56  	Endpoint       string            `long:"endpoint"`
    57  	Level          encoding.LogLevel `choice:"debug"                                                                                 choice:"info"           choice:"warning" long:"log-level"`
    58  	MaxPayloadSize encoding.ByteSize `description:"Maximum size of GRPC messages the UI will accept from the GRPC server (e.g. 4mb)" long:"max-payload-size"`
    59  }
    60  
    61  func NewDefaultGRPCUIConfig() GRPCUIConfig {
    62  	return GRPCUIConfig{
    63  		Enabled:        encoding.Bool(true),
    64  		Endpoint:       "/grpc",
    65  		Level:          encoding.LogLevel{Level: logging.InfoLevel},
    66  		MaxPayloadSize: encoding.ByteSize(4 * bytesize.MB),
    67  	}
    68  }
    69  
    70  type GatewayConfig struct {
    71  	CORS libhttp.CORSConfig `description:"CORS allowed origins" long:"cors"`
    72  }
    73  
    74  func NewDefaultGatewayConfig() GatewayConfig {
    75  	return GatewayConfig{
    76  		CORS: libhttp.CORSConfig{
    77  			AllowedOrigins: []string{"*"},
    78  			MaxAge:         7200,
    79  		},
    80  	}
    81  }
    82  
    83  type RESTConfig struct {
    84  	Level    encoding.LogLevel `choice:"debug"  choice:"info" choice:"warning" long:"log-level"`
    85  	Enabled  encoding.Bool     `long:"enabled"`
    86  	Endpoint string            `long:"endpoint"`
    87  }
    88  
    89  func NewDefaultRESTConfig() RESTConfig {
    90  	return RESTConfig{
    91  		Level:    encoding.LogLevel{Level: logging.InfoLevel},
    92  		Enabled:  encoding.Bool(true),
    93  		Endpoint: "/rest",
    94  	}
    95  }