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.

101 lines
2.6 KiB

  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "time"
  6. "github.com/BurntSushi/toml"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "gopkg.in/go-playground/validator.v9"
  9. )
  10. // Duration is a wrapper type that parses time duration from text.
  11. type Duration struct {
  12. time.Duration
  13. }
  14. // UnmarshalText unmarshalls time duration from text.
  15. func (d *Duration) UnmarshalText(data []byte) error {
  16. duration, err := time.ParseDuration(string(data))
  17. if err != nil {
  18. return err
  19. }
  20. d.Duration = duration
  21. return nil
  22. }
  23. // Coordinator is the coordinator specific configuration.
  24. type Coordinator struct {
  25. ForgerAddress ethCommon.Address `validate:"required"`
  26. ForgeLoopInterval Duration `validate:"required"`
  27. }
  28. // Node is the hermez node configuration.
  29. type Node struct {
  30. StateDB struct {
  31. Path string
  32. } `validate:"required"`
  33. PostgreSQL struct {
  34. Port int `validate:"required"`
  35. Host string `validate:"required"`
  36. User string `validate:"required"`
  37. Password string `validate:"required"`
  38. } `validate:"required"`
  39. L2DB struct {
  40. Name string `validate:"required"`
  41. SafetyPeriod uint16 `validate:"required"`
  42. MaxTxs uint32 `validate:"required"`
  43. TTL Duration `validate:"required"`
  44. } `validate:"required"`
  45. HistoryDB struct {
  46. Name string `validate:"required"`
  47. } `validate:"required"`
  48. Web3 struct {
  49. URL string `validate:"required"`
  50. } `validate:"required"`
  51. TxSelector struct {
  52. Path string `validate:"required"`
  53. } `validate:"required"`
  54. BatchBuilder struct {
  55. Path string `validate:"required"`
  56. } `validate:"required"`
  57. Synchronizer struct {
  58. SyncLoopInterval Duration `validate:"required"`
  59. } `validate:"required"`
  60. }
  61. // Load loads a generic config.
  62. func Load(path string, cfg interface{}) error {
  63. bs, err := ioutil.ReadFile(path) //nolint:gosec
  64. if err != nil {
  65. return err
  66. }
  67. cfgToml := string(bs)
  68. if _, err := toml.Decode(cfgToml, cfg); err != nil {
  69. return err
  70. }
  71. validate := validator.New()
  72. if err := validate.Struct(cfg); err != nil {
  73. return fmt.Errorf("error validating configuration file: %w", err)
  74. }
  75. return nil
  76. }
  77. // LoadCoordinator loads the Coordinator configuration from path.
  78. func LoadCoordinator(path string) (*Coordinator, error) {
  79. var cfg Coordinator
  80. if err := Load(path, &cfg); err != nil {
  81. return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
  82. }
  83. return &cfg, nil
  84. }
  85. // LoadNode loads the Node configuration from path.
  86. func LoadNode(path string) (*Node, error) {
  87. var cfg Node
  88. if err := Load(path, &cfg); err != nil {
  89. return nil, fmt.Errorf("error loading node configuration file: %w", err)
  90. }
  91. return &cfg, nil
  92. }