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.

121 lines
2.1 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
  1. package config
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. //"crypto/rand"
  12. //"encoding/hex"
  13. )
  14. var APP_DIR = os.Getenv("HOME") + "/.tendermint"
  15. /* Global & initialization */
  16. var Config Config_
  17. func init() {
  18. configFile := APP_DIR + "/config.json"
  19. // try to read configuration. if missing, write default
  20. configBytes, err := ioutil.ReadFile(configFile)
  21. if err != nil {
  22. defaultConfig.write(configFile)
  23. fmt.Println("Config file written to config.json. Please edit & run again")
  24. os.Exit(1)
  25. return
  26. }
  27. // try to parse configuration. on error, die
  28. Config = Config_{}
  29. err = json.Unmarshal(configBytes, &Config)
  30. if err != nil {
  31. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  32. }
  33. err = Config.validate()
  34. if err != nil {
  35. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  36. }
  37. }
  38. /* Default configuration */
  39. var defaultConfig = Config_{
  40. Host: "127.0.0.1",
  41. Port: 8770,
  42. Db: DbConfig{
  43. Type: "level",
  44. Dir: APP_DIR + "/data",
  45. },
  46. Twilio: TwilioConfig{},
  47. }
  48. /* Configuration types */
  49. type Config_ struct {
  50. Host string
  51. Port int
  52. Db DbConfig
  53. Twilio TwilioConfig
  54. }
  55. type TwilioConfig struct {
  56. Sid string
  57. Token string
  58. From string
  59. To string
  60. MinInterval int
  61. }
  62. type DbConfig struct {
  63. Type string
  64. Dir string
  65. }
  66. func (cfg *Config_) validate() error {
  67. if cfg.Host == "" {
  68. return errors.New("Host must be set")
  69. }
  70. if cfg.Port == 0 {
  71. return errors.New("Port must be set")
  72. }
  73. if cfg.Db.Type == "" {
  74. return errors.New("Db.Type must be set")
  75. }
  76. return nil
  77. }
  78. func (cfg *Config_) bytes() []byte {
  79. configBytes, err := json.Marshal(cfg)
  80. if err != nil {
  81. panic(err)
  82. }
  83. return configBytes
  84. }
  85. func (cfg *Config_) write(configFile string) {
  86. if strings.Index(configFile, "/") != -1 {
  87. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  88. if err != nil {
  89. panic(err)
  90. }
  91. }
  92. err := ioutil.WriteFile(configFile, cfg.bytes(), 0600)
  93. if err != nil {
  94. panic(err)
  95. }
  96. }
  97. /* TODO: generate priv/pub keys
  98. func generateKeys() string {
  99. bytes := &[30]byte{}
  100. rand.Read(bytes[:])
  101. return hex.EncodeToString(bytes[:])
  102. }
  103. */