-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
99 lines (87 loc) · 2.98 KB
/
component.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
// 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 component contains code that is shared by all components (discovery, router, broker, networkserver, handler)
package component
import (
"crypto/ecdsa"
"crypto/tls"
"fmt"
"github.com/TheThingsNetwork/go-account-lib/claims"
"github.com/TheThingsNetwork/go-account-lib/tokenkey"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
pb_discovery "github.com/TheThingsNetwork/ttn/api/discovery"
pb_monitor "github.com/TheThingsNetwork/ttn/api/monitor"
"github.com/TheThingsNetwork/ttn/api/pool"
"github.com/spf13/viper"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
)
// Component contains the common attributes for all TTN components
type Component struct {
Config Config
Pool *pool.Pool
Identity *pb_discovery.Announcement
Discovery pb_discovery.Client
Monitor *pb_monitor.Client
Ctx ttnlog.Interface
Context context.Context
AccessToken string
privateKey *ecdsa.PrivateKey
tlsConfig *tls.Config
TokenKeyProvider tokenkey.Provider
status int32
healthServer *health.Server
}
type Interface interface {
RegisterRPC(s *grpc.Server)
Init(c *Component) error
Shutdown()
ValidateNetworkContext(ctx context.Context) (*pb_discovery.Announcement, error)
ValidateTTNAuthContext(ctx context.Context) (*claims.Claims, error)
}
type ManagementInterface interface {
RegisterManager(s *grpc.Server)
}
// New creates a new Component
func New(ctx ttnlog.Interface, serviceName string, announcedAddress string) (*Component, error) {
component := &Component{
Config: ConfigFromViper(),
Ctx: ctx,
Identity: &pb_discovery.Announcement{
Id: viper.GetString("id"),
Description: viper.GetString("description"),
ServiceName: serviceName,
ServiceVersion: fmt.Sprintf("%s-%s (%s)", viper.GetString("version"), viper.GetString("gitCommit"), viper.GetString("buildDate")),
NetAddress: announcedAddress,
Public: viper.GetBool("public"),
},
AccessToken: viper.GetString("auth-token"),
Pool: pool.NewPool(context.Background(), pool.DefaultDialOptions...),
}
if err := component.initialize(); err != nil {
return nil, err
}
if err := component.InitAuth(); err != nil {
return nil, err
}
if serviceName != "discovery" && serviceName != "networkserver" {
var err error
component.Discovery, err = pb_discovery.NewClient(
viper.GetString("discovery-address"),
component.Identity,
func() string {
token, _ := component.BuildJWT()
return token
},
)
if err != nil {
return nil, err
}
}
component.Monitor = pb_monitor.NewClient(pb_monitor.DefaultClientConfig)
for name, addr := range viper.GetStringMapString("monitor-servers") {
component.Monitor.AddServer(name, addr)
}
return component, nil
}