...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/webserver/metric.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/webserver

     1  package webserver
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  )
     7  
     8  func (Server *WebUIServer) handleMetrics(w http.ResponseWriter, r *http.Request) {
     9  	var (
    10  		err    error
    11  		params []byte
    12  		res    string
    13  	)
    14  
    15  	defer func() {
    16  		if err != nil {
    17  			http.Error(w, err.Error(), http.StatusInternalServerError)
    18  		}
    19  	}()
    20  
    21  	switch r.Method {
    22  	case http.MethodGet:
    23  		// return stored metrics
    24  		if res, err = Server.GetMetrics(); err != nil {
    25  			return
    26  		}
    27  		_, err = w.Write([]byte(res))
    28  
    29  	case http.MethodPost:
    30  		// add new stored metric
    31  		if params, err = io.ReadAll(r.Body); err != nil {
    32  			return
    33  		}
    34  		err = Server.UpdateMetric(r.URL.Query().Get("name"), params)
    35  
    36  	case http.MethodDelete:
    37  		// delete stored metric
    38  		err = Server.DeleteMetric(r.URL.Query().Get("name"))
    39  
    40  	case http.MethodOptions:
    41  		w.Header().Set("Allow", "GET, POST, DELETE, OPTIONS")
    42  		w.WriteHeader(http.StatusNoContent)
    43  
    44  	default:
    45  		w.Header().Set("Allow", "GET, POST, DELETE, OPTIONS")
    46  		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
    47  	}
    48  }
    49