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.5 KiB

11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 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(".tendermint")
  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 = "188.166.55.222: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. "Accounts": [
  54. {
  55. "Address": "29BF3A0A13001A0D23533386BE03E74923AF1179",
  56. "Amount": 2099900000000000
  57. }
  58. ],
  59. "Validators": [
  60. {
  61. "PubKey": [1, "3A2C5C341FFC1D5F7AB518519FF8289D3BFAB82DFD6E167B926FAD72C1BF10F8"],
  62. "Amount": 100000000000,
  63. "UnbondTo": [
  64. {
  65. "Address": "29BF3A0A13001A0D23533386BE03E74923AF1179",
  66. "Amount": 100000000000
  67. }
  68. ]
  69. }
  70. ]
  71. }`
  72. // NOTE: If you change this, maybe also change defaultConfig
  73. func initDefaults(rootDir string) {
  74. app.SetDefault("Network", "tendermint_testnet0")
  75. app.SetDefault("ListenAddr", "0.0.0.0:8080")
  76. app.SetDefault("DB.Backend", "leveldb")
  77. app.SetDefault("DB.Dir", rootDir+"/data")
  78. app.SetDefault("Log.Stdout.Level", "info")
  79. app.SetDefault("Log.File.Dir", rootDir+"/log")
  80. app.SetDefault("Log.File.Level", "debug")
  81. app.SetDefault("RPC.HTTP.ListenAddr", "0.0.0.0:8081")
  82. app.SetDefault("GenesisFile", rootDir+"/genesis.json")
  83. app.SetDefault("AddrBookFile", rootDir+"/addrbook.json")
  84. app.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json")
  85. app.SetDefault("FastSync", false)
  86. }
  87. func Init(rootDir string) {
  88. // Get RootDir
  89. configFile := path.Join(rootDir, "config.toml")
  90. genesisFile := path.Join(rootDir, "genesis.json")
  91. // Write default config file if missing.
  92. checkWriteFile(configFile, defaultConfig)
  93. checkWriteFile(genesisFile, DefaultGenesis)
  94. // Initialize Config
  95. app = confer.NewConfig()
  96. initDefaults(rootDir)
  97. paths := []string{configFile}
  98. if err := app.ReadPaths(paths...); err != nil {
  99. log.Warn("Error reading configuration", "paths", paths, "error", err)
  100. }
  101. // Confused?
  102. // app.Debug()
  103. }
  104. // Check if a file exists; if not, ensure the directory is made and write the file
  105. func checkWriteFile(configFile, contents string) {
  106. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  107. if strings.Index(configFile, "/") != -1 {
  108. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  109. if err != nil {
  110. fmt.Printf("Could not create directory: %v", err)
  111. os.Exit(1)
  112. }
  113. }
  114. err := ioutil.WriteFile(configFile, []byte(contents), 0600)
  115. if err != nil {
  116. fmt.Printf("Could not write config file: %v", err)
  117. os.Exit(1)
  118. }
  119. fmt.Printf("Config file written to %v.\n", configFile)
  120. }
  121. }
  122. func ParseFlags(args []string) {
  123. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  124. var printHelp = false
  125. // Declare flags
  126. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  127. flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
  128. flags.String("seed_node", app.GetString("SeedNode"), "Address of seed node")
  129. flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
  130. flags.Bool("fast_sync", app.GetBool("FastSync"), "Fast blockchain syncing")
  131. flags.String("log_stdout_level", app.GetString("Log.Stdout.Level"), "Stdout log level")
  132. flags.Parse(args)
  133. if printHelp {
  134. flags.PrintDefaults()
  135. os.Exit(0)
  136. }
  137. // Merge parsed flag values onto app.
  138. app.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
  139. app.BindPFlag("SeedNode", flags.Lookup("seed_node"))
  140. app.BindPFlag("FastSync", flags.Lookup("fast_sync"))
  141. app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
  142. app.BindPFlag("Log.Stdout.Level", flags.Lookup("log_stdout_level"))
  143. // Confused?
  144. //app.Debug()
  145. }