Skip to content

Commit 20e6f85

Browse files
committed
refactor(agent): extract debug handler into functions
1 parent 489b0ec commit 20e6f85

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

agent/agent.go

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,52 +1660,50 @@ func (a *agent) isClosed() bool {
16601660
return a.hardCtx.Err() != nil
16611661
}
16621662

1663-
func (a *agent) HTTPDebug() http.Handler {
1664-
r := chi.NewRouter()
1665-
1666-
requireNetwork := func(w http.ResponseWriter) (*tailnet.Conn, bool) {
1667-
a.closeMutex.Lock()
1668-
network := a.network
1669-
a.closeMutex.Unlock()
1670-
1671-
if network == nil {
1672-
w.WriteHeader(http.StatusNotFound)
1673-
_, _ = w.Write([]byte("network is not ready yet"))
1674-
return nil, false
1675-
}
1663+
func (a *agent) requireNetwork() (*tailnet.Conn, bool) {
1664+
a.closeMutex.Lock()
1665+
defer a.closeMutex.Unlock()
1666+
return a.network, a.network != nil
1667+
}
16761668

1677-
return network, true
1669+
func (a *agent) HandleHTTPDebugMagicsock(w http.ResponseWriter, r *http.Request) {
1670+
network, ok := a.requireNetwork()
1671+
if !ok {
1672+
w.WriteHeader(http.StatusNotFound)
1673+
_, _ = w.Write([]byte("network is not ready yet"))
1674+
return
16781675
}
1676+
network.MagicsockServeHTTPDebug(w, r)
1677+
}
16791678

1680-
r.Get("/debug/magicsock", func(w http.ResponseWriter, r *http.Request) {
1681-
network, ok := requireNetwork(w)
1682-
if !ok {
1683-
return
1684-
}
1685-
network.MagicsockServeHTTPDebug(w, r)
1686-
})
1679+
func (a *agent) HandleHTTPMagicsockDebugLoggingState(w http.ResponseWriter, r *http.Request) {
1680+
state := chi.URLParam(r, "state")
1681+
stateBool, err := strconv.ParseBool(state)
1682+
if err != nil {
1683+
w.WriteHeader(http.StatusBadRequest)
1684+
_, _ = fmt.Fprintf(w, "invalid state %q, must be a boolean", state)
1685+
return
1686+
}
16871687

1688-
r.Get("/debug/magicsock/debug-logging/{state}", func(w http.ResponseWriter, r *http.Request) {
1689-
state := chi.URLParam(r, "state")
1690-
stateBool, err := strconv.ParseBool(state)
1691-
if err != nil {
1692-
w.WriteHeader(http.StatusBadRequest)
1693-
_, _ = fmt.Fprintf(w, "invalid state %q, must be a boolean", state)
1694-
return
1695-
}
1688+
network, ok := a.requireNetwork()
1689+
if !ok {
1690+
w.WriteHeader(http.StatusNotFound)
1691+
_, _ = w.Write([]byte("network is not ready yet"))
1692+
return
1693+
}
16961694

1697-
network, ok := requireNetwork(w)
1698-
if !ok {
1699-
return
1700-
}
1695+
network.MagicsockSetDebugLoggingEnabled(stateBool)
1696+
a.logger.Info(r.Context(), "updated magicsock debug logging due to debug request", slog.F("new_state", stateBool))
17011697

1702-
network.MagicsockSetDebugLoggingEnabled(stateBool)
1703-
a.logger.Info(r.Context(), "updated magicsock debug logging due to debug request", slog.F("new_state", stateBool))
1698+
w.WriteHeader(http.StatusOK)
1699+
_, _ = fmt.Fprintf(w, "updated magicsock debug logging to %v", stateBool)
1700+
}
17041701

1705-
w.WriteHeader(http.StatusOK)
1706-
_, _ = fmt.Fprintf(w, "updated magicsock debug logging to %v", stateBool)
1707-
})
1702+
func (a *agent) HTTPDebug() http.Handler {
1703+
r := chi.NewRouter()
17081704

1705+
r.Get("/debug/magicsock", a.HandleHTTPDebugMagicsock)
1706+
r.Get("/debug/magicsock/debug-logging/{state}", a.HandleHTTPMagicsockDebugLoggingState)
17091707
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
17101708
w.WriteHeader(http.StatusNotFound)
17111709
_, _ = w.Write([]byte("404 not found"))

0 commit comments

Comments
 (0)