|
| 1 | +// Copyright (c) 2021 Alexandre Trottier (bonedaddy). All rights reserved. |
| 2 | +// Use of this source code is governed by the Apache 2.0 License that can be found in |
| 3 | +// the LICENSE file. |
| 4 | + |
| 5 | +package config |
| 6 | + |
| 7 | +import ( |
| 8 | + "io/ioutil" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/vrischmann/envconfig" |
| 12 | + "go.bobheadxi.dev/zapx/zapx" |
| 13 | + "go.uber.org/zap" |
| 14 | + "gopkg.in/yaml.v2" |
| 15 | +) |
| 16 | + |
| 17 | +// Config provides the base configuration type |
| 18 | +type Config struct { |
| 19 | + Database `yaml:"database" envconfig:"optional"` |
| 20 | + Blockchain `yaml:"blockchain" envconfig:"optional"` |
| 21 | + Logger `yaml:"logger"` |
| 22 | +} |
| 23 | + |
| 24 | +// Blockchain provides configuration for connection to a blockchain rpc provider |
| 25 | +type Blockchain struct { |
| 26 | + // APIKey provides an API key to be used for authenticaiton with an rpc provider |
| 27 | + APIKey string `yaml:"api_key" envconfig:"optional"` |
| 28 | + // ProvderType indicates the type of rpc provider |
| 29 | + // current values are: infura, direct |
| 30 | + // when direct is provided we connected directly via the RPC parameter |
| 31 | + ProviderType string `yaml:"provider_type"` |
| 32 | + // Network indicates the network type |
| 33 | + // current values are: mainnet, kovan, rinkeby, ropsten |
| 34 | + Network string `yaml:"network"` |
| 35 | + // RPC is the JSON-RPC provider url used when directly connecting to a node |
| 36 | + RPC string `yaml:"rpc" envconfig:"optional"` |
| 37 | + // Websockets when set to true will attempt to use websockets connection negotiation |
| 38 | + Websockets bool `yaml:"websockets" envconfig:"optional"` |
| 39 | +} |
| 40 | + |
| 41 | +// Database provides configuration over our database connection |
| 42 | +type Database struct { |
| 43 | + // Type specifies the database type to be used |
| 44 | + // currently supports: sqlite, postgres |
| 45 | + // when using sqlite type all other options except DBName and DBPath are ignore |
| 46 | + Type string `yaml:"type"` |
| 47 | + // Host specifies the database host address |
| 48 | + Host string `yaml:"host" envconfig:"optional"` |
| 49 | + // Port specifies the port the database is exposed on |
| 50 | + Port string `yaml:"port" envconfig:"optional"` |
| 51 | + // User specifies the username to use for authentication |
| 52 | + User string `yaml:"user" envconfig:"optional"` |
| 53 | + // Pass specifies the password to use for authentication |
| 54 | + Pass string `yaml:"pass" envconfig:"optional"` |
| 55 | + // DBName specifies the name of the database |
| 56 | + DBName string `yaml:"db_name"` |
| 57 | + // DBPath specifies the path to the database file (only applicable to sqlite) |
| 58 | + DBPath string `yaml:"db_path" envconfig:"optional"` |
| 59 | + // SSLModeDisable specifies whether or not to disable ssl connection layer security |
| 60 | + SSLModeDisable bool `yaml:"ssl_mode_disable" envconfig:"optional"` |
| 61 | +} |
| 62 | + |
| 63 | +// Logger provides configuration over zap logger |
| 64 | +type Logger struct { |
| 65 | + // Path to store the configuration file |
| 66 | + Path string `yaml:"path" envconfig:"default=go-defi.log"` |
| 67 | + // Debug enables displaying of debug logs |
| 68 | + Debug bool `yaml:"debug" envconfig:"optional"` |
| 69 | + // Dev enables foramtting of logs to be more human readable |
| 70 | + Dev bool `yaml:"dev" envconfig:"optional"` |
| 71 | + // FileOnly disables stdout logging and will only display log output in `Path` |
| 72 | + FileOnly bool `yaml:"file_only" envconfig:"optional"` |
| 73 | + // Fields provides optional fields to use for logging metadata |
| 74 | + Fields map[string]interface{} `yaml:"fields" envconfig:"optional"` |
| 75 | +} |
| 76 | + |
| 77 | +var ( |
| 78 | + // ExampleConfig is primarily used to provide a template for generating the config file |
| 79 | + ExampleConfig = &Config{ |
| 80 | + Database: Database{ |
| 81 | + Type: "sqlite", |
| 82 | + Host: "localhost", |
| 83 | + Port: "5432", |
| 84 | + User: "postgres", |
| 85 | + Pass: "password123", |
| 86 | + DBName: "go-defi", |
| 87 | + DBPath: "", |
| 88 | + SSLModeDisable: false, |
| 89 | + }, |
| 90 | + Blockchain: Blockchain{ |
| 91 | + APIKey: "CHANGEME", |
| 92 | + ProviderType: "infura", |
| 93 | + Network: "mainnet", |
| 94 | + RPC: "http://localhost:8545", |
| 95 | + Websockets: true, |
| 96 | + }, |
| 97 | + Logger: Logger{ |
| 98 | + Path: "go-defi.log", |
| 99 | + Debug: true, |
| 100 | + Dev: true, |
| 101 | + }, |
| 102 | + } |
| 103 | +) |
| 104 | + |
| 105 | +// NewConfig generates a new config and stores at path |
| 106 | +func NewConfig(path string) error { |
| 107 | + data, err := yaml.Marshal(ExampleConfig) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + return ioutil.WriteFile(path, data, os.ModePerm) |
| 112 | +} |
| 113 | + |
| 114 | +// LoadConfig loads the configuration at path |
| 115 | +// however if loading from path fails, we attempt to load |
| 116 | +// from environment variabels |
| 117 | +func LoadConfig(path string) (cfg *Config, err error) { |
| 118 | + func() { |
| 119 | + var r []byte |
| 120 | + r, err = ioutil.ReadFile(path) |
| 121 | + if err != nil { |
| 122 | + return |
| 123 | + } |
| 124 | + var _cfg Config |
| 125 | + if err := yaml.Unmarshal(r, &cfg); err != nil { |
| 126 | + return |
| 127 | + } |
| 128 | + cfg = &_cfg |
| 129 | + }() |
| 130 | + if err != nil { |
| 131 | + cfg = &Config{} |
| 132 | + err = envconfig.Init(cfg) |
| 133 | + return |
| 134 | + } |
| 135 | + return |
| 136 | +} |
| 137 | + |
| 138 | +// ZapLogger parses the Logger configuration struct and returns a zap logger instance |
| 139 | +func (c *Config) ZapLogger() (*zap.Logger, error) { |
| 140 | + var opts []zapx.Option |
| 141 | + if c.Logger.Debug { |
| 142 | + opts = append(opts, zapx.WithDebug(true)) |
| 143 | + } |
| 144 | + if c.Logger.FileOnly { |
| 145 | + opts = append(opts, zapx.OnlyToFile()) |
| 146 | + } |
| 147 | + if c.Logger.Fields != nil { |
| 148 | + opts = append(opts, zapx.WithFields(c.Logger.Fields)) |
| 149 | + } |
| 150 | + return zapx.New( |
| 151 | + c.Logger.Path, |
| 152 | + c.Logger.Dev, |
| 153 | + opts..., |
| 154 | + ) |
| 155 | +} |
0 commit comments