fix: initialize db properly, add cfg and logs

This commit is contained in:
Jefferson Girão
2020-02-07 14:14:39 +01:00
parent 4bc315223e
commit bdcab1fee7
5 changed files with 144 additions and 7 deletions
+73
View File
@@ -0,0 +1,73 @@
package config
import (
"fmt"
"github.com/spotify/backstage/inventory/storage"
"os"
"github.com/BurntSushi/toml"
"github.com/kardianos/osext"
"github.com/sirupsen/logrus"
)
// Config struct holds the current configuration
type Config struct {
Server struct {
Address string
Port int
}
Logging struct {
Format string
Level string
}
DB storage.Config
}
// Initialize a new Config
func Initialize(configFile string) *Config {
cfg := DefaultConfig()
ReadConfigFile(cfg, getConfigFilePath(configFile))
return cfg
}
// DefaultConfig returns a Config struct with default values
func DefaultConfig() *Config {
cfg := &Config{}
cfg.Server.Address = "0.0.0.0"
cfg.Server.Port = 50051
cfg.Logging.Format = "text"
cfg.Logging.Level = "DEBUG"
return cfg
}
func getConfigFilePath(configPath string) string {
if configPath != "" {
if _, err := os.Stat(configPath); err == nil {
return configPath
}
panic(fmt.Sprintf("unable to open %s.", configPath))
}
path, _ := osext.ExecutableFolder()
path = fmt.Sprintf("%s/config.toml", path)
if _, err := os.Open(path); err == nil {
return path
}
return ""
}
func ReadConfigFile(cfg *Config, path string) {
_, err := os.Stat(path)
if err != nil {
return
}
if _, err := toml.DecodeFile(path, cfg); err != nil {
logrus.WithError(err).Fatal("unable to read config")
}
}
+3
View File
@@ -5,7 +5,10 @@ go 1.12
replace github.com/spotify/backstage/proto => ../proto
require (
github.com/BurntSushi/toml v0.3.1
github.com/golang/protobuf v1.3.3
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/sirupsen/logrus v1.4.2
github.com/spotify/backstage/proto v0.0.0-00010101000000-000000000000
go.etcd.io/bbolt v1.3.3
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3
+12
View File
@@ -1,7 +1,9 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -12,7 +14,15 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
go.etcd.io/bbolt v1.3.3 h1:MUGmc65QhB3pIlaQ5bB4LwqSj6GIonVJXpZiaKNyaKk=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -32,6 +42,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Binary file not shown.
+56 -7
View File
@@ -1,25 +1,74 @@
package main
import (
"log"
"flag"
"fmt"
"github.com/spotify/backstage/inventory/config"
"github.com/spotify/backstage/inventory/storage"
"net"
"os"
"os/signal"
"github.com/spotify/backstage/inventory/app"
pb "github.com/spotify/backstage/proto/inventory/v1"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
const (
port = ":50051"
)
func main() {
lis, err := net.Listen("tcp", port)
configFile := flag.String("config", "", "specify a config.toml file")
flag.Parse()
go catchInterrupt()
cfg := config.Initialize(*configFile)
setupLogging(cfg)
storage := getStorage(cfg)
address := fmt.Sprintf("%v:%v", cfg.Server.Address, cfg.Server.Port)
lis, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Infof("inventory service listening on: %v", address)
grpcServer := grpc.NewServer()
pb.RegisterInventoryServer(grpcServer, &app.Server{})
pb.RegisterInventoryServer(grpcServer, &app.Server{Storage: storage})
grpcServer.Serve(lis)
}
func getStorage(cfg *config.Config) *storage.Storage {
storage, err := storage.OpenStorage(cfg.DB)
if err != nil {
log.Fatalf("could not open database: %v", err)
os.Exit(1)
}
return storage
}
func setupLogging(cfg *config.Config) {
if cfg.Logging.Format == "json" {
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyLevel: "severity",
},
})
}
level, err := log.ParseLevel(cfg.Logging.Level)
if err != nil {
level = log.InfoLevel
}
log.SetOutput(os.Stdout)
log.SetLevel(level)
}
func catchInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
s := <-c
if s != os.Interrupt && s != os.Kill {
return
}
log.Info("shutting down...")
os.Exit(0)
}