You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.6 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package config
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. . "github.com/tendermint/tendermint/common"
  12. )
  13. //-----------------------------------------------------------------------------j
  14. // Configuration types
  15. type ConfigType struct {
  16. Network string
  17. LAddr string
  18. SeedNode string
  19. DB DBConfig
  20. Alert AlertConfig
  21. SMTP SMTPConfig
  22. RPC RPCConfig
  23. }
  24. type DBConfig struct {
  25. Backend string
  26. Dir string
  27. }
  28. type AlertConfig struct {
  29. MinInterval int
  30. TwilioSid string
  31. TwilioToken string
  32. TwilioFrom string
  33. TwilioTo string
  34. EmailRecipients []string
  35. }
  36. type SMTPConfig struct {
  37. User string
  38. Password string
  39. Host string
  40. Port uint
  41. }
  42. type RPCConfig struct {
  43. HTTPPort uint
  44. }
  45. func (cfg *ConfigType) validate() error {
  46. if cfg.Network == "" {
  47. cfg.Network = defaultConfig.Network
  48. }
  49. if cfg.LAddr == "" {
  50. cfg.LAddr = defaultConfig.LAddr
  51. }
  52. if cfg.SeedNode == "" {
  53. cfg.SeedNode = defaultConfig.SeedNode
  54. }
  55. if cfg.DB.Backend == "" {
  56. return errors.New("DB.Backend must be set")
  57. }
  58. return nil
  59. }
  60. func (cfg *ConfigType) bytes() []byte {
  61. configBytes, err := json.MarshalIndent(cfg, "", "\t")
  62. if err != nil {
  63. panic(err)
  64. }
  65. return configBytes
  66. }
  67. func (cfg *ConfigType) write(configFile string) {
  68. if strings.Index(configFile, "/") != -1 {
  69. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  70. if err != nil {
  71. panic(err)
  72. }
  73. }
  74. err := ioutil.WriteFile(configFile, cfg.bytes(), 0600)
  75. if err != nil {
  76. panic(err)
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. var rootDir string
  81. var defaultConfig ConfigType
  82. func init() {
  83. // Get RootDir
  84. rootDir = os.Getenv("TMROOT")
  85. if rootDir == "" {
  86. rootDir = os.Getenv("HOME") + "/.tendermint"
  87. }
  88. // Compute defaultConfig
  89. defaultConfig = ConfigType{
  90. Network: "tendermint_testnet0",
  91. LAddr: "0.0.0.0:0",
  92. SeedNode: "",
  93. DB: DBConfig{
  94. Backend: "leveldb",
  95. Dir: DataDir(),
  96. },
  97. Alert: AlertConfig{},
  98. SMTP: SMTPConfig{},
  99. RPC: RPCConfig{
  100. HTTPPort: 8888,
  101. },
  102. }
  103. }
  104. func ConfigFile() string { return rootDir + "/config.json" }
  105. func GenesisFile() string { return rootDir + "/genesis.json" }
  106. func AddrBookFile() string { return rootDir + "/addrbook.json" }
  107. func PrivValidatorFile() string { return rootDir + "/priv_validator.json" }
  108. func DataDir() string { return rootDir + "/data" }
  109. // The actual global config singleton object.
  110. var Config ConfigType
  111. func parseFlags(flags *flag.FlagSet, args []string) (printHelp bool) {
  112. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  113. flags.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)")
  114. flags.StringVar(&Config.SeedNode, "seed", Config.SeedNode, "Address of seed node")
  115. flags.Parse(args)
  116. return
  117. }
  118. func ParseFlags(args []string) {
  119. configFile := ConfigFile()
  120. // try to read configuration from file. if missing, write default
  121. configBytes, err := ioutil.ReadFile(configFile)
  122. if err != nil {
  123. defaultConfig.write(configFile)
  124. fmt.Println("Config file written to config.json. Please edit & run again")
  125. os.Exit(1)
  126. return
  127. }
  128. // try to parse configuration. on error, die
  129. Config = ConfigType{}
  130. err = json.Unmarshal(configBytes, &Config)
  131. if err != nil {
  132. Exit(Fmt("Invalid configuration file %s: %v", configFile, err))
  133. }
  134. err = Config.validate()
  135. if err != nil {
  136. Exit(Fmt("Invalid configuration file %s: %v", configFile, err))
  137. }
  138. // try to parse arg flags, which can override file configuration.
  139. flags := flag.NewFlagSet("main", flag.ExitOnError)
  140. printHelp := parseFlags(flags, args)
  141. if printHelp {
  142. flags.PrintDefaults()
  143. os.Exit(0)
  144. }
  145. }