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.

168 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. var Config ConfigType
  110. func parseFlags(flags *flag.FlagSet, args []string) (printHelp bool) {
  111. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  112. flags.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)")
  113. flags.StringVar(&Config.SeedNode, "seed", Config.SeedNode, "Address of seed node")
  114. flags.Parse(args)
  115. return
  116. }
  117. func ParseFlags(args []string) {
  118. configFile := ConfigFile()
  119. // try to read configuration from file. if missing, write default
  120. configBytes, err := ioutil.ReadFile(configFile)
  121. if err != nil {
  122. defaultConfig.write(configFile)
  123. fmt.Println("Config file written to config.json. Please edit & run again")
  124. os.Exit(1)
  125. return
  126. }
  127. // try to parse configuration. on error, die
  128. Config = ConfigType{}
  129. err = json.Unmarshal(configBytes, &Config)
  130. if err != nil {
  131. Exit(Fmt("Invalid configuration file %s: %v", configFile, err))
  132. }
  133. err = Config.validate()
  134. if err != nil {
  135. Exit(Fmt("Invalid configuration file %s: %v", configFile, err))
  136. }
  137. // try to parse arg flags, which can override file configuration.
  138. flags := flag.NewFlagSet("main", flag.ExitOnError)
  139. printHelp := parseFlags(flags, args)
  140. if printHelp {
  141. flags.PrintDefaults()
  142. os.Exit(0)
  143. }
  144. }