registry.go 733 B

123456789101112131415161718192021222324252627282930313233
  1. package notification
  2. import (
  3. "fmt"
  4. "net/url"
  5. )
  6. // NotifierCreator function for create a notifier
  7. type NotifierCreator func(uri *url.URL) (Notifier, error)
  8. // Registry of all Notifiers
  9. var registry = map[string]NotifierCreator{}
  10. // Register a Notifier to the registry
  11. func Register(scheme string, creator NotifierCreator) {
  12. registry[scheme] = creator
  13. }
  14. // NewNotifier create new Notifier
  15. func NewNotifier(uri string) (Notifier, error) {
  16. if uri == "" {
  17. return nil, nil
  18. }
  19. u, err := url.Parse(uri)
  20. if err != nil {
  21. return nil, fmt.Errorf("invalid notification URL: %s", uri)
  22. }
  23. creator, ok := registry[u.Scheme]
  24. if !ok {
  25. return nil, fmt.Errorf("unsupported notification scheme: %s", u.Scheme)
  26. }
  27. return creator(u)
  28. }