config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Package config is an interface for dynamic configuration.
  2. package config
  3. import (
  4. "context"
  5. "gogs.baozhida.cn/zoie/OAuth-core/config/loader"
  6. "gogs.baozhida.cn/zoie/OAuth-core/config/reader"
  7. "gogs.baozhida.cn/zoie/OAuth-core/config/source"
  8. "gogs.baozhida.cn/zoie/OAuth-core/config/source/file"
  9. )
  10. // Config is an interface abstraction for dynamic configuration
  11. type Config interface {
  12. // Values provide the reader.Values interface
  13. reader.Values
  14. // Init the config
  15. Init(opts ...Option) error
  16. // Options in the config
  17. Options() Options
  18. // Close Stop the config loader/watcher
  19. Close() error
  20. // Load config sources
  21. Load(source ...source.Source) error
  22. // Sync Force a source changeset sync
  23. Sync() error
  24. // Watch a value for changes
  25. Watch(path ...string) (Watcher, error)
  26. }
  27. // Watcher is the config watcher
  28. type Watcher interface {
  29. Next() (reader.Value, error)
  30. Stop() error
  31. }
  32. // Entity 配置实体
  33. type Entity interface {
  34. OnChange()
  35. }
  36. // Options 配置的参数
  37. type Options struct {
  38. Loader loader.Loader
  39. Reader reader.Reader
  40. Source []source.Source
  41. // for alternative data
  42. Context context.Context
  43. Entity Entity
  44. }
  45. // Option 调用类型
  46. type Option func(o *Options)
  47. var (
  48. // DefaultConfig Default Config Manager
  49. DefaultConfig Config
  50. )
  51. // NewConfig returns new config
  52. func NewConfig(opts ...Option) (Config, error) {
  53. return newConfig(opts...)
  54. }
  55. // Bytes Return config as raw json
  56. func Bytes() []byte {
  57. return DefaultConfig.Bytes()
  58. }
  59. // Map Return config as a map
  60. func Map() map[string]interface{} {
  61. return DefaultConfig.Map()
  62. }
  63. // Scan values to a go type
  64. func Scan(v interface{}) error {
  65. return DefaultConfig.Scan(v)
  66. }
  67. // Sync Force a source changeset sync
  68. func Sync() error {
  69. return DefaultConfig.Sync()
  70. }
  71. // Get a value from the config
  72. func Get(path ...string) reader.Value {
  73. return DefaultConfig.Get(path...)
  74. }
  75. // Load config sources
  76. func Load(source ...source.Source) error {
  77. return DefaultConfig.Load(source...)
  78. }
  79. // Watch a value for changes
  80. func Watch(path ...string) (Watcher, error) {
  81. return DefaultConfig.Watch(path...)
  82. }
  83. // LoadFile is short hand for creating a file source and loading it
  84. func LoadFile(path string) error {
  85. return Load(file.NewSource(
  86. file.WithPath(path),
  87. ))
  88. }