<commit_before>// 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"
	"io/ioutil"
	"net"
	"os"
	"os/signal"
	"syscall"
	"time"

	"google.golang.org/grpc"

	ttnlog "github.com/TheThingsNetwork/go-utils/log"
	"github.com/TheThingsNetwork/ttn/core/broker"
	"github.com/TheThingsNetwork/ttn/core/component"
	"github.com/dgrijalva/jwt-go"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// brokerCmd represents the broker command
var brokerCmd = &cobra.Command{
	Use:   "broker",
	Short: "The Things Network broker",
	Long:  ``,
	PreRun: func(cmd *cobra.Command, args []string) {
		ctx.WithFields(ttnlog.Fields{
			"Server":             fmt.Sprintf("%s:%d", viper.GetString("broker.server-address"), viper.GetInt("broker.server-port")),
			"Announce":           fmt.Sprintf("%s:%d", viper.GetString("broker.server-address-announce"), viper.GetInt("broker.server-port")),
			"NetworkServer":      viper.GetString("broker.networkserver-address"),
			"DeduplicationDelay": viper.GetString("broker.deduplication-delay"),
		}).Info("Initializing Broker")
	},
	Run: func(cmd *cobra.Command, args []string) {
		ctx.Info("Starting")

		// Component
		component, err := component.New(ttnlog.Get(), "broker", fmt.Sprintf("%s:%d", viper.GetString("broker.server-address-announce"), viper.GetInt("broker.server-port")))
		if err != nil {
			ctx.WithError(err).Fatal("Could not initialize component")
		}

		var nsCert string
		if nsCertFile := viper.GetString("broker.networkserver-cert"); nsCertFile != "" {
			contents, err := ioutil.ReadFile(nsCertFile)
			if err != nil {
				ctx.WithError(err).Fatal("Could not get Networkserver certificate")
			}
			nsCert = string(contents)
		}

		// Broker
		broker := broker.NewBroker(
			time.Duration(viper.GetInt("broker.deduplication-delay")) * time.Millisecond,
		)
		nsToken := viper.GetString("broker.networkserver-token")
		if _, err := jwt.Parse(nsToken, func(*jwt.Token) (interface{}, error) { return nil, nil }); nsToken != "" && err != nil {
			ctx.WithError(err).Fatal("Could not read networkserver token")
		}
		broker.SetNetworkServer(viper.GetString("broker.networkserver-address"), nsCert, nsToken)
		err = broker.Init(component)
		if err != nil {
			ctx.WithError(err).Fatal("Could not initialize broker")
		}

		// gRPC Server
		lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", viper.GetString("broker.server-address"), viper.GetInt("broker.server-port")))<commit_msg>Revert "broker: Fatal error when a networkserver token can't be read"<commit_after>// 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"
	"io/ioutil"
	"net"
	"os"
	"os/signal"
	"syscall"
	"time"

	"google.golang.org/grpc"

	ttnlog "github.com/TheThingsNetwork/go-utils/log"
	"github.com/TheThingsNetwork/ttn/core/broker"
	"github.com/TheThingsNetwork/ttn/core/component"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// brokerCmd represents the broker command
var brokerCmd = &cobra.Command{
	Use:   "broker",
	Short: "The Things Network broker",
	Long:  ``,
	PreRun: func(cmd *cobra.Command, args []string) {
		ctx.WithFields(ttnlog.Fields{
			"Server":             fmt.Sprintf("%s:%d", viper.GetString("broker.server-address"), viper.GetInt("broker.server-port")),
			"Announce":           fmt.Sprintf("%s:%d", viper.GetString("broker.server-address-announce"), viper.GetInt("broker.server-port")),
			"NetworkServer":      viper.GetString("broker.networkserver-address"),
			"DeduplicationDelay": viper.GetString("broker.deduplication-delay"),
		}).Info("Initializing Broker")
	},
	Run: func(cmd *cobra.Command, args []string) {
		ctx.Info("Starting")

		// Component
		component, err := component.New(ttnlog.Get(), "broker", fmt.Sprintf("%s:%d", viper.GetString("broker.server-address-announce"), viper.GetInt("broker.server-port")))
		if err != nil {
			ctx.WithError(err).Fatal("Could not initialize component")
		}

		var nsCert string
		if nsCertFile := viper.GetString("broker.networkserver-cert"); nsCertFile != "" {
			contents, err := ioutil.ReadFile(nsCertFile)
			if err != nil {
				ctx.WithError(err).Fatal("Could not get Networkserver certificate")
			}
			nsCert = string(contents)
		}

		// Broker
		broker := broker.NewBroker(
			time.Duration(viper.GetInt("broker.deduplication-delay")) * time.Millisecond,
		)
		broker.SetNetworkServer(viper.GetString("broker.networkserver-address"), nsCert, viper.GetString("broker.networkserver-token"))
		err = broker.Init(component)
		if err != nil {
			ctx.WithError(err).Fatal("Could not initialize broker")
		}

		// gRPC Server
		lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", viper.GetString("broker.server-address"), viper.GetInt("broker.server-port")))
{'fraction_non_alphanumeric': 0.16400164001640016, 'fraction_numerical': 0.003280032800328003, 'mean_word_length': 4.421111111111111, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2792664', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1692, 'n_words': 319}