vitess.io/vitess@v0.16.2/go/vt/vtadmin/http/debug/cluster.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package debug
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"github.com/gorilla/mux"
    25  )
    26  
    27  // Cluster returns an http.HandlerFunc for the /debug/cluster/{cluster_id}
    28  // route.
    29  func Cluster(api API) http.HandlerFunc {
    30  	return func(w http.ResponseWriter, r *http.Request) {
    31  		id, ok := mux.Vars(r)["cluster_id"]
    32  		if !ok {
    33  			w.WriteHeader(http.StatusBadRequest)
    34  			w.Write([]byte("missing {cluster_id} route component"))
    35  			return
    36  		}
    37  
    38  		c, ok := api.Cluster(id)
    39  		if !ok {
    40  			w.WriteHeader(http.StatusNotFound)
    41  		}
    42  
    43  		data, err := json.Marshal(c.Debug())
    44  		if err != nil {
    45  			w.WriteHeader(http.StatusInternalServerError)
    46  			fmt.Fprintf(w, "could not marshal cluster debug map: %s\n", err)
    47  			return
    48  		}
    49  
    50  		w.Write(data)
    51  		w.Write([]byte("\n"))
    52  	}
    53  }
    54  
    55  // Clusters returns an http.HandlerFunc for the /debug/clusters route.
    56  func Clusters(api API) http.HandlerFunc {
    57  	return func(w http.ResponseWriter, r *http.Request) {
    58  		clusters := api.Clusters()
    59  		m := make(map[string]map[string]any, len(clusters))
    60  		for _, c := range clusters {
    61  			m[c.ID] = c.Debug()
    62  		}
    63  
    64  		data, err := json.Marshal(m)
    65  		if err != nil {
    66  			w.WriteHeader(http.StatusInternalServerError)
    67  			fmt.Fprintf(w, "could not marshal cluster debug map: %s\n", err)
    68  			return
    69  		}
    70  
    71  		w.Write(data)
    72  		w.Write([]byte("\n"))
    73  	}
    74  }