fixed ENV merge with flags.

pull/655/head
hunterlong 2020-06-10 12:41:43 -07:00
parent d3d1f9d2a0
commit 4f268edc10
6 changed files with 15 additions and 11 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/statping/statping/utils"
)
@ -15,14 +14,14 @@ var (
func parseFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&ipAddress, "ip", "s", "0.0.0.0", "server run on host")
viper.BindPFlag("ip", cmd.PersistentFlags().Lookup("ip"))
utils.Params.BindPFlag("ip", cmd.PersistentFlags().Lookup("ip"))
cmd.PersistentFlags().IntVarP(&port, "port", "p", 8080, "server port")
viper.BindPFlag("port", cmd.PersistentFlags().Lookup("port"))
utils.Params.BindPFlag("port", cmd.PersistentFlags().Lookup("port"))
cmd.PersistentFlags().IntVarP(&verboseMode, "verbose", "v", 2, "verbose logging")
viper.BindPFlag("verbose", cmd.PersistentFlags().Lookup("verbose"))
utils.Params.BindPFlag("VERBOSE", cmd.PersistentFlags().Lookup("verbose"))
cmd.PersistentFlags().StringVarP(&configFile, "config", "c", utils.Directory+"/config.yml", "path to config.yml file")
viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
utils.Params.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
}

View File

@ -29,6 +29,8 @@ var (
func init() {
process = make(chan struct{})
core.New(VERSION)
utils.InitEnvs()
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(assetsCmd)
rootCmd.AddCommand(exportCmd)
@ -37,7 +39,7 @@ func init() {
rootCmd.AddCommand(onceCmd)
rootCmd.AddCommand(envCmd)
rootCmd.AddCommand(resetCmd)
utils.InitLogs()
parseFlags(rootCmd)
}
@ -57,6 +59,7 @@ func Close() {
// main will run the Statping application
func main() {
utils.InitLogs()
go Execute()
<-process
Close()

View File

@ -41,7 +41,7 @@ func RunHTTPServer() error {
}
ip := utils.Params.GetString("HOST")
host := fmt.Sprintf("%v:%v", ip, utils.Params.GetInt64("PORT"))
host := fmt.Sprintf("%v:%v", ip, utils.Params.GetInt("PORT"))
key := utils.FileExists(utils.Directory + "/server.key")
cert := utils.FileExists(utils.Directory + "/server.crt")
@ -55,6 +55,7 @@ func RunHTTPServer() error {
router = Router()
resetCookies()
httpError = make(chan error)
if usingSSL {
go startSSLServer(ip)

View File

@ -8,7 +8,6 @@ import (
)
func startServer(host string) {
httpError = make(chan error)
httpServer = &http.Server{
Addr: host,
WriteTimeout: timeout,
@ -23,7 +22,6 @@ func startServer(host string) {
}
func startSSLServer(ip string) {
httpError = make(chan error)
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},

View File

@ -11,7 +11,10 @@ var (
Params *viper.Viper
)
func initEnvs() {
func InitEnvs() {
if Params != nil {
return
}
Params = viper.New()
Params.AutomaticEnv()

View File

@ -146,7 +146,7 @@ func createLog(dir string) error {
// InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging
func InitLogs() error {
initEnvs()
InitEnvs()
if Params.GetBool("DISABLE_LOGS") {
return nil
}