2021-07-20 12:18:00 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-20 12:25:05 +02:00
|
|
|
"flag"
|
2021-07-21 01:33:54 +02:00
|
|
|
"net/http"
|
2022-08-14 15:13:46 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-07-20 12:18:00 +02:00
|
|
|
|
2022-08-14 15:13:46 +02:00
|
|
|
"github.com/knadh/koanf"
|
|
|
|
"github.com/knadh/koanf/parsers/toml"
|
|
|
|
"github.com/knadh/koanf/parsers/json"
|
|
|
|
"github.com/knadh/koanf/parsers/yaml"
|
|
|
|
"github.com/knadh/koanf/providers/env"
|
|
|
|
"github.com/knadh/koanf/providers/file"
|
2021-07-21 01:33:54 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2021-09-29 14:03:25 +02:00
|
|
|
"go.uber.org/zap"
|
2021-07-21 00:15:57 +02:00
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/oven-exporter/api"
|
2021-07-20 12:18:00 +02:00
|
|
|
)
|
|
|
|
|
2022-08-14 15:13:46 +02:00
|
|
|
var configExtParser = map[string]koanf.Parser{
|
|
|
|
".json": json.Parser(),
|
|
|
|
".toml": toml.Parser(),
|
|
|
|
".yaml": yaml.Parser(),
|
|
|
|
".yml": yaml.Parser(),
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:18:00 +02:00
|
|
|
type configData struct {
|
2021-09-29 14:03:25 +02:00
|
|
|
log *zap.Logger
|
2022-08-14 15:13:46 +02:00
|
|
|
Log *zap.Config `config:"log"`
|
|
|
|
API api.Client `config:"api"`
|
|
|
|
Listen string `config:"listen"`
|
2021-07-20 12:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-08-14 15:13:46 +02:00
|
|
|
|
2021-07-20 12:18:00 +02:00
|
|
|
configPath := "config.toml"
|
|
|
|
|
2021-12-28 18:08:53 +01:00
|
|
|
log, _ := zap.NewProduction()
|
2021-09-29 14:03:25 +02:00
|
|
|
|
2021-07-20 12:18:00 +02:00
|
|
|
flag.StringVar(&configPath, "c", configPath, "path to configuration file")
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-08-14 15:13:46 +02:00
|
|
|
k := koanf.New("/")
|
|
|
|
|
|
|
|
if configPath != "" {
|
|
|
|
fileExt := filepath.Ext(configPath)
|
|
|
|
parser, ok := configExtParser[fileExt]
|
|
|
|
if !ok {
|
|
|
|
log.Panic("unsupported file extention:", zap.String("file-ext", fileExt))
|
|
|
|
}
|
|
|
|
if err := k.Load(file.Provider(configPath), parser); err != nil {
|
|
|
|
log.Panic("load file config:", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := k.Load(env.Provider("OVEN_E_", "/", func(s string) string {
|
|
|
|
return strings.Replace(strings.ToLower(
|
|
|
|
strings.TrimPrefix(s, "OVEN_E_")), "__", "/", -1)
|
|
|
|
}), nil); err != nil {
|
|
|
|
log.Panic("load env:", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:18:00 +02:00
|
|
|
config := &configData{}
|
2022-08-14 15:13:46 +02:00
|
|
|
if err := k.UnmarshalWithConf("", &config, koanf.UnmarshalConf{Tag: "config"}); err != nil {
|
|
|
|
log.Panic("reading config", zap.Error(err))
|
2021-09-29 14:03:25 +02:00
|
|
|
}
|
|
|
|
if config.Log != nil {
|
|
|
|
l, err := config.Log.Build()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("generate logger from config", zap.Error(err))
|
|
|
|
}
|
|
|
|
log = l
|
2021-07-20 12:18:00 +02:00
|
|
|
}
|
2021-09-29 14:03:25 +02:00
|
|
|
config.log = log
|
|
|
|
//config.SetLogger(log)
|
2021-07-21 00:15:57 +02:00
|
|
|
config.API.SetToken(config.API.Token)
|
2021-07-21 01:33:54 +02:00
|
|
|
|
|
|
|
prometheus.MustRegister(config)
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
2021-09-29 14:03:25 +02:00
|
|
|
if err := http.ListenAndServe(config.Listen, nil); err != nil {
|
|
|
|
log.Fatal("crash webserver", zap.Error(err))
|
|
|
|
}
|
2021-07-20 12:18:00 +02:00
|
|
|
}
|