This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
discovery.go
146 lines (124 loc) · 5.48 KB
/
discovery.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
// 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 (
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
pb "github.com/TheThingsNetwork/api/discovery"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api/pool"
"github.com/TheThingsNetwork/ttn/core/component"
"github.com/TheThingsNetwork/ttn/core/discovery"
"github.com/TheThingsNetwork/ttn/core/discovery/announcement"
"github.com/TheThingsNetwork/ttn/core/proxy"
"github.com/TheThingsNetwork/ttn/core/proxy/jsonpb"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"gopkg.in/redis.v5"
)
// discoveryCmd represents the discovery command
var discoveryCmd = &cobra.Command{
Use: "discovery",
Short: "The Things Network discovery",
Long: ``,
PreRun: func(cmd *cobra.Command, args []string) {
ctx.WithFields(ttnlog.Fields{
"Server": fmt.Sprintf("%s:%d", viper.GetString("discovery.server-address"), viper.GetInt("discovery.server-port")),
"HTTP Proxy": fmt.Sprintf("%s:%d", viper.GetString("discovery.http-address"), viper.GetInt("discovery.http-port")),
"Database": fmt.Sprintf("%s/%d", viper.GetString("discovery.redis-address"), viper.GetInt("discovery.redis-db")),
}).Info("Initializing Discovery")
},
Run: func(cmd *cobra.Command, args []string) {
ctx.Info("Starting")
// Redis Client
client := redis.NewClient(&redis.Options{
Addr: viper.GetString("discovery.redis-address"),
Password: viper.GetString("discovery.redis-password"),
DB: viper.GetInt("discovery.redis-db"),
})
if err := connectRedis(client); err != nil {
ctx.WithError(err).Fatal("Could not initialize database connection")
}
// Component
component, err := component.New(ttnlog.Get(), "discovery", fmt.Sprintf("%s:%d", "localhost", viper.GetInt("discovery.server-port")))
if err != nil {
ctx.WithError(err).Fatal("Could not initialize component")
}
// Discovery Server
discovery := discovery.NewRedisDiscovery(client)
if viper.GetBool("discovery.cache") {
discovery.WithCache(announcement.DefaultCacheOptions)
}
discovery.MonitorServiceConnectivity()
discovery.WithMasterAuthServers(viper.GetStringSlice("discovery.master-auth-servers")...)
err = discovery.Init(component)
if err != nil {
ctx.WithError(err).Fatal("Could not initialize discovery")
}
// gRPC Server
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", viper.GetString("discovery.server-address"), viper.GetInt("discovery.server-port")))
if err != nil {
ctx.WithError(err).Fatal("Could not start gRPC server")
}
grpc := grpc.NewServer(component.ServerOptions()...)
// Register and Listen
discovery.RegisterRPC(grpc)
component.RegisterHealthServer(grpc) // must be last one
go grpc.Serve(lis)
if viper.GetString("discovery.http-address") != "" && viper.GetInt("discovery.http-port") != 0 {
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.RegisterDiscoveryHandler(netCtx, mux, proxyConn)
prxy := proxy.WithLogger(mux, ctx)
prxy = proxy.WithPagination(prxy)
go func() {
err := http.ListenAndServe(
fmt.Sprintf("%s:%d", viper.GetString("discovery.http-address"), viper.GetInt("discovery.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")
grpc.Stop()
},
}
func init() {
RootCmd.AddCommand(discoveryCmd)
discoveryCmd.Flags().String("redis-address", "localhost:6379", "Redis server and port")
viper.BindPFlag("discovery.redis-address", discoveryCmd.Flags().Lookup("redis-address"))
discoveryCmd.Flags().String("redis-password", "", "Redis password")
viper.BindPFlag("discovery.redis-password", discoveryCmd.Flags().Lookup("redis-password"))
discoveryCmd.Flags().Int("redis-db", 0, "Redis database")
viper.BindPFlag("discovery.redis-db", discoveryCmd.Flags().Lookup("redis-db"))
discoveryCmd.Flags().String("server-address", "0.0.0.0", "The IP address to listen for communication")
discoveryCmd.Flags().Int("server-port", 1900, "The port for communication")
viper.BindPFlag("discovery.server-address", discoveryCmd.Flags().Lookup("server-address"))
viper.BindPFlag("discovery.server-port", discoveryCmd.Flags().Lookup("server-port"))
discoveryCmd.Flags().Bool("cache", false, "Add a cache in front of the database")
viper.BindPFlag("discovery.cache", discoveryCmd.Flags().Lookup("cache"))
discoveryCmd.Flags().StringSlice("master-auth-servers", []string{"ttn-account-v2"}, "Auth servers that are allowed to manage this network")
viper.BindPFlag("discovery.master-auth-servers", discoveryCmd.Flags().Lookup("master-auth-servers"))
discoveryCmd.Flags().String("http-address", "0.0.0.0", "The IP address where the gRPC proxy should listen")
discoveryCmd.Flags().Int("http-port", 8080, "The port where the gRPC proxy should listen")
viper.BindPFlag("discovery.http-address", discoveryCmd.Flags().Lookup("http-address"))
viper.BindPFlag("discovery.http-port", discoveryCmd.Flags().Lookup("http-port"))
}