From d756e8bdc790c4e67b4e96f9c1474d5cfa2e690b Mon Sep 17 00:00:00 2001 From: Lars Bahner Date: Sun, 7 Apr 2024 23:16:23 +0200 Subject: [PATCH] Add debug flag This muxes pprof in on the normal http-socket on /debug/pprof as expected --- config/common.go | 94 ++++++++++++++++++------------------------------ config/config.go | 9 +---- config/debug.go | 26 -------------- ui/web/web.go | 10 ++++-- 4 files changed, 44 insertions(+), 95 deletions(-) delete mode 100644 config/debug.go diff --git a/config/common.go b/config/common.go index b3101b9..def2b73 100644 --- a/config/common.go +++ b/config/common.go @@ -5,7 +5,6 @@ import ( "os" "sync" - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" ) @@ -13,6 +12,13 @@ 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() { @@ -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:") @@ -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. @@ -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() @@ -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 +} diff --git a/config/config.go b/config/config.go index ff789d8..2b9b416 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { @@ -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() @@ -64,7 +57,7 @@ func Init() error { } // Handle the easy flags first. - if versionFlag() { + if versionCommandFlag { fmt.Println(VERSION) os.Exit(0) } diff --git a/config/debug.go b/config/debug.go deleted file mode 100644 index 659aa7b..0000000 --- a/config/debug.go +++ /dev/null @@ -1,26 +0,0 @@ -//go:build debug - -package config - -import ( - "net/http" - _ "net/http/pprof" - "runtime" -) - -func init() { - // Assume you have a function setupDebugHandlers to register debug routes - setupDebugHandlers() -} - -func setupDebugHandlers() { - // Register your pprof handlers or other debug routes here - // Since "net/http/pprof" is imported above, its init function automatically registers its routes with the default mux - go http.ListenAndServe(HttpDebugSocket(), nil) - - http.HandleFunc("/force-gc", func(w http.ResponseWriter, r *http.Request) { - // Force a garbage collection - runtime.GC() - w.Write([]byte("Garbage collection triggered")) - }) -} diff --git a/ui/web/web.go b/ui/web/web.go index 6a13417..7c8d3ba 100644 --- a/ui/web/web.go +++ b/ui/web/web.go @@ -7,11 +7,13 @@ import ( "github.com/bahner/go-ma-actor/config" + _ "net/http/pprof" + log "github.com/sirupsen/logrus" ) // Start the WebU with the given handler. -func Start(h WebHandler) { +func Start(handler WebHandler) { fmt.Println("Starting web server...") @@ -21,7 +23,11 @@ func Start(h WebHandler) { // Start a simple web server to handle incoming requests. // This is defined in web.go. It makes it possible to add extra parameters to the handler. mux := http.NewServeMux() - mux.Handle("/", h) + mux.Handle("/", handler) + + if config.Debug() { + mux.Handle("/debug/pprof/", http.DefaultServeMux) + } log.Infof("Listening on %s", config.HttpSocket())