-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpsub.go
78 lines (75 loc) · 1.8 KB
/
httpsub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"encoding/json"
"github.com/godaner/brokerc/broker"
"github.com/godaner/brokerc/broker/httpv1"
"github.com/urfave/cli"
"os"
"os/signal"
)
var HTTPSubscribeCommand = cli.Command{
Name: "httpsub",
Usage: "subscribe http message",
UsageText: "Usage: brokerc httpsub [options...]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "h",
Usage: "host.",
Required: true,
},
cli.BoolFlag{
Name: "d",
Usage: "debug.",
Required: false,
},
cli.StringFlag{
Name: "cafile",
Usage: "path to a file containing trusted CA certificates to enable encrypted communication.",
Required: false,
},
cli.StringFlag{
Name: "cert",
Usage: "server certificate for authentication, if required by server.",
Required: false,
},
cli.StringFlag{
Name: "key",
Usage: "server private key for authentication, if required by server.",
Required: false,
},
},
Action: func(context *cli.Context) error {
h, d, cafile, cert, key :=
context.String("h"),
context.Bool("d"),
context.String("cafile"),
context.String("cert"),
context.String("key")
logger.SetDebug(d)
b := httpv1.HTTPBrokerV1{
CACertFile: cafile,
CertFile: cert,
KeyFile: key,
Logger: logger,
}
err := b.Connect()
if err != nil {
return err
}
defer b.Disconnect()
s, err := b.Subscribe([]string{h}, func(event broker.Event) error {
hs, _ := json.Marshal(event.Message().Header)
logger.Infof("SUBSCRIBE=> uri:%v, m:%v !", event.Topic(), string(event.Message().Body))
logger.Debugf("SUBSCRIBE=> uri:%v, H:%v !", event.Topic(), string(hs))
return nil
})
if err != nil {
return err
}
defer s.Unsubscribe()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
<-sig
return nil
},
}