| // Copyright 2009 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 rpc | |
| /* | |
| Some HTML presented at http://machine:port/debug/rpc | |
| Lists services, their methods, and some statistics, still rudimentary. | |
| */ | |
| import ( | |
| "fmt" | |
| "html/template" | |
| "net/http" | |
| "slices" | |
| "strings" | |
| ) | |
| const debugText = `<html> | |
| <body> | |
| <title>Services</title> | |
| {{range .}} | |
| <hr> | |
| Service {{.Name}} | |
| <hr> | |
| <table> | |
| <th align=center>Method</th><th align=center>Calls</th> | |
| {{range .Method}} | |
| <tr> | |
| <td align=left font=fixed>{{.Name}}({{.Type.ArgType}}, {{.Type.ReplyType}}) error</td> | |
| <td align=center>{{.Type.NumCalls}}</td> | |
| </tr> | |
| {{end}} | |
| </table> | |
| {{end}} | |
| </body> | |
| </html>` | |
| var debug = template.Must(template.New("RPC debug").Parse(debugText)) | |
| // If set, print log statements for internal and I/O errors. | |
| var debugLog = false | |
| type debugMethod struct { | |
| Type *methodType | |
| Name string | |
| } | |
| type methodArray []debugMethod | |
| type debugService struct { | |
| Service *service | |
| Name string | |
| Method []debugMethod | |
| } | |
| type serviceArray []debugService | |
| type debugHTTP struct { | |
| *Server | |
| } | |
| // Runs at /debug/rpc | |
| func (server debugHTTP) ServeHTTP(w http.ResponseWriter, req *http.Request) { | |
| // Build a sorted version of the data. | |
| var services serviceArray | |
| server.serviceMap.Range(func(snamei, svci any) bool { | |
| svc := svci.(*service) | |
| ds := debugService{svc, snamei.(string), make([]debugMethod, 0, len(svc.method))} | |
| for mname, method := range svc.method { | |
| ds.Method = append(ds.Method, debugMethod{method, mname}) | |
| } | |
| slices.SortFunc(ds.Method, func(a, b debugMethod) int { | |
| return strings.Compare(a.Name, b.Name) | |
| }) | |
| services = append(services, ds) | |
| return true | |
| }) | |
| slices.SortFunc(services, func(a, b debugService) int { | |
| return strings.Compare(a.Name, b.Name) | |
| }) | |
| err := debug.Execute(w, services) | |
| if err != nil { | |
| fmt.Fprintln(w, "rpc: error executing template:", err.Error()) | |
| } | |
| } |