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.

58 lines
1.0 KiB

  1. package config
  2. import (
  3. "go-dht/kademlia"
  4. "github.com/spf13/viper"
  5. "github.com/urfave/cli"
  6. )
  7. type Config struct {
  8. ID string
  9. Addr string
  10. Port string
  11. AdminPort string
  12. KnownNodesStr []KnownNodeStr `mapstructure:"knownnodes"`
  13. KnownNodes []kademlia.ListedNode `mapstructure:"-"`
  14. Storage string
  15. }
  16. type KnownNodeStr struct {
  17. ID string
  18. Addr string
  19. Port string
  20. }
  21. var C Config
  22. func MustRead(c *cli.Context) error {
  23. viper.SetConfigType("yaml")
  24. viper.SetConfigName("config")
  25. viper.AddConfigPath(".")
  26. if c.GlobalString("config") != "" {
  27. viper.SetConfigFile(c.GlobalString("config"))
  28. }
  29. if err := viper.ReadInConfig(); err != nil {
  30. return err
  31. }
  32. if err := viper.Unmarshal(&C); err != nil {
  33. return err
  34. }
  35. for _, v := range C.KnownNodesStr {
  36. id, err := kademlia.IDFromString(v.ID)
  37. if err != nil {
  38. return err
  39. }
  40. kn := kademlia.ListedNode{
  41. ID: id,
  42. Addr: v.Addr,
  43. Port: v.Port,
  44. }
  45. C.KnownNodes = append(C.KnownNodes, kn)
  46. }
  47. C.KnownNodesStr = []KnownNodeStr{}
  48. return nil
  49. }