client/web: refactor serveGetNodeData

Remove the "JSON" ending, we no longer have a non-JSON version,
it was removed in d74c771 when we switched from the legacy web
client to React.

Also combine getNodeData into serveGetNodeData now that serveGetNodeData
is the single caller of getNodeData.

A #cleanup

Signed-off-by: Sonia Appasamy <sonia@tailscale.com>
This commit is contained in:
Sonia Appasamy 2023-09-27 13:27:59 -04:00 committed by Sonia Appasamy
parent d31460f793
commit 697f92f4a7
1 changed files with 8 additions and 15 deletions

View File

@ -143,7 +143,7 @@ func (s *Server) serveAPI(w http.ResponseWriter, r *http.Request) {
case path == "/data": case path == "/data":
switch r.Method { switch r.Method {
case httpm.GET: case httpm.GET:
s.serveGetNodeDataJSON(w, r) s.serveGetNodeData(w, r)
case httpm.POST: case httpm.POST:
s.servePostNodeUpdate(w, r) s.servePostNodeUpdate(w, r)
default: default:
@ -173,14 +173,16 @@ type nodeData struct {
IPNVersion string IPNVersion string
} }
func (s *Server) getNodeData(ctx context.Context) (*nodeData, error) { func (s *Server) serveGetNodeData(w http.ResponseWriter, r *http.Request) {
st, err := s.lc.Status(ctx) st, err := s.lc.Status(r.Context())
if err != nil { if err != nil {
return nil, err http.Error(w, err.Error(), http.StatusInternalServerError)
return
} }
prefs, err := s.lc.GetPrefs(ctx) prefs, err := s.lc.GetPrefs(r.Context())
if err != nil { if err != nil {
return nil, err http.Error(w, err.Error(), http.StatusInternalServerError)
return
} }
profile := st.User[st.Self.UserID] profile := st.User[st.Self.UserID]
deviceName := strings.Split(st.Self.DNSName, ".")[0] deviceName := strings.Split(st.Self.DNSName, ".")[0]
@ -212,15 +214,6 @@ func (s *Server) getNodeData(ctx context.Context) (*nodeData, error) {
if len(st.TailscaleIPs) != 0 { if len(st.TailscaleIPs) != 0 {
data.IP = st.TailscaleIPs[0].String() data.IP = st.TailscaleIPs[0].String()
} }
return data, nil
}
func (s *Server) serveGetNodeDataJSON(w http.ResponseWriter, r *http.Request) {
data, err := s.getNodeData(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(*data); err != nil { if err := json.NewEncoder(w).Encode(*data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return