code.vegaprotocol.io/vega@v0.79.0/wallet/service/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 service 17 18 import ( 19 "errors" 20 "fmt" 21 "time" 22 23 vgencoding "code.vegaprotocol.io/vega/libs/encoding" 24 25 "go.uber.org/zap" 26 ) 27 28 var ( 29 ErrInvalidLogLevelValue = errors.New("the service log level is invalid") 30 ErrInvalidMaximumTokenDuration = errors.New("the maximum token duration is invalid") 31 ErrInvalidMaximumNodeRequestDuration = errors.New("the maximum request duration is invalid") 32 ErrServerHostUnset = errors.New("the service host is unset") 33 ErrServerPortUnset = errors.New("the service port is unset") 34 ) 35 36 type Config struct { 37 LogLevel vgencoding.LogLevel `json:"logLevel"` 38 Server ServerConfig `json:"server"` 39 APIV1 APIV1Config `json:"apiV1"` 40 APIV2 APIV2Config `json:"apiV2"` 41 } 42 43 type ServerConfig struct { 44 Port int `json:"port"` 45 Host string `json:"host"` 46 } 47 48 func (c ServerConfig) String() string { 49 return fmt.Sprintf("http://%v:%v", c.Host, c.Port) 50 } 51 52 type APIV1Config struct { 53 MaximumTokenDuration vgencoding.Duration `json:"maximumTokenDuration"` 54 } 55 56 type APIV2Config struct { 57 Nodes Nodes `json:"nodes"` 58 } 59 60 type Nodes struct { 61 MaximumRetryPerRequest uint64 `json:"maximumRetryPerRequest"` 62 MaximumRequestDuration vgencoding.Duration `json:"maximumRequestDuration"` 63 } 64 65 func DefaultConfig() *Config { 66 return &Config{ 67 LogLevel: vgencoding.LogLevel{ 68 Level: zap.InfoLevel, 69 }, 70 Server: ServerConfig{ 71 Port: 1789, 72 Host: "127.0.0.1", 73 }, 74 75 APIV1: APIV1Config{ 76 MaximumTokenDuration: vgencoding.Duration{ 77 Duration: 168 * time.Hour, 78 }, 79 }, 80 APIV2: APIV2Config{ 81 Nodes: Nodes{ 82 MaximumRetryPerRequest: 5, 83 MaximumRequestDuration: vgencoding.Duration{ 84 Duration: 5 * time.Second, 85 }, 86 }, 87 }, 88 } 89 } 90 91 // Validate checks the values set in the server config file returning an error is anything is awry. 92 func (c *Config) Validate() error { 93 if c.Server.Host == "" { 94 return ErrServerHostUnset 95 } 96 97 if c.Server.Port == 0 { 98 return ErrServerPortUnset 99 } 100 101 tokenExpiry := &vgencoding.Duration{} 102 if err := tokenExpiry.UnmarshalText([]byte(c.APIV1.MaximumTokenDuration.String())); err != nil { 103 return ErrInvalidMaximumTokenDuration 104 } 105 106 requestExpiry := &vgencoding.Duration{} 107 if err := requestExpiry.UnmarshalText([]byte(c.APIV2.Nodes.MaximumRequestDuration.String())); err != nil { 108 return ErrInvalidMaximumNodeRequestDuration 109 } 110 111 logLevel := &vgencoding.LogLevel{} 112 if err := logLevel.UnmarshalText([]byte(c.LogLevel.String())); err != nil { 113 return ErrInvalidLogLevelValue 114 } 115 116 return nil 117 }