forked from TheThingsArchive/ttn
/
handler.go
41 lines (36 loc) · 1.44 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
// 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 util
import (
"github.com/TheThingsNetwork/go-account-lib/scope"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api/discovery"
"github.com/TheThingsNetwork/ttn/api/handler"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
// GetHandlerManager gets a new HandlerManager for ttnctl
func GetHandlerManager(ctx ttnlog.Interface, appID string) (*grpc.ClientConn, *handler.ManagerClient) {
ctx.WithField("Handler", viper.GetString("handler-id")).Info("Discovering Handler...")
dscConn, client := GetDiscovery(ctx)
defer dscConn.Close()
handlerAnnouncement, err := client.Get(GetContext(ctx), &discovery.GetRequest{
ServiceName: "handler",
Id: viper.GetString("handler-id"),
})
if err != nil {
ctx.WithError(errors.FromGRPCError(err)).Fatal("Could not find Handler")
}
token := TokenForScope(ctx, scope.App(appID))
ctx.WithField("Handler", handlerAnnouncement.NetAddress).Info("Connecting with Handler...")
hdlConn, err := handlerAnnouncement.Dial(nil)
if err != nil {
ctx.WithError(err).Fatal("Could not connect to Handler")
}
managerClient, err := handler.NewManagerClient(hdlConn, token)
if err != nil {
ctx.WithError(err).Fatal("Could not create Handler Manager")
}
return hdlConn, managerClient
}