-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.go
75 lines (58 loc) · 1.8 KB
/
broker.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
package main
import (
"flag"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/radiodan/broker/pubsub"
"github.com/radiodan/broker/service"
"os"
"path"
"time"
)
func init() {
// set global logging here
var logLevel log.Level
logLevel, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
logLevel = log.InfoLevel
}
fmt.Printf("Broker started %s\n", time.Now())
fmt.Printf("Log Level: %s\n", logLevel)
log.SetLevel(logLevel)
}
func main() {
serviceLocation, pubLocation, subLocation := parseFlags()
serviceBroker := service.New(serviceLocation)
pubSubBroker := pubsub.New(pubLocation, subLocation)
go serviceBroker.Poll()
go pubSubBroker.Poll()
log.Printf("Broker services on %s", serviceLocation)
log.Printf("Broker publishes on %s", pubLocation)
log.Printf("Broker subscribes on %s", subLocation)
// cheap trick to keep the main thread running
forever := make(chan bool)
<-forever
}
func parseFlags() (service string, pub string, sub string) {
servicePort := flag.Int("service-port", 7171, "Port for service")
pubPort := flag.Int("pub-port", 7172, "Port for publishing")
subPort := flag.Int("sub-port", 7173, "Port for subscribing")
serviceSocket := flag.String("service-socket", "", "Socket path for service")
pubSocket := flag.String("pub-socket", "", "Socket path for publishing")
subSocket := flag.String("sub-socket", "", "Socket path for subscribing")
flag.Parse()
service = connectionPath(*servicePort, *serviceSocket)
pub = connectionPath(*pubPort, *pubSocket)
sub = connectionPath(*subPort, *subSocket)
return
}
func connectionPath(port int, socket string) (fullPath string) {
switch {
case socket == "":
fullPath = fmt.Sprintf("tcp://0.0.0.0:%v", port)
default:
socketPath := path.Clean(socket)
fullPath = fmt.Sprintf("ipc://%v", socketPath)
}
return
}