diff --git a/internal/config/server.go b/internal/config/server.go index c98e2337..b68df15a 100644 --- a/internal/config/server.go +++ b/internal/config/server.go @@ -12,6 +12,7 @@ type Server struct { Key string Bind string Static string + PProf bool CORS []string } @@ -36,6 +37,11 @@ func (Server) Init(cmd *cobra.Command) error { return err } + cmd.PersistentFlags().Bool("server.pprof", false, "enable pprof endpoint available at /debug/pprof") + if err := viper.BindPFlag("server.pprof", cmd.PersistentFlags().Lookup("server.pprof")); err != nil { + return err + } + cmd.PersistentFlags().StringSlice("server.cors", []string{"*"}, "list of allowed origins for CORS") if err := viper.BindPFlag("server.cors", cmd.PersistentFlags().Lookup("server.cors")); err != nil { return err @@ -49,6 +55,7 @@ func (s *Server) Set() { s.Key = viper.GetString("server.key") s.Bind = viper.GetString("server.bind") s.Static = viper.GetString("server.static") + s.PProf = viper.GetBool("server.pprof") s.CORS = viper.GetStringSlice("server.cors") in, _ := utils.ArrayIn("*", s.CORS) diff --git a/internal/http/debug.go b/internal/http/debug.go new file mode 100644 index 00000000..ef72b35a --- /dev/null +++ b/internal/http/debug.go @@ -0,0 +1,36 @@ +package http + +import ( + "net/http" + "net/http/pprof" + + "github.com/go-chi/chi" + + "demodesk/neko/internal/types" +) + +func pprofHandler(r types.Router) { + r.Get("/debug/pprof/", func(w http.ResponseWriter, r *http.Request) error { + pprof.Index(w, r) + return nil + }) + + r.Get("/debug/pprof/{action}", func(w http.ResponseWriter, r *http.Request) error { + action := chi.URLParam(r, "action") + + switch action { + case "cmdline": + pprof.Cmdline(w, r) + case "profile": + pprof.Profile(w, r) + case "symbol": + pprof.Symbol(w, r) + case "trace": + pprof.Trace(w, r) + default: + pprof.Handler(action).ServeHTTP(w, r) + } + + return nil + }) +} diff --git a/internal/http/manager.go b/internal/http/manager.go index 20169a2e..73c4ed25 100644 --- a/internal/http/manager.go +++ b/internal/http/manager.go @@ -57,6 +57,10 @@ func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, c }) } + if config.PProf { + pprofHandler(router) + } + return &HttpManagerCtx{ logger: logger, config: config,