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.

178 lines
4.3 KiB

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