-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
224 lines (195 loc) · 9.56 KB
/
handler.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
pb "github.com/TheThingsNetwork/api/handler"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api/pool"
"github.com/TheThingsNetwork/ttn/core/component"
"github.com/TheThingsNetwork/ttn/core/handler"
"github.com/TheThingsNetwork/ttn/core/proxy"
"github.com/TheThingsNetwork/ttn/core/proxy/jsonpb"
"github.com/TheThingsNetwork/ttn/utils/parse"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711"
"google.golang.org/grpc"
"gopkg.in/redis.v5"
)
// handlerCmd represents the handler command
var handlerCmd = &cobra.Command{
Use: "handler",
Short: "The Things Network handler",
Long: ``,
PreRun: func(cmd *cobra.Command, args []string) {
ctx.WithFields(ttnlog.Fields{
"Server": fmt.Sprintf("%s:%d", viper.GetString("handler.server-address"), viper.GetInt("handler.server-port")),
"HTTP Proxy": fmt.Sprintf("%s:%d", viper.GetString("handler.http-address"), viper.GetInt("handler.http-port")),
"Announce": fmt.Sprintf("%s:%d", viper.GetString("handler.server-address-announce"), viper.GetInt("handler.server-port")),
"Database": fmt.Sprintf("%s/%d", viper.GetString("handler.redis-address"), viper.GetInt("handler.redis-db")),
"TTN Broker ID": viper.GetString("handler.broker-id"),
"MQTT": viper.GetString("handler.mqtt-address"),
"AMQP": viper.GetString("handler.amqp-address"),
}).Info("Initializing Handler")
},
Run: func(cmd *cobra.Command, args []string) {
ctx.Info("Starting")
// Redis Client
client := redis.NewClient(&redis.Options{
Addr: viper.GetString("handler.redis-address"),
Password: viper.GetString("handler.redis-password"),
DB: viper.GetInt("handler.redis-db"),
})
if err := connectRedis(client); err != nil {
ctx.WithError(err).Fatal("Could not initialize database connection")
}
// Component
component, err := component.New(ttnlog.Get(), "handler", fmt.Sprintf("%s:%d", viper.GetString("handler.server-address-announce"), viper.GetInt("handler.server-port")))
if err != nil {
ctx.WithError(err).Fatal("Could not initialize component")
}
httpActive := viper.GetString("handler.http-address") != "" && viper.GetInt("handler.http-port") != 0
if httpActive && component.Identity.ApiAddress == "" {
component.Identity.ApiAddress = fmt.Sprintf("http://%s:%d", viper.GetString("handler.server-address-announce"), viper.GetInt("handler.http-port"))
}
// Handler
handler := handler.NewRedisHandler(
client,
viper.GetString("handler.broker-id"),
)
if viper.GetString("handler.mqtt-address") != "" {
handler = handler.WithMQTT(
viper.GetString("handler.mqtt-username"),
viper.GetString("handler.mqtt-password"),
viper.GetString("handler.mqtt-address"),
)
mqttPort, err := parse.Port(viper.GetString("handler.mqtt-address"))
if err != nil {
ctx.WithError(err).Error("Could not announce the handler")
}
if announceAddr := viper.GetString("handler.mqtt-address-announce"); announceAddr != "" {
component.Identity.MqttAddress = fmt.Sprintf("%s:%d", announceAddr, mqttPort)
} else {
component.Identity.MqttAddress = fmt.Sprintf("%s:%d", viper.GetString("handler.server-address-announce"), mqttPort)
}
} else {
ctx.Warn("MQTT is not enabled in your configuration")
}
if viper.GetString("handler.amqp-address") != "" {
handler = handler.WithAMQP(
viper.GetString("handler.amqp-username"),
viper.GetString("handler.amqp-password"),
viper.GetString("handler.amqp-address"),
viper.GetString("handler.amqp-exchange"),
)
amqpPort, err := parse.Port(viper.GetString("handler.amqp-address"))
if err != nil {
ctx.WithError(err).Error("Could not announce the handler")
}
if announceAddr := viper.GetString("handler.amqp-address-announce"); announceAddr != "" {
component.Identity.AmqpAddress = fmt.Sprintf("%s:%d", announceAddr, amqpPort)
} else {
component.Identity.AmqpAddress = fmt.Sprintf("%s:%d", viper.GetString("handler.server-address-announce"), amqpPort)
}
} else {
ctx.Warn("AMQP is not enabled in your configuration")
}
if extraDeviceAttributes := viper.GetStringSlice("handler.extra-device-attributes"); len(extraDeviceAttributes) != 0 {
handler = handler.WithDeviceAttributes(extraDeviceAttributes...)
} else {
ctx.Debug("No extra device attribute set in your configuration")
}
err = handler.Init(component)
if err != nil {
ctx.WithError(err).Fatal("Could not initialize handler")
}
defer handler.Shutdown()
// gRPC Server
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", viper.GetString("handler.server-address"), viper.GetInt("handler.server-port")))
if err != nil {
ctx.WithError(err).Fatal("Could not start gRPC server")
}
grpc := grpc.NewServer(component.ServerOptions()...)
// Register and Listen
handler.RegisterRPC(grpc)
handler.RegisterManager(grpc)
component.RegisterHealthServer(grpc) // must be last one
go grpc.Serve(lis)
defer grpc.Stop()
if httpActive {
proxyConn, err := component.Identity.Dial(pool.Global)
if err != nil {
ctx.WithError(err).Fatal("Could not start client for gRPC proxy")
}
mux := runtime.NewServeMux(runtime.WithMarshalerOption("*", &jsonpb.GoGoJSONPb{
OrigName: true,
}))
netCtx, cancel := context.WithCancel(context.Background())
defer cancel()
pb.RegisterApplicationManagerHandler(netCtx, mux, proxyConn)
prxy := proxy.WithToken(mux)
prxy = proxy.WithPagination(prxy)
prxy = proxy.WithLogger(prxy, ctx)
go func() {
err := http.ListenAndServe(
fmt.Sprintf("%s:%d", viper.GetString("handler.http-address"), viper.GetInt("handler.http-port")),
prxy,
)
if err != nil {
ctx.WithError(err).Fatal("Error in gRPC proxy")
}
}()
}
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
ctx.WithField("signal", <-sigChan).Info("signal received")
},
}
func init() {
RootCmd.AddCommand(handlerCmd)
handlerCmd.Flags().String("redis-address", "localhost:6379", "Redis host and port")
viper.BindPFlag("handler.redis-address", handlerCmd.Flags().Lookup("redis-address"))
handlerCmd.Flags().String("redis-password", "", "Redis password")
viper.BindPFlag("handler.redis-password", handlerCmd.Flags().Lookup("redis-password"))
handlerCmd.Flags().Int("redis-db", 0, "Redis database")
viper.BindPFlag("handler.redis-db", handlerCmd.Flags().Lookup("redis-db"))
handlerCmd.Flags().String("broker-id", "dev", "The ID of the TTN Broker as announced in the Discovery server")
viper.BindPFlag("handler.broker-id", handlerCmd.Flags().Lookup("broker-id"))
handlerCmd.Flags().String("mqtt-address", "", "MQTT host and port. Leave empty to disable MQTT")
handlerCmd.Flags().String("mqtt-address-announce", "", "MQTT address to announce (takes value of server-address-announce if empty while enabled)")
handlerCmd.Flags().String("mqtt-username", "", "MQTT username")
handlerCmd.Flags().String("mqtt-password", "", "MQTT password")
viper.BindPFlag("handler.mqtt-address", handlerCmd.Flags().Lookup("mqtt-address"))
viper.BindPFlag("handler.mqtt-address-announce", handlerCmd.Flags().Lookup("mqtt-address-announce"))
viper.BindPFlag("handler.mqtt-username", handlerCmd.Flags().Lookup("mqtt-username"))
viper.BindPFlag("handler.mqtt-password", handlerCmd.Flags().Lookup("mqtt-password"))
handlerCmd.Flags().String("amqp-address", "", "AMQP host and port. Leave empty to disable AMQP")
handlerCmd.Flags().String("amqp-address-announce", "", "AMQP address to announce (takes value of server-address-announce if empty while enabled)")
handlerCmd.Flags().String("amqp-username", "guest", "AMQP username")
handlerCmd.Flags().String("amqp-password", "guest", "AMQP password")
handlerCmd.Flags().String("amqp-exchange", "ttn.handler", "AMQP exchange")
viper.BindPFlag("handler.amqp-address", handlerCmd.Flags().Lookup("amqp-address"))
viper.BindPFlag("handler.amqp-address-announce", handlerCmd.Flags().Lookup("amqp-address-announce"))
viper.BindPFlag("handler.amqp-username", handlerCmd.Flags().Lookup("amqp-username"))
viper.BindPFlag("handler.amqp-password", handlerCmd.Flags().Lookup("amqp-password"))
viper.BindPFlag("handler.amqp-exchange", handlerCmd.Flags().Lookup("amqp-exchange"))
handlerCmd.Flags().String("server-address", "0.0.0.0", "The IP address to listen for communication")
handlerCmd.Flags().String("server-address-announce", "localhost", "The public IP address to announce")
handlerCmd.Flags().Int("server-port", 1904, "The port for communication")
viper.BindPFlag("handler.server-address", handlerCmd.Flags().Lookup("server-address"))
viper.BindPFlag("handler.server-address-announce", handlerCmd.Flags().Lookup("server-address-announce"))
viper.BindPFlag("handler.server-port", handlerCmd.Flags().Lookup("server-port"))
handlerCmd.Flags().String("http-address", "0.0.0.0", "The IP address where the gRPC proxy should listen")
handlerCmd.Flags().Int("http-port", 8084, "The port where the gRPC proxy should listen")
viper.BindPFlag("handler.http-address", handlerCmd.Flags().Lookup("http-address"))
viper.BindPFlag("handler.http-port", handlerCmd.Flags().Lookup("http-port"))
handlerCmd.Flags().StringSlice("extra-device-attributes", nil, "Extra device attributes to be whitelisted")
viper.BindPFlag("handler.extra-device-attributes", handlerCmd.Flags().Lookup("extra-device-attributes"))
}