1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:04:42 -07:00
go/internal/lsp/debug/serve.go
Ian Cottrell b97706b7f6 internal/lsp: add debug page serving
This adds a framework for gopls server debugging pages, and adds the standard profiling pages to it.

Change-Id: Ie319e4ad070ac41b2ae7791cb3e0e5bb4ae12ef4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179277
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-05-30 00:16:15 +00:00

89 lines
1.9 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package debug
import (
"context"
"html/template"
"log"
"net"
"net/http"
_ "net/http/pprof" // pull in the standard pprof handlers
)
func init() {
http.HandleFunc("/", Render(mainTmpl, nil))
http.HandleFunc("/debug/", Render(debugTmpl, nil))
}
// Serve starts and runs a debug server in the background.
// It also logs the port the server starts on, to allow for :0 auto assigned
// ports.
func Serve(ctx context.Context, addr string) error {
if addr == "" {
return nil
}
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
log.Printf("Debug serving on port: %d", listener.Addr().(*net.TCPAddr).Port)
go func() {
if err := http.Serve(listener, nil); err != nil {
log.Printf("Debug server failed with %v", err)
return
}
log.Printf("Debug server finished")
}()
return nil
}
func Render(tmpl *template.Template, fun func(*http.Request) interface{}) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var data interface{}
if fun != nil {
data = fun(r)
}
if err := tmpl.Execute(w, data); err != nil {
log.Print(err)
}
}
}
var BaseTemplate = template.Must(template.New("").Parse(`
<html>
<head>
<title>{{template "title"}}</title>
<style>
.profile-name{
display:inline-block;
width:6rem;
}
</style>
</head>
<body>
{{template "title"}}
<br>
{{block "body" .Data}}
Unknown page
{{end}}
</body>
</html>
`))
var mainTmpl = template.Must(template.Must(BaseTemplate.Clone()).Parse(`
{{define "title"}}GoPls server information{{end}}
{{define "body"}}
<A href="/debug/">Debug</A>
{{end}}
`))
var debugTmpl = template.Must(template.Must(BaseTemplate.Clone()).Parse(`
{{define "title"}}GoPls Debug pages{{end}}
{{define "body"}}
<A href="/debug/pprof">Profiling</A>
{{end}}
`))