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.

172 lines
4.3 KiB

11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. "sync"
  10. flag "github.com/spf13/pflag"
  11. "github.com/tendermint/confer"
  12. )
  13. var app *confer.Config
  14. var appMtx sync.Mutex
  15. func App() *confer.Config {
  16. appMtx.Lock()
  17. defer appMtx.Unlock()
  18. if app == nil {
  19. Init("")
  20. }
  21. return app
  22. }
  23. // NOTE: If you change this, maybe also change initDefaults()
  24. var defaultConfig = `# This is a TOML config file.
  25. # For more information, see https://github.com/toml-lang/toml
  26. Network = "tendermint_testnet0"
  27. ListenAddr = "0.0.0.0:8080"
  28. # First node to connect to. Command-line overridable.
  29. SeedNode = "23.239.22.253:8080"
  30. [DB]
  31. # The only other available backend is "memdb"
  32. Backend = "leveldb"
  33. # Dir = "~/.tendermint/data"
  34. [Log.Stdout]
  35. Level = "info"
  36. [Log.File]
  37. Level = "debug"
  38. # Dir = "~/.tendermint/log"
  39. [RPC.HTTP]
  40. # For the RPC API HTTP server. Port required.
  41. ListenAddr = "127.0.0.1:8081"
  42. [Alert]
  43. # TODO: Document options
  44. [SMTP]
  45. # TODO: Document options
  46. `
  47. var defaultGenesis = `
  48. {
  49. "Accounts": [
  50. {
  51. "Address": "553722287BF1230C081C270908C1F453E7D1C397",
  52. "Amount": 200000000
  53. },
  54. {
  55. "Address": "AC89A6DDF4C309A89A2C4078CE409A5A7B282270",
  56. "Amount": 200000000
  57. }
  58. ],
  59. "Validators": [
  60. {
  61. "PubKey": [1, "932A857D334BA5A38DD8E0D9CDE9C84687C21D0E5BEE64A1EDAB9C6C32344F1A"],
  62. "Amount": 100000000,
  63. "UnbondTo": [
  64. {
  65. "Address": "553722287BF1230C081C270908C1F453E7D1C397",
  66. "Amount": 100000000
  67. }
  68. ]
  69. }
  70. ]
  71. }
  72. `
  73. // NOTE: If you change this, maybe also change defaultConfig
  74. func initDefaults(rootDir string) {
  75. app.SetDefault("Network", "tendermint_testnet0")
  76. app.SetDefault("ListenAddr", "0.0.0.0:8080")
  77. app.SetDefault("DB.Backend", "leveldb")
  78. app.SetDefault("DB.Dir", rootDir+"/data")
  79. app.SetDefault("Log.Stdout.Level", "info")
  80. app.SetDefault("Log.File.Dir", rootDir+"/log")
  81. app.SetDefault("Log.File.Level", "debug")
  82. app.SetDefault("RPC.HTTP.ListenAddr", "0.0.0.0:8081")
  83. app.SetDefault("GenesisFile", rootDir+"/genesis.json")
  84. app.SetDefault("AddrBookFile", rootDir+"/addrbook.json")
  85. app.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json")
  86. }
  87. func Init(rootDir string) {
  88. // Get RootDir
  89. if rootDir == "" {
  90. rootDir = os.Getenv("TMROOT")
  91. }
  92. if rootDir == "" {
  93. rootDir = os.Getenv("HOME") + "/.tendermint"
  94. }
  95. configFile := path.Join(rootDir, "config.toml")
  96. genesisFile := path.Join(rootDir, "genesis.json")
  97. // Write default config file if missing.
  98. checkWriteFile(configFile, defaultConfig)
  99. checkWriteFile(genesisFile, defaultGenesis)
  100. // Initialize Config
  101. app = confer.NewConfig()
  102. initDefaults(rootDir)
  103. paths := []string{configFile}
  104. if err := app.ReadPaths(paths...); err != nil {
  105. log.Warn("Error reading configuration", "paths", paths, "error", err)
  106. }
  107. // Confused?
  108. // app.Debug()
  109. }
  110. // Check if a file exists; if not, ensure the directory is made and write the file
  111. func checkWriteFile(configFile, contents string) {
  112. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  113. if strings.Index(configFile, "/") != -1 {
  114. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  115. if err != nil {
  116. fmt.Printf("Could not create directory: %v", err)
  117. os.Exit(1)
  118. }
  119. }
  120. err := ioutil.WriteFile(configFile, []byte(contents), 0600)
  121. if err != nil {
  122. fmt.Printf("Could not write config file: %v", err)
  123. os.Exit(1)
  124. }
  125. fmt.Printf("Config file written to %v.\n", configFile)
  126. }
  127. }
  128. func ParseFlags(args []string) {
  129. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  130. var printHelp = false
  131. // Declare flags
  132. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  133. flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
  134. flags.String("seed_node", app.GetString("SeedNode"), "Address of seed node")
  135. flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
  136. flags.Parse(args)
  137. if printHelp {
  138. flags.PrintDefaults()
  139. os.Exit(0)
  140. }
  141. // Merge parsed flag values onto app.
  142. app.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
  143. app.BindPFlag("SeedNode", flags.Lookup("seed_node"))
  144. app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
  145. // Confused?
  146. //app.Debug()
  147. }