2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/netstate"
|
|
|
|
proto "storj.io/storj/protos/netstate"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
port int
|
|
|
|
dbPath string
|
|
|
|
prod bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func initializeFlags() {
|
|
|
|
flag.IntVar(&port, "port", 8080, "port")
|
|
|
|
flag.StringVar(&dbPath, "db", "netstate.db", "db path")
|
|
|
|
flag.BoolVar(&prod, "prod", false, "type of environment where this service runs")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
initializeFlags()
|
|
|
|
|
|
|
|
// No err here because no vars passed into NewDevelopment().
|
|
|
|
// The default won't return an error, but if args are passed in,
|
|
|
|
// then there will need to be error handling.
|
|
|
|
logger, _ := zap.NewDevelopment()
|
|
|
|
if prod {
|
|
|
|
logger, _ = zap.NewProduction()
|
|
|
|
}
|
|
|
|
defer logger.Sync()
|
|
|
|
|
|
|
|
bdb, err := boltdb.New(logger, dbPath)
|
|
|
|
if err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
logger.Fatal("failed to initiate boltdb", zap.Error(err))
|
2018-05-15 01:31:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer bdb.Close()
|
|
|
|
|
|
|
|
// start grpc server
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("failed to listen", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
proto.RegisterNetStateServer(grpcServer, netstate.NewServer(bdb, logger))
|
2018-05-30 03:47:40 +01:00
|
|
|
logger.Debug(fmt.Sprintf("server listening on port %d", port))
|
2018-05-15 01:31:26 +01:00
|
|
|
|
|
|
|
defer grpcServer.GracefulStop()
|
|
|
|
err = grpcServer.Serve(lis)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to serve:", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|