...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/webserver/source.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) handleSources(w http.ResponseWriter, r *http.Request) {
     9  	switch r.Method {
    10  	case http.MethodGet:
    11  		// return monitored databases
    12  		dbs, err := Server.GetSources()
    13  		if err != nil {
    14  			http.Error(w, err.Error(), http.StatusInternalServerError)
    15  			return
    16  		}
    17  		_, _ = w.Write([]byte(dbs))
    18  
    19  	case http.MethodPost:
    20  		// add new monitored database
    21  		p, err := io.ReadAll(r.Body)
    22  		if err != nil {
    23  			http.Error(w, err.Error(), http.StatusBadRequest)
    24  			return
    25  		}
    26  		if err := Server.UpdateSource(p); err != nil {
    27  			http.Error(w, err.Error(), http.StatusBadRequest)
    28  		}
    29  
    30  	case http.MethodDelete:
    31  		// delete monitored database
    32  		if err := Server.DeleteSource(r.URL.Query().Get("name")); err != nil {
    33  			http.Error(w, err.Error(), http.StatusBadRequest)
    34  		}
    35  
    36  	case http.MethodOptions:
    37  		w.Header().Set("Allow", "GET, POST, DELETE, OPTIONS")
    38  		w.WriteHeader(http.StatusNoContent)
    39  
    40  	default:
    41  		w.Header().Set("Allow", "GET, POST, DELETE, OPTIONS")
    42  		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
    43  	}
    44  }
    45