From 13ac3b6bd12ee052b19c742e304e53dfa8d7e369 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 11 Mar 2021 11:16:48 +0100 Subject: [PATCH] Improve config loading Signed-off-by: Knut Ahlers --- app.go | 35 ++++++++++++++++++++++------------- go.mod | 1 + go.sum | 1 + 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app.go b/app.go index 0ef08fc..9404d60 100644 --- a/app.go +++ b/app.go @@ -5,8 +5,6 @@ import ( "crypto/sha1" "errors" "fmt" - "io/ioutil" - "log" "net/http" "net/url" "os" @@ -15,16 +13,16 @@ import ( "text/template" "time" - "gopkg.in/yaml.v2" - + "github.com/gorilla/mux" + log "github.com/sirupsen/logrus" + "github.com/tdewolff/minify" + "github.com/tdewolff/minify/svg" "golang.org/x/net/context" + "gopkg.in/yaml.v2" "github.com/Luzifer/badge-gen/cache" "github.com/Luzifer/go_helpers/v2/accessLogger" "github.com/Luzifer/rconfig/v2" - "github.com/gorilla/mux" - "github.com/tdewolff/minify" - "github.com/tdewolff/minify/svg" ) const ( @@ -100,20 +98,31 @@ func main() { cfg.Listen = fmt.Sprintf(":%d", cfg.Port) } - log.Printf("badge-gen %s started...", version) + log.Infof("badge-gen %s started...", version) var err error cacheStore, err = cache.GetCacheByURI(cfg.Cache) if err != nil { - log.Fatalf("Unable to open cache: %s", err) + log.WithError(err).Fatal("Unable to open cache") } - if _, err := os.Stat(cfg.ConfStorage); err == nil { - rawConfig, _ := ioutil.ReadFile(cfg.ConfStorage) - if err := yaml.Unmarshal(rawConfig, &configStore); err != nil { - log.Fatalf("Unable to parse config: %s", err) + f, err := os.Open(cfg.ConfStorage) + switch { + case err == nil: + yamlDecoder := yaml.NewDecoder(f) + yamlDecoder.SetStrict(true) + if err = yamlDecoder.Decode(&configStore); err != nil { + log.WithError(err).Fatal("Unable to parse config") } log.Printf("Loaded %d value pairs into configuration store", len(configStore)) + + f.Close() + + case os.IsNotExist(err): + // Do nothing + + default: + log.WithError(err).Fatal("Unable to open config") } r := mux.NewRouter().UseEncodedPath() diff --git a/go.mod b/go.mod index 461e44e..f89787f 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gorilla/mux v1.8.0 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.9.0 + github.com/sirupsen/logrus v1.7.0 github.com/tdewolff/minify v2.3.6+incompatible github.com/tdewolff/parse v2.3.4+incompatible // indirect github.com/tdewolff/test v1.0.6 // indirect diff --git a/go.sum b/go.sum index 2dca6eb..5534645 100644 --- a/go.sum +++ b/go.sum @@ -271,6 +271,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= +github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=