2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-01 15:07:58 +01:00
|
|
|
"context"
|
2018-05-15 01:31:26 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2018-06-27 09:02:49 +01:00
|
|
|
"github.com/spf13/cobra"
|
2018-06-04 17:45:07 +01:00
|
|
|
"github.com/spf13/viper"
|
2018-05-15 01:31:26 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
2018-06-01 15:07:58 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/netstate"
|
2018-06-01 15:07:58 +01:00
|
|
|
"storj.io/storj/pkg/process"
|
2018-05-15 01:31:26 +01:00
|
|
|
proto "storj.io/storj/protos/netstate"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-06-01 15:07:58 +01:00
|
|
|
port = flag.Int("port", 8080, "port")
|
|
|
|
dbPath = flag.String("db", "netstate.db", "db path")
|
2018-05-15 01:31:26 +01:00
|
|
|
)
|
|
|
|
|
2018-06-27 09:02:49 +01:00
|
|
|
func (s *serv) Process(ctx context.Context, _ *cobra.Command, _ []string) error {
|
2018-06-13 19:22:32 +01:00
|
|
|
bdb, err := boltdb.NewClient(s.logger, *dbPath, boltdb.PointerBucket)
|
2018-06-04 17:45:07 +01:00
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
2018-06-01 15:07:58 +01:00
|
|
|
return err
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
defer bdb.Close()
|
|
|
|
|
|
|
|
// start grpc server
|
2018-06-04 17:45:07 +01:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
2018-06-01 15:07:58 +01:00
|
|
|
return err
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer()
|
2018-06-04 17:45:07 +01:00
|
|
|
|
2018-06-01 15:07:58 +01:00
|
|
|
proto.RegisterNetStateServer(grpcServer, netstate.NewServer(bdb, s.logger))
|
|
|
|
s.logger.Debug(fmt.Sprintf("server listening on port %d", *port))
|
2018-05-15 01:31:26 +01:00
|
|
|
|
|
|
|
defer grpcServer.GracefulStop()
|
2018-06-01 15:07:58 +01:00
|
|
|
return grpcServer.Serve(lis)
|
|
|
|
}
|
|
|
|
|
|
|
|
type serv struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
metrics *monkit.Registry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serv) SetLogger(l *zap.Logger) error {
|
|
|
|
s.logger = l
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-04 17:45:07 +01:00
|
|
|
func setEnv() error {
|
|
|
|
viper.SetEnvPrefix("API")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:07:58 +01:00
|
|
|
func (s *serv) SetMetricHandler(m *monkit.Registry) error {
|
|
|
|
s.metrics = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serv) InstanceID() string { return "" }
|
|
|
|
|
|
|
|
func main() {
|
2018-06-04 17:45:07 +01:00
|
|
|
setEnv()
|
2018-06-01 15:07:58 +01:00
|
|
|
process.Must(process.Main(&serv{}))
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|