Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5.  
  6. "github.com/hashicorp/consul/api"
  7. "github.com/hashicorp/consul/watch"
  8. )
  9.  
  10. func main() {
  11. client, err := api.NewClient(&api.Config{Address: ":8500"})
  12. if err != nil {
  13. panic(err)
  14. }
  15.  
  16. _, err = client.KV().Put(&api.KVPair{Key: "volplugin/test"}, nil)
  17. if err != nil {
  18. panic(err)
  19. }
  20.  
  21. wp, err := watch.Parse(map[string]interface{}{
  22. "type": "keyprefix",
  23. "prefix": "volplugin",
  24. })
  25.  
  26. if err != nil {
  27. panic(err)
  28. }
  29.  
  30. wp.Handler = func(u uint64, i interface{}) {
  31. pairs := i.(api.KVPairs)
  32.  
  33. for _, pair := range pairs {
  34. fmt.Printf("%v %q\n", pair.Key, string(pair.Value))
  35. }
  36. }
  37.  
  38. go func() {
  39. if err := wp.Run(":8500"); err != nil {
  40. panic(err)
  41. }
  42. }()
  43.  
  44. _, err = client.KV().Put(&api.KVPair{Key: "volplugin/test2", Value: []byte("hi")}, nil)
  45. if err != nil {
  46. panic(err)
  47. }
  48.  
  49. select {}
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement