Skip to content

Commit

Permalink
Add debug flag
Browse files Browse the repository at this point in the history
This muxes pprof in on the normal http-socket on
/debug/pprof as expected
  • Loading branch information
bahner committed Apr 7, 2024
1 parent 52d4979 commit 1e89f0d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 93 deletions.
94 changes: 35 additions & 59 deletions config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"os"
"sync"

log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)

var (
CommonFlags = pflag.NewFlagSet("common", pflag.ContinueOnError)

commonOnce sync.Once

debugFlag bool = false
forceFlag bool = false

generateCommandFlag bool = false
showConfigCommandFlag bool = false
versionCommandFlag bool = false
)

func InitCommon() {
Expand All @@ -23,14 +29,14 @@ func InitCommon() {
CommonFlags.StringP("config", "c", "", "Config file to use.")
CommonFlags.StringP("profile", "p", "", "Config profile (name) to use.")

CommonFlags.Bool("show-config", false, "Whether to print the config.")

CommonFlags.BoolP("version", "v", false, "Print version and exit.")
// COmmands
CommonFlags.BoolVar(&showConfigCommandFlag, "show-config", false, "Whether to print the config.")
CommonFlags.BoolVarP(&versionCommandFlag, "version", "v", false, "Print version and exit.")
CommonFlags.BoolVar(&generateCommandFlag, "generate", false, "Generates a new keyset")

CommonFlags.Bool("generate", false, "Generates a new keyset")
CommonFlags.Bool("force", false, "Forces regneration of config keyset and publishing")

CommonFlags.String("debug-socket", defaultDebugSocket, "Port to listen on for debug endpoints")
// Flags
CommonFlags.BoolVar(&forceFlag, "force", false, "Forces regneration of config keyset and publishing")
CommonFlags.BoolVar(&debugFlag, "debug", false, "Port to listen on for debug endpoints")

if HelpNeeded() {
fmt.Println("Common flags:")
Expand All @@ -41,57 +47,6 @@ func InitCommon() {
})
}

func GenerateFlag() bool {
// This will exit when done. It will also publish if applicable.
generateFlag, err := CommonFlags.GetBool("generate")
if err != nil {
log.Warnf("config.init: %v", err)
return false
}

return generateFlag
}

func PublishFlag() bool {
publishFlag, err := CommonFlags.GetBool("publish")
if err != nil {
log.Warnf("config.init: %v", err)
return false
}

return publishFlag
}

func ShowConfigFlag() bool {
showConfigFlag, err := CommonFlags.GetBool("show-config")
if err != nil {
log.Warnf("config.init: %v", err)
return false
}

return showConfigFlag
}

func versionFlag() bool {
versionFlag, err := CommonFlags.GetBool("version")
if err != nil {
log.Warnf("config.init: %v", err)
return false
}

return versionFlag
}

func ForceFlag() bool {
forceFlag, err := CommonFlags.GetBool("force")
if err != nil {
log.Warnf("config.init: %v", err)
return false
}

return forceFlag
}

/*
Parse common flags.
Expand All @@ -102,6 +57,7 @@ Set exitOnHelp to true if you want the program to exit
after help is printed. This is useful for the main function,
when this is the last flag parsing function called.
*/

func ParseCommonFlags(exitOnHelp bool) {

InitCommon()
Expand All @@ -114,3 +70,23 @@ func ParseCommonFlags(exitOnHelp bool) {
os.Exit(0)
}
}

func Debug() bool {
return debugFlag
}

func ForceFlag() bool {
return forceFlag
}

func GenerateFlag() bool {
return generateCommandFlag
}

func ShowConfigFlag() bool {
return showConfigCommandFlag
}

func VersionFlag() bool {
return versionCommandFlag
}
9 changes: 1 addition & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const (
configDirMode os.FileMode = 0700
configFileMode os.FileMode = 0600
dataHomeMode os.FileMode = 0755

defaultDebugSocket = "127.0.0.1:6060"
)

type Config interface {
Expand All @@ -33,11 +31,6 @@ type Config interface {
func Init() error {

var err error

//VIPER CONFIGURATION
viper.BindPFlag("http.debug-socket", CommonFlags.Lookup("debug-socket"))
viper.SetDefault("http.debug-socket", defaultDebugSocket)

// Read the config file and environment variables.
viper.SetEnvPrefix(ENV_PREFIX)
viper.AutomaticEnv()
Expand All @@ -64,7 +57,7 @@ func Init() error {
}

// Handle the easy flags first.
if versionFlag() {
if versionCommandFlag {
fmt.Println(VERSION)
os.Exit(0)
}
Expand Down
26 changes: 0 additions & 26 deletions config/debug.go

This file was deleted.

6 changes: 6 additions & 0 deletions ui/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/bahner/go-ma-actor/config"

_ "net/http/pprof"

log "github.com/sirupsen/logrus"
)

Expand All @@ -23,6 +25,10 @@ func Start(h WebHandler) {
mux := http.NewServeMux()
mux.Handle("/", h)

if config.Debug() {
mux.Handle("/debug/pprof/", http.DefaultServeMux)
}

log.Infof("Listening on %s", config.HttpSocket())

// IN relay mode we want to stop here.
Expand Down

0 comments on commit 1e89f0d

Please sign in to comment.