This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.go
105 lines (87 loc) · 2.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package broker is the vine broker
package broker
import (
"time"
"github.com/lack-io/cli"
"github.com/lack-io/vine"
"github.com/lack-io/vine/cmd/vine/service/broker/handler"
pb "github.com/lack-io/vine/proto/services/broker"
)
var (
// Name of the broker
Name = "go.vine.broker"
// The address of the broker
Address = ":8001"
)
func Run(ctx *cli.Context, svcOpts ...vine.Option) {
if len(ctx.String("server-name")) > 0 {
Name = ctx.String("server-name")
}
if len(ctx.String("address")) > 0 {
Address = ctx.String("address")
}
// Init plugins
for _, p := range Plugins() {
p.Init(ctx)
}
// service opts
svcOpts = append(svcOpts, vine.Name(Name))
if i := time.Duration(ctx.Int("register-ttl")); i > 0 {
svcOpts = append(svcOpts, vine.RegisterTTL(i*time.Second))
}
if i := time.Duration(ctx.Int("register-interval")); i > 0 {
svcOpts = append(svcOpts, vine.RegisterInterval(i*time.Second))
}
// set address
if len(Address) > 0 {
svcOpts = append(svcOpts, vine.Address(Address))
}
// new service
svc := vine.NewService(svcOpts...)
// connect to the broker
svc.Options().Broker.Connect()
// register the broker handler
pb.RegisterBrokerHandler(svc.Server(), &handler.Broker{
// using the mdns broker
Broker: svc.Options().Broker,
})
// run the service
svc.Run()
}
func Commands(options ...vine.Option) []*cli.Command {
command := &cli.Command{
Name: "broker",
Usage: "Run the message broker",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Usage: "Set the broker http address e.g 0.0.0.0:8001",
EnvVars: []string{"VINE_SERVER_ADDRESS"},
},
},
Action: func(ctx *cli.Context) error {
Run(ctx, options...)
return nil
},
}
for _, p := range Plugins() {
if cmds := p.Commands(); len(cmds) > 0 {
command.Subcommands = append(command.Subcommands, cmds...)
}
if flags := p.Flags(); len(flags) > 0 {
command.Flags = append(command.Flags, flags...)
}
}
return []*cli.Command{command}
}