admin control features and admin dashboard
This commit is contained in:
1 parent
c630f13c57
commit
37910e410b
69 files changed
+4959
-400
No files matched your search
@@ -0,0 +1,183 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// --- validators ---
|
||||
|
||||
func (s *Server) handleConfigListValidators(w http.ResponseWriter, r *http.Request) {
|
||||
validators, err := s.DB.ListValidators(r.Context())
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
out := make([]validatorDTO, len(validators))
|
||||
for i, v := range validators {
|
||||
out[i] = validatorDTO{ValidatorID: v.ValidatorID, OSPortID: v.OSPortID, State: v.State}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigCreateValidator(w http.ResponseWriter, r *http.Request) {
|
||||
var req createValidatorRequest
|
||||
if err := readJSON(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
||||
return
|
||||
}
|
||||
if req.ValidatorID == "" {
|
||||
writeError(w, http.StatusBadRequest, "validator_id must not be empty")
|
||||
return
|
||||
}
|
||||
if err := s.DB.AdminCreateValidator(r.Context(), req.ValidatorID, req.OSPortID); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, validatorDTO{ValidatorID: req.ValidatorID, OSPortID: req.OSPortID, State: "idle"})
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigUpdateValidator(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
var req updateValidatorRequest
|
||||
if err := readJSON(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
||||
return
|
||||
}
|
||||
if err := s.DB.AdminUpdateValidatorPort(r.Context(), id, req.OSPortID); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigDeleteValidator(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if err := s.DB.DeleteValidator(r.Context(), id); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
||||
}
|
||||
|
||||
// --- sites ---
|
||||
|
||||
func (s *Server) handleConfigListSites(w http.ResponseWriter, r *http.Request) {
|
||||
sites, err := s.DB.ListSites(r.Context())
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
out := make([]siteDTO, len(sites))
|
||||
for i, site := range sites {
|
||||
out[i] = siteDTO{Index: site.Index, SiteID: site.SiteID}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigPutSite(w http.ResponseWriter, r *http.Request) {
|
||||
idx, err := strconv.Atoi(r.PathValue("index"))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "index must be an integer")
|
||||
return
|
||||
}
|
||||
var req putSiteRequest
|
||||
if err := readJSON(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
||||
return
|
||||
}
|
||||
if err := s.DB.UpsertSite(r.Context(), idx, req.SiteID); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, siteDTO{Index: idx, SiteID: req.SiteID})
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigDeleteSite(w http.ResponseWriter, r *http.Request) {
|
||||
idx, err := strconv.Atoi(r.PathValue("index"))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "index must be an integer")
|
||||
return
|
||||
}
|
||||
if err := s.DB.DeleteSite(r.Context(), idx); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
||||
}
|
||||
|
||||
// --- target groups ---
|
||||
|
||||
func (s *Server) handleConfigListTargets(w http.ResponseWriter, r *http.Request) {
|
||||
groups, err := s.DB.ListTargetGroups(r.Context())
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
out := make([]targetGroupDTO, len(groups))
|
||||
for i, g := range groups {
|
||||
out[i] = targetGroupDTO{Name: g.Name, Targets: g.Targets}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigPutTargetGroup(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("group")
|
||||
var req putTargetGroupRequest
|
||||
if err := readJSON(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
||||
return
|
||||
}
|
||||
if err := s.DB.UpsertTargetGroup(r.Context(), name, req.Targets); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, targetGroupDTO{Name: name, Targets: req.Targets})
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigDeleteTargetGroup(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("group")
|
||||
if err := s.DB.DeleteTargetGroup(r.Context(), name); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
||||
}
|
||||
|
||||
// --- check types ---
|
||||
|
||||
func (s *Server) handleConfigListCheckTypes(w http.ResponseWriter, r *http.Request) {
|
||||
checkTypes, err := s.DB.ListCheckTypes(r.Context())
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
out := make([]checkTypeDTO, len(checkTypes))
|
||||
for i, ct := range checkTypes {
|
||||
out[i] = checkTypeDTO{Name: ct.Name, Enabled: ct.Enabled, Targets: ct.TargetGroups}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigPutCheckType(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("name")
|
||||
var req putCheckTypeRequest
|
||||
if err := readJSON(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
||||
return
|
||||
}
|
||||
if err := s.DB.UpsertCheckType(r.Context(), name, req.Enabled, req.Targets); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, checkTypeDTO{Name: name, Enabled: req.Enabled, Targets: req.Targets})
|
||||
}
|
||||
|
||||
func (s *Server) handleConfigDeleteCheckType(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("name")
|
||||
if err := s.DB.DeleteCheckType(r.Context(), name); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
||||
}
|
||||
Reference in new issue
Block a user