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.

141 lines
2.8 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
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. //"crypto/rand"
  13. //"encoding/hex"
  14. )
  15. /* Global & initialization */
  16. var RootDir string
  17. var Config Config_
  18. func initFlags(printHelp *bool) {
  19. flag.BoolVar(printHelp, "help", false, "Print this help message.")
  20. flag.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)")
  21. flag.StringVar(&Config.Seed, "seed", Config.Seed, "Address of seed node")
  22. }
  23. func init() {
  24. RootDir = os.Getenv("TMROOT")
  25. if RootDir == "" {
  26. RootDir = os.Getenv("HOME") + "/.tendermint"
  27. }
  28. configFile := RootDir + "/config.json"
  29. // try to read configuration. if missing, write default
  30. configBytes, err := ioutil.ReadFile(configFile)
  31. if err != nil {
  32. defaultConfig.write(configFile)
  33. fmt.Println("Config file written to config.json. Please edit & run again")
  34. os.Exit(1)
  35. return
  36. }
  37. // try to parse configuration. on error, die
  38. Config = Config_{}
  39. err = json.Unmarshal(configBytes, &Config)
  40. if err != nil {
  41. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  42. }
  43. err = Config.validate()
  44. if err != nil {
  45. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  46. }
  47. // try to parse arg flags, which can override file configuration.
  48. var printHelp bool
  49. initFlags(&printHelp)
  50. flag.Parse()
  51. if printHelp {
  52. fmt.Println("----------------------------------")
  53. flag.PrintDefaults()
  54. fmt.Println("----------------------------------")
  55. os.Exit(0)
  56. }
  57. }
  58. /* Default configuration */
  59. var defaultConfig = Config_{
  60. LAddr: "0.0.0.0:0",
  61. Seed: "",
  62. Db: DbConfig{
  63. Type: "level",
  64. Dir: RootDir + "/data",
  65. },
  66. Twilio: TwilioConfig{},
  67. }
  68. /* Configuration types */
  69. type Config_ struct {
  70. LAddr string
  71. Seed string
  72. Db DbConfig
  73. Twilio TwilioConfig
  74. }
  75. type TwilioConfig struct {
  76. Sid string
  77. Token string
  78. From string
  79. To string
  80. MinInterval int
  81. }
  82. type DbConfig struct {
  83. Type string
  84. Dir string
  85. }
  86. func (cfg *Config_) validate() error {
  87. if cfg.LAddr == "" {
  88. cfg.LAddr = defaultConfig.LAddr
  89. }
  90. if cfg.Seed == "" {
  91. cfg.Seed = defaultConfig.Seed
  92. }
  93. if cfg.Db.Type == "" {
  94. return errors.New("Db.Type must be set")
  95. }
  96. return nil
  97. }
  98. func (cfg *Config_) bytes() []byte {
  99. configBytes, err := json.MarshalIndent(cfg, "", "\t")
  100. if err != nil {
  101. panic(err)
  102. }
  103. return configBytes
  104. }
  105. func (cfg *Config_) write(configFile string) {
  106. if strings.Index(configFile, "/") != -1 {
  107. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  108. if err != nil {
  109. panic(err)
  110. }
  111. }
  112. err := ioutil.WriteFile(configFile, cfg.bytes(), 0600)
  113. if err != nil {
  114. panic(err)
  115. }
  116. }
  117. /* TODO: generate priv/pub keys
  118. func generateKeys() string {
  119. bytes := &[30]byte{}
  120. rand.Read(bytes[:])
  121. return hex.EncodeToString(bytes[:])
  122. }
  123. */