From 1105ce3ebd1b7ba946676dfb60e9aab1f8970615 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Fri, 3 Dec 2021 02:06:33 +0100 Subject: [PATCH] Initialize config default values on app start --- frontend/config/config.go | 36 ++++++++++++++++++++++++++++++++++++ go.mod | 6 ++++++ 2 files changed, 42 insertions(+) diff --git a/frontend/config/config.go b/frontend/config/config.go index 3fd2122..59a9949 100644 --- a/frontend/config/config.go +++ b/frontend/config/config.go @@ -1,5 +1,13 @@ package config +import ( + "strings" + + "github.com/sirupsen/logrus" + "github.com/spf13/pflag" + "github.com/spf13/viper" +) + // Content contains content-specific configuration type Content struct { Backend string `mapstructure:"backend"` @@ -16,3 +24,31 @@ type Config struct { Unix string `mapstructure:"unix"` Content Content `mapstructure:"content"` } + +// initialize default values on app-start +func init() { + + pflag.BoolP("version", "V", false, "get application version") + pflag.BoolP("verbose", "v", false, "enable verbose logging") + pflag.StringP("config", "c", string("frontend.yaml"), "config file path") + + pflag.String("addr", "127.0.0.1:8000", "tcp addr to listen") + pflag.String("unix", "", "unix socket path to listen") + pflag.String("base", "", "http URI prefix") + + pflag.String("content.backend", "demo.ghost.io:443", "ghost backend addr") + pflag.String("content.key", "22444f78447824223cefc48062", "ghost content api key") + pflag.String("content.pinned", "contact", "pinned page slug") + pflag.Int("content.postsPerPage", 5, "amount of posts per page") + + pflag.Parse() + + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() + viper.BindPFlags(pflag.CommandLine) + + if viper.GetBool("verbose") { + logrus.SetLevel(logrus.DebugLevel) + logrus.Debug("Verbose mode") + } +} diff --git a/go.mod b/go.mod index e5f0b6d..4730479 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module code.tokarch.uk/mainnika/nikita-tokarch-uk go 1.17 + +require ( + github.com/sirupsen/logrus v1.8.1 + github.com/spf13/pflag v1.0.5 + github.com/spf13/viper v1.9.0 +)