// 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 (
	
	
	
	
	
)

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

func ( serviceArray) () int           { return len() }
func ( serviceArray) (,  int) bool { return [].Name < [].Name }
func ( serviceArray) (,  int)      { [], [] = [], [] }

func ( methodArray) () int           { return len() }
func ( methodArray) (,  int) bool { return [].Name < [].Name }
func ( methodArray) (,  int)      { [], [] = [], [] }

type debugHTTP struct {
	*Server
}

// Runs at /debug/rpc
func ( debugHTTP) ( http.ResponseWriter,  *http.Request) {
	// Build a sorted version of the data.
	var  serviceArray
	.serviceMap.Range(func(,  any) bool {
		 := .(*service)
		 := debugService{, .(string), make([]debugMethod, 0, len(.method))}
		for ,  := range .method {
			.Method = append(.Method, debugMethod{, })
		}
		slices.SortFunc(.Method, func(,  debugMethod) int {
			return strings.Compare(.Name, .Name)
		})
		 = append(, )
		return true
	})
	slices.SortFunc(, func(,  debugService) int {
		return strings.Compare(.Name, .Name)
	})
	 := debug.Execute(, )
	if  != nil {
		fmt.Fprintln(, "rpc: error executing template:", .Error())
	}
}