package goroutineinstance import ( "encoding/json" "flag" "log" "net/http" "sync" "github.com/DataDog/gostackparse" "github.com/a-h/templ" "github.com/go-chi/chi/v5" "github.com/rs/xid" "tuxpa.in/a/pprofweb/pkg/goroutineinstance/site" ) type Instance struct { id xid.ID dat []*gostackparse.Goroutine mu sync.RWMutex once sync.Once handler http.Handler } func (i *Instance) Id() xid.ID { return i.id } func (i *Instance) ServeHTTP(tw http.ResponseWriter, tr *http.Request) { i.once.Do(func() { r := chi.NewRouter() indexHandler := templ.Handler(site.Index()) r.Get("/", indexHandler.ServeHTTP) r.Get("/raw", func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(i.dat) }) i.handler = r }) log.Printf("serve goroutine instance %s %s", i.id, tr.URL.String()) i.handler.ServeHTTP(tw, tr) } // Mostly copied from https://github.com/google/pprof/blob/master/internal/driver/flags.go type pprofFlags struct { args []string s flag.FlagSet usage []string }