code.vegaprotocol.io/vega@v0.79.0/core/faucet/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 faucet
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/libs/config/encoding"
    24  	vgfs "code.vegaprotocol.io/vega/libs/fs"
    25  	vghttp "code.vegaprotocol.io/vega/libs/http"
    26  	"code.vegaprotocol.io/vega/logging"
    27  	"code.vegaprotocol.io/vega/paths"
    28  )
    29  
    30  const (
    31  	namedLogger     = "faucet"
    32  	defaultCoolDown = 1 * time.Minute
    33  )
    34  
    35  type Config struct {
    36  	Level      encoding.LogLevel      `description:"Log level"                                long:"level"`
    37  	RateLimit  vghttp.RateLimitConfig `group:"RateLimit"                                      namespace:"rateLimit"`
    38  	WalletName string                 `description:"Name of the wallet to use to sign events" long:"wallet-name"`
    39  	Port       int                    `description:"Listen for connections on port <port>"    long:"port"`
    40  	IP         string                 `description:"Bind to address <ip>"                     long:"ip"`
    41  	Node       NodeConfig             `group:"Node"                                           namespace:"node"`
    42  }
    43  
    44  type NodeConfig struct {
    45  	Port    int    `description:"Connect to Node on port <port>"  long:"port"`
    46  	IP      string `description:"Connect to Node on address <ip>" long:"ip"`
    47  	Retries uint64 `description:"Connection retries before fail"  long:"retries"`
    48  }
    49  
    50  func NewDefaultConfig() Config {
    51  	return Config{
    52  		Level: encoding.LogLevel{Level: logging.InfoLevel},
    53  		RateLimit: vghttp.RateLimitConfig{
    54  			CoolDown:  encoding.Duration{Duration: defaultCoolDown},
    55  			AllowList: []string{"10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fe80::/10"},
    56  		},
    57  		Node: NodeConfig{
    58  			IP:      "127.0.0.1",
    59  			Port:    3002,
    60  			Retries: 5,
    61  		},
    62  		IP:   "0.0.0.0",
    63  		Port: 1790,
    64  	}
    65  }
    66  
    67  type ConfigLoader struct {
    68  	configFilePath string
    69  }
    70  
    71  func InitialiseConfigLoader(vegaPaths paths.Paths) (*ConfigLoader, error) {
    72  	configFilePath, err := vegaPaths.CreateConfigPathFor(paths.FaucetDefaultConfigFile)
    73  	if err != nil {
    74  		return nil, fmt.Errorf("couldn't get path for %s: %w", paths.FaucetDefaultConfigFile, err)
    75  	}
    76  
    77  	return &ConfigLoader{
    78  		configFilePath: configFilePath,
    79  	}, nil
    80  }
    81  
    82  func (l *ConfigLoader) ConfigFilePath() string {
    83  	return l.configFilePath
    84  }
    85  
    86  func (l *ConfigLoader) ConfigExists() (bool, error) {
    87  	exists, err := vgfs.FileExists(l.configFilePath)
    88  	if err != nil {
    89  		return false, fmt.Errorf("couldn't verify file presence: %w", err)
    90  	}
    91  	return exists, nil
    92  }
    93  
    94  func (l *ConfigLoader) GetConfig() (*Config, error) {
    95  	cfg := &Config{}
    96  	if err := paths.ReadStructuredFile(l.configFilePath, cfg); err != nil {
    97  		return nil, fmt.Errorf("couldn't read file at %s: %w", l.configFilePath, err)
    98  	}
    99  	return cfg, nil
   100  }
   101  
   102  func (l *ConfigLoader) SaveConfig(cfg *Config) error {
   103  	if err := paths.WriteStructuredFile(l.configFilePath, cfg); err != nil {
   104  		return fmt.Errorf("couldn't write file at %s: %w", l.configFilePath, err)
   105  	}
   106  	return nil
   107  }
   108  
   109  func (l *ConfigLoader) RemoveConfig() {
   110  	_ = os.RemoveAll(l.configFilePath)
   111  }