Fix(restapi): debug missing mountpoints

This commit is contained in:
xjasonlyu
2022-04-04 22:05:47 +08:00
parent fd8223e4d0
commit e4801c3989
3 changed files with 24 additions and 6 deletions

View File

@@ -16,6 +16,10 @@ import (
const defaultInterval = 1000 const defaultInterval = 1000
func init() {
registerMountPoint("/connections", connectionRouter())
}
func connectionRouter() http.Handler { func connectionRouter() http.Handler {
r := chi.NewRouter() r := chi.NewRouter()
r.Get("/", getConnections) r.Get("/", getConnections)

View File

@@ -18,6 +18,10 @@ func SetStatsFunc(s func() tcpip.Stats) {
_statsFunc = s _statsFunc = s
} }
func init() {
registerMountPoint("/netstats", http.HandlerFunc(getNetStats))
}
func getNetStats(w http.ResponseWriter, r *http.Request) { func getNetStats(w http.ResponseWriter, r *http.Request) {
if _statsFunc == nil { if _statsFunc == nil {
render.Status(r, http.StatusInternalServerError) render.Status(r, http.StatusInternalServerError)

View File

@@ -20,12 +20,20 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var _upgrader = websocket.Upgrader{ var (
_upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { CheckOrigin: func(r *http.Request) bool {
return true return true
}, },
} }
_mountPoints = make(map[string]http.Handler)
)
func registerMountPoint(pattern string, handler http.Handler) {
_mountPoints[pattern] = handler
}
func Start(addr, token string) error { func Start(addr, token string) error {
r := chi.NewRouter() r := chi.NewRouter()
@@ -43,8 +51,10 @@ func Start(addr, token string) error {
r.Get("/logs", getLogs) r.Get("/logs", getLogs)
r.Get("/traffic", traffic) r.Get("/traffic", traffic)
r.Get("/version", version) r.Get("/version", version)
r.Get("/netstats", getNetStats) // attach HTTP handlers
r.Mount("/connections", connectionRouter()) for pattern, handler := range _mountPoints {
r.Mount(pattern, handler)
}
}) })
listener, err := net.Listen("tcp", addr) listener, err := net.Listen("tcp", addr)