@@ -132,14 +132,12 @@ func (m *Monitor) handleNodes(w http.ResponseWriter, r *http.Request) {
132132 strings .Contains (strings .ToLower (node .NodeName ), query )
133133 })
134134 response := m .buildNodeResponse (snapshot )
135- w .Header ().Set ("Content-Type" , "application/json" )
136- json .NewEncoder (w ).Encode (response )
135+ writeJSON (w , response )
137136}
138137
139138func (m * Monitor ) handleShardTree (w http.ResponseWriter , r * http.Request ) {
140139 tree := m .GetShardTree ()
141- w .Header ().Set ("Content-Type" , "application/json" )
142- json .NewEncoder (w ).Encode (tree )
140+ writeJSON (w , tree )
143141}
144142
145143func (m * Monitor ) handleShardNodes (w http.ResponseWriter , r * http.Request ) {
@@ -148,16 +146,10 @@ func (m *Monitor) handleShardNodes(w http.ResponseWriter, r *http.Request) {
148146 return shard == shardFilter
149147 })
150148 response := m .buildNodeResponse (snapshot )
151- shardLabel := shardFilter
152- if shardLabel == "" {
153- shardLabel = "root"
154- }
155- w .Header ().Set ("Content-Type" , "application/json" )
156- json .NewEncoder (w ).Encode (map [string ]interface {}{"shard_id" : shardFilter , "shard_label" : shardLabel , "nodes" : response , "count" : len (response )})
149+ writeJSON (w , map [string ]interface {}{"shard_id" : shardFilter , "shard_label" : shardLabel (shardFilter ), "nodes" : response , "count" : len (response )})
157150}
158151
159152func (m * Monitor ) handleRootTopic (w http.ResponseWriter , r * http.Request ) {
160- w .Header ().Set ("Content-Type" , "application/json" )
161153 if r .Method == http .MethodPost {
162154 var body struct {
163155 TopicPrefix string `json:"topic_prefix,omitempty"`
@@ -183,7 +175,7 @@ func (m *Monitor) writeRootTopicResponse(w http.ResponseWriter) {
183175 prefix := m .getTopicPrefix ()
184176 topic := m .getTopicName ()
185177 rootTopic := fmt .Sprintf ("%s-%s-shard-" , prefix , topic )
186- json . NewEncoder ( w ). Encode ( map [string ]string {
178+ writeJSON ( w , map [string ]string {
187179 "root_topic" : rootTopic ,
188180 "topic_prefix" : prefix ,
189181 "topic_name" : topic ,
@@ -204,16 +196,14 @@ func (m *Monitor) handleNodeFiles(w http.ResponseWriter, r *http.Request) {
204196 entries = []CIDEntry {}
205197 }
206198 m .mu .RUnlock ()
207- w .Header ().Set ("Content-Type" , "application/json" )
208- json .NewEncoder (w ).Encode (map [string ]interface {}{"peer_id" : peerID , "cids" : entries , "count" : len (entries )})
199+ writeJSON (w , map [string ]interface {}{"peer_id" : peerID , "cids" : entries , "count" : len (entries )})
209200}
210201
211202func (m * Monitor ) handleUniqueCIDs (w http.ResponseWriter , r * http.Request ) {
212203 m .mu .RLock ()
213204 entries := m .buildCIDEntriesUnlocked (m .uniqueCIDs )
214205 m .mu .RUnlock ()
215- w .Header ().Set ("Content-Type" , "application/json" )
216- json .NewEncoder (w ).Encode (map [string ]interface {}{"cids" : entries , "count" : len (entries )})
206+ writeJSON (w , map [string ]interface {}{"cids" : entries , "count" : len (entries )})
217207}
218208
219209func (m * Monitor ) handleReplication (w http.ResponseWriter , r * http.Request ) {
@@ -223,9 +213,8 @@ func (m *Monitor) handleReplication(w http.ResponseWriter, r *http.Request) {
223213 }
224214 dist , avg , atTarget := m .getReplicationStats ()
225215 byShard := m .getReplicationByShard ()
226- w .Header ().Set ("Content-Type" , "application/json" )
227216 w .Header ().Set ("Cache-Control" , "no-store, no-cache, must-revalidate" )
228- json . NewEncoder ( w ). Encode ( map [string ]interface {}{
217+ writeJSON ( w , map [string ]interface {}{
229218 "replication_distribution" : dist ,
230219 "avg_replication_level" : avg ,
231220 "files_at_target" : atTarget ,
@@ -246,8 +235,7 @@ func (m *Monitor) handleReplicationCIDs(w http.ResponseWriter, r *http.Request)
246235 return
247236 }
248237 entries := m .getReplicationCIDsByLevel (level )
249- w .Header ().Set ("Content-Type" , "application/json" )
250- json .NewEncoder (w ).Encode (map [string ]interface {}{"level" : level , "cids" : entries , "count" : len (entries )})
238+ writeJSON (w , map [string ]interface {}{"level" : level , "cids" : entries , "count" : len (entries )})
251239}
252240
253241func (m * Monitor ) handleManifestPayload (w http.ResponseWriter , r * http.Request ) {
@@ -284,17 +272,16 @@ func (m *Monitor) handleManifestPayload(w http.ResponseWriter, r *http.Request)
284272 "size" : ro .TotalSize ,
285273 "sig" : base64 .StdEncoding .EncodeToString (ro .Signature ),
286274 }
287- w .Header ().Set ("Content-Type" , "application/json" )
288- json .NewEncoder (w ).Encode (map [string ]interface {}{"payload_cid" : ro .Payload .String (), "manifest" : manifest })
275+ writeJSON (w , map [string ]interface {}{"payload_cid" : ro .Payload .String (), "manifest" : manifest })
289276}
290277
291278func (m * Monitor ) handleIdentify (w http.ResponseWriter , r * http.Request ) {
292- peerStr := strings .TrimSpace (r .URL .Query ().Get ("peer" ))
293- if peerStr == "" {
279+ peerIDStr := strings .TrimSpace (r .URL .Query ().Get ("peer" ))
280+ if peerIDStr == "" {
294281 writeJSONError (w , "missing peer parameter" , http .StatusBadRequest )
295282 return
296283 }
297- pid , err := peer .Decode (peerStr )
284+ pid , err := peer .Decode (peerIDStr )
298285 if err != nil {
299286 writeJSONError (w , "invalid peer ID" , http .StatusBadRequest )
300287 return
@@ -335,8 +322,7 @@ func (m *Monitor) handleIdentify(w http.ResponseWriter, r *http.Request) {
335322 "connected" : connected ,
336323 "region" : region ,
337324 }
338- w .Header ().Set ("Content-Type" , "application/json" )
339- json .NewEncoder (w ).Encode (result )
325+ writeJSON (w , result )
340326}
341327
342328func (m * Monitor ) handleDashboard (w http.ResponseWriter , r * http.Request ) {
@@ -376,7 +362,7 @@ func (m *Monitor) RunStatusLogger(ctx context.Context) {
376362 sort .Strings (shardIDs )
377363 parts := make ([]string , 0 , len (shardIDs ))
378364 for _ , sid := range shardIDs {
379- parts = append (parts , fmt .Sprintf ("%s: %d" , shardLogLabel (sid ), shardCounts [sid ]))
365+ parts = append (parts , fmt .Sprintf ("%s: %d" , shardLabel (sid ), shardCounts [sid ]))
380366 }
381367 slog .Info ("status" , "nodes" , nodeCount , "shards" , len (shardCounts ), "pinned" , totalPinned , "detail" , strings .Join (parts , ", " ))
382368 }
0 commit comments