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.

156 lines
3.1 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
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. "log"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. var RootDir string
  14. var Config Config_
  15. func setFlags(printHelp *bool) {
  16. flag.BoolVar(printHelp, "help", false, "Print this help message.")
  17. flag.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)")
  18. flag.StringVar(&Config.SeedNode, "seed", Config.SeedNode, "Address of seed node")
  19. }
  20. func ParseFlags() {
  21. RootDir = os.Getenv("TMROOT")
  22. if RootDir == "" {
  23. RootDir = os.Getenv("HOME") + "/.tendermint"
  24. }
  25. configFile := RootDir + "/config.json"
  26. // try to read configuration. if missing, write default
  27. configBytes, err := ioutil.ReadFile(configFile)
  28. if err != nil {
  29. defaultConfig.write(configFile)
  30. fmt.Println("Config file written to config.json. Please edit & run again")
  31. os.Exit(1)
  32. return
  33. }
  34. // try to parse configuration. on error, die
  35. Config = Config_{}
  36. err = json.Unmarshal(configBytes, &Config)
  37. if err != nil {
  38. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  39. }
  40. err = Config.validate()
  41. if err != nil {
  42. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  43. }
  44. // try to parse arg flags, which can override file configuration.
  45. var printHelp bool
  46. setFlags(&printHelp)
  47. flag.Parse()
  48. if printHelp {
  49. flag.PrintDefaults()
  50. os.Exit(0)
  51. }
  52. }
  53. //-----------------------------------------------------------------------------j
  54. // Default configuration
  55. var defaultConfig = Config_{
  56. Network: "tendermint_testnet0",
  57. LAddr: "0.0.0.0:0",
  58. SeedNode: "",
  59. Db: DbConfig{
  60. Type: "level",
  61. Dir: RootDir + "/data",
  62. },
  63. Alert: AlertConfig{},
  64. SMTP: SMTPConfig{},
  65. RPC: RPCConfig{
  66. HTTPPort: 8888,
  67. },
  68. }
  69. //-----------------------------------------------------------------------------j
  70. // Configuration types
  71. type Config_ struct {
  72. Network string
  73. LAddr string
  74. SeedNode string
  75. Db DbConfig
  76. Alert AlertConfig
  77. SMTP SMTPConfig
  78. RPC RPCConfig
  79. }
  80. type DbConfig struct {
  81. Type string
  82. Dir string
  83. }
  84. type AlertConfig struct {
  85. MinInterval int
  86. TwilioSid string
  87. TwilioToken string
  88. TwilioFrom string
  89. TwilioTo string
  90. EmailRecipients []string
  91. }
  92. type SMTPConfig struct {
  93. User string
  94. Password string
  95. Host string
  96. Port uint
  97. }
  98. type RPCConfig struct {
  99. HTTPPort uint
  100. }
  101. //-----------------------------------------------------------------------------j
  102. func (cfg *Config_) validate() error {
  103. if cfg.Network == "" {
  104. cfg.Network = defaultConfig.Network
  105. }
  106. if cfg.LAddr == "" {
  107. cfg.LAddr = defaultConfig.LAddr
  108. }
  109. if cfg.SeedNode == "" {
  110. cfg.SeedNode = defaultConfig.SeedNode
  111. }
  112. if cfg.Db.Type == "" {
  113. return errors.New("Db.Type must be set")
  114. }
  115. return nil
  116. }
  117. func (cfg *Config_) bytes() []byte {
  118. configBytes, err := json.MarshalIndent(cfg, "", "\t")
  119. if err != nil {
  120. panic(err)
  121. }
  122. return configBytes
  123. }
  124. func (cfg *Config_) write(configFile string) {
  125. if strings.Index(configFile, "/") != -1 {
  126. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  127. if err != nil {
  128. panic(err)
  129. }
  130. }
  131. err := ioutil.WriteFile(configFile, cfg.bytes(), 0600)
  132. if err != nil {
  133. panic(err)
  134. }
  135. }