feat: implement http support

Signed-off-by: Kairee Wu <kaireewu@gmail.com>
This commit is contained in:
2022-04-23 17:34:50 +08:00
parent 74ef998a9b
commit 4477a63c49
6 changed files with 150 additions and 4 deletions

54
debug.go Normal file
View File

@ -0,0 +1,54 @@
package krpc
import (
"fmt"
"net/http"
"text/template"
)
const debugText = `<html>
<head><title>kRPC Services</title></head>
<body>
{{range .}}
<hr>
Service {{.Name}}
<hr>
<table>
<tr>
<th>Method</th><th>Calls</th>
</tr>
<tr>
{{range $name, $mtype := .Method}}
<tr>
<td>{{$name}}({{$mtype.ArgType}}, {{$mtype.ReplyType}}) error</td>
<td>{{$mtype.NumCalls}}
</tr>
{{end}}
</tr>
</table>
{{end}}
</body>`
var debug = template.Must(template.New("RPC debug").Parse(debugText))
type debugHTTP struct {
*Server
}
type debugService struct {
Name string
Method map[string]*methodType
}
func (s debugHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var services []debugService
s.serviceMap.Range(func(namei, svci interface{}) bool {
svc := svci.(*service)
services = append(services, debugService{Name: namei.(string), Method: svc.method,})
return true
})
err := debug.Execute(w, services)
if err != nil {
_, _ = fmt.Fprintln(w, "rpc: error executing template:", err.Error())
}
}