Skip to content

Commit

Permalink
fixed ENV merge with flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Jun 10, 2020
1 parent d3d1f9d commit 4f268ed
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
9 changes: 4 additions & 5 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/statping/statping/utils"
)

Expand All @@ -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"))
}
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,7 +39,7 @@ func init() {
rootCmd.AddCommand(onceCmd)
rootCmd.AddCommand(envCmd)
rootCmd.AddCommand(resetCmd)
utils.InitLogs()

parseFlags(rootCmd)
}

Expand All @@ -57,6 +59,7 @@ func Close() {

// main will run the Statping application
func main() {
utils.InitLogs()
go Execute()
<-process
Close()
Expand Down
3 changes: 2 additions & 1 deletion handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -55,6 +55,7 @@ func RunHTTPServer() error {

router = Router()
resetCookies()
httpError = make(chan error)

if usingSSL {
go startSSLServer(ip)
Expand Down
2 changes: 0 additions & 2 deletions handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func startServer(host string) {
httpError = make(chan error)
httpServer = &http.Server{
Addr: host,
WriteTimeout: timeout,
Expand All @@ -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},
Expand Down
5 changes: 4 additions & 1 deletion utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ var (
Params *viper.Viper
)

func initEnvs() {
func InitEnvs() {
if Params != nil {
return
}
Params = viper.New()
Params.AutomaticEnv()

Expand Down
2 changes: 1 addition & 1 deletion utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4f268ed

Please sign in to comment.