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.

186 lines
4.7 KiB

11 years ago
11 years ago
11 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
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. app.SetDefault("FastSync", false)
  92. app.SetDefault("Debora.LogFile", rootDir+"/debora.log")
  93. }
  94. func Init(rootDir string) {
  95. // Get RootDir
  96. if rootDir == "" {
  97. rootDir = os.Getenv("TMROOT")
  98. }
  99. if rootDir == "" {
  100. rootDir = os.Getenv("HOME") + "/.tendermint"
  101. }
  102. configFile := path.Join(rootDir, "config.toml")
  103. genesisFile := path.Join(rootDir, "genesis.json")
  104. // Write default config file if missing.
  105. checkWriteFile(configFile, defaultConfig)
  106. checkWriteFile(genesisFile, DefaultGenesis)
  107. // Initialize Config
  108. app = confer.NewConfig()
  109. initDefaults(rootDir)
  110. paths := []string{configFile}
  111. if err := app.ReadPaths(paths...); err != nil {
  112. log.Warn("Error reading configuration", "paths", paths, "error", err)
  113. }
  114. // Confused?
  115. // app.Debug()
  116. }
  117. // Check if a file exists; if not, ensure the directory is made and write the file
  118. func checkWriteFile(configFile, contents string) {
  119. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  120. if strings.Index(configFile, "/") != -1 {
  121. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  122. if err != nil {
  123. fmt.Printf("Could not create directory: %v", err)
  124. os.Exit(1)
  125. }
  126. }
  127. err := ioutil.WriteFile(configFile, []byte(contents), 0600)
  128. if err != nil {
  129. fmt.Printf("Could not write config file: %v", err)
  130. os.Exit(1)
  131. }
  132. fmt.Printf("Config file written to %v.\n", configFile)
  133. }
  134. }
  135. func ParseFlags(args []string) {
  136. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  137. var printHelp = false
  138. // Declare flags
  139. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  140. flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
  141. flags.String("seed_node", app.GetString("SeedNode"), "Address of seed node")
  142. flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
  143. flags.Bool("fast_sync", app.GetBool("FastSync"), "Fast blockchain syncing")
  144. flags.String("log_stdout_level", app.GetString("Log.Stdout.Level"), "Stdout log level")
  145. flags.Parse(args)
  146. if printHelp {
  147. flags.PrintDefaults()
  148. os.Exit(0)
  149. }
  150. // Merge parsed flag values onto app.
  151. app.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
  152. app.BindPFlag("SeedNode", flags.Lookup("seed_node"))
  153. app.BindPFlag("FastSync", flags.Lookup("fast_sync"))
  154. app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
  155. app.BindPFlag("Log.Stdout.Level", flags.Lookup("log_stdout_level"))
  156. // Confused?
  157. //app.Debug()
  158. }