add reflection to gRPC server
only log to file if configured
This commit is contained in:
parent
bcdf42b277
commit
aa6d0ed864
1 changed files with 13 additions and 6 deletions
19
main.go
19
main.go
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog/pkgerrors"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
@ -16,7 +17,7 @@ import (
|
|||
|
||||
type Config struct {
|
||||
Outputs []string `yaml:"outputs"`
|
||||
Address string `yaml:"address" env:"ADDRESS" env-default:":3000"`
|
||||
Address string `yaml:"address" env:"ADDRESS" env-default:"localhost:3000"`
|
||||
LogFile string `yaml:"logfile" env:"LOGFILE" env-default:"./gstreamer-graphix.log"`
|
||||
}
|
||||
|
||||
|
@ -28,19 +29,24 @@ func main() {
|
|||
log.Logger = log.Output(consoleWriter).With().Timestamp().Caller().Logger()
|
||||
|
||||
if err := cleanenv.ReadConfig("config.toml", &cfg); err != nil {
|
||||
log.Fatal().Msgf("No configfile: ", err)
|
||||
log.Info().Msgf("No configfile, continuing without: ", err)
|
||||
}
|
||||
|
||||
fset := flag.NewFlagSet("config", flag.ContinueOnError)
|
||||
fset.Usage = cleanenv.FUsage(fset.Output(), &cfg, nil, fset.Usage)
|
||||
fset.Parse(os.Args[1:])
|
||||
|
||||
l, err := os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
|
||||
multi := zerolog.MultiLevelWriter(consoleWriter, l)
|
||||
log.Logger = zerolog.New(multi).With().Timestamp().Caller().Logger()
|
||||
if cfg.LogFile != "" {
|
||||
l, _ := os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
|
||||
multi := zerolog.MultiLevelWriter(consoleWriter, l)
|
||||
log.Logger = zerolog.New(multi).With().Timestamp().Caller().Logger()
|
||||
}
|
||||
|
||||
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
|
||||
if log.Logger.GetLevel() == zerolog.DebugLevel {
|
||||
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
|
||||
}
|
||||
|
||||
log.Info().Msgf("listening on %s", cfg.Address)
|
||||
ln, err := net.Listen("tcp", cfg.Address)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
|
@ -51,6 +57,7 @@ func main() {
|
|||
pipelineService := &api.PipelineService{Gstreamer: gst}
|
||||
|
||||
g := grpc.NewServer()
|
||||
reflection.Register(g)
|
||||
api.RegisterPipelineServiceServer(g, pipelineService)
|
||||
|
||||
log.Fatal().Msgf("Failed to serve: %v", g.Serve(ln))
|
||||
|
|
Loading…
Reference in a new issue