feat: implement http support
Signed-off-by: Kairee Wu <kaireewu@gmail.com>
This commit is contained in:
54
debug.go
Normal file
54
debug.go
Normal 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())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user