Ejemplo n.º 1
0
// NewServer sets up a new server instance.
func NewServer(ctx log.Interface, address, templateFilename string) Server {
	return Server{
		Log: ctx.WithFields(log.Fields{
			"module":  "Server",
			"address": address,
		}),
		IndexTemplate: template.Must(template.ParseFiles(templateFilename)),
		Address:       address,
		Hub:           NewGameHub(ctx),
	}
}
Ejemplo n.º 2
0
// NewGame creates a new game instance.
func NewGame(ctx log.Interface, id string) Game {
	gs := NewGameState(ctx)
	cp := NewCommandProcessor(&gs)

	return Game{
		Log: ctx.WithFields(log.Fields{
			"module": "Game",
			"id":     id,
		}),
		State:            &gs,
		CommandProcessor: &cp,
		register:         make(chan *User),
		unregister:       make(chan *User),
		commands:         make(chan Command),
	}
}
Ejemplo n.º 3
0
// New creates a new Component
func New(ctx log.Interface, serviceName string, announcedAddress string) (*Component, error) {
	go func() {
		memstats := new(runtime.MemStats)
		for range time.Tick(time.Minute) {
			runtime.ReadMemStats(memstats)
			ctx.WithFields(log.Fields{
				"Goroutines": runtime.NumGoroutine(),
				"Memory":     float64(memstats.Alloc) / 1000000,
			}).Debugf("Stats")
		}
	}()

	// Disable gRPC tracing
	// SEE: https://github.com/grpc/grpc-go/issues/695
	grpc.EnableTracing = false

	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"),
	}

	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
		}
	}

	if healthPort := viper.GetInt("health-port"); healthPort > 0 {
		http.HandleFunc("/healthz", func(w http.ResponseWriter, req *http.Request) {
			switch component.GetStatus() {
			case StatusHealthy:
				w.WriteHeader(200)
				w.Write([]byte("Status is HEALTHY"))
				return
			case StatusUnhealthy:
				w.WriteHeader(503)
				w.Write([]byte("Status is UNHEALTHY"))
				return
			}
		})
		go http.ListenAndServe(fmt.Sprintf(":%d", healthPort), nil)
	}

	if monitors := viper.GetStringMapString("monitor-servers"); len(monitors) != 0 {
		component.Monitors = make(map[string]*pb_monitor.Client)
		for name, addr := range monitors {
			var err error
			component.Monitors[name], err = pb_monitor.NewClient(ctx.WithField("Monitor", name), addr)
			if err != nil {
				return nil, err
			}
		}
	}

	return component, nil
}
Ejemplo n.º 4
0
				BufferSize: 10,
			}), logLevel))
		}

		ctx = &log.Logger{
			Handler: multiHandler.New(logHandlers...),
		}

		// Set the API/gRPC logger
		ttnlog.Set(apex.Wrap(ctx))
		grpclog.SetLogger(grpc.Wrap(ttnlog.Get()))

		ctx.WithFields(log.Fields{
			"ComponentID":              viper.GetString("id"),
			"Description":              viper.GetString("description"),
			"Discovery Server Address": viper.GetString("discovery-address"),
			"Auth Servers":             viper.GetStringMapString("auth-servers"),
			"Monitors":                 viper.GetStringMapString("monitor-servers"),
		}).Info("Initializing The Things Network")
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		if logFile != nil {
			logFile.Close()
		}
	},
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	defer func() {