1
0
Fork 0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-11-09 13:50:03 +00:00

Improve config loading

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-03-11 11:16:48 +01:00
parent a0e3ab6575
commit 13ac3b6bd1
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 24 additions and 13 deletions

35
app.go
View file

@ -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()

1
go.mod
View file

@ -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

1
go.sum
View file

@ -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=