From 6726f785c10d738531ab029e5a306931a85640e4 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 23 Oct 2026 21:55:26 +0000 Subject: [PATCH] use json helper from common-lib HTTP helper --- app/app.go | 33 +++++++++++++++++---------------- app/utils.go | 14 -------------- proxmoxaas-common-lib | 2 +- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/app/app.go b/app/app.go index 6cb6b64..df69529 100644 --- a/app/app.go +++ b/app/app.go @@ -9,6 +9,8 @@ import ( "strconv" "time" + paas "proxmoxaas-common-lib" + "github.com/luthermonson/go-proxmox" ) @@ -60,10 +62,9 @@ func Run() { router.HandleFunc("GET /version", func(w http.ResponseWriter, r *http.Request) { PVEVersion, err := client.Version() if err != nil { - - WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) } else { - WriteJson(w, http.StatusOK, map[string]any{"api-version": APIVersion, "pve-version": PVEVersion}) + paas.HTTPWriteJson(w, http.StatusOK, map[string]any{"api-version": APIVersion, "pve-version": PVEVersion}) } }) @@ -71,9 +72,9 @@ func Run() { callback := Callback{} v, err := GetCluster(&callback, &cluster) if err != nil { - WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) } else { - WriteJson(w, http.StatusOK, map[string]any{"cluster": v}) + paas.HTTPWriteJson(w, http.StatusOK, map[string]any{"cluster": v}) } callback.Invoke() }) @@ -83,9 +84,9 @@ func Run() { callback := Callback{} node, err := GetNode(&callback, &cluster, nodeid) if err != nil { - WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) } else { - WriteJson(w, http.StatusOK, map[string]any{"node": node}) + paas.HTTPWriteJson(w, http.StatusOK, map[string]any{"node": node}) } callback.Invoke() }) @@ -94,15 +95,15 @@ func Run() { nodeid := r.PathValue("node") vmid, err := strconv.ParseUint(r.PathValue("vmid"), 10, 64) if err != nil { - WriteJson(w, http.StatusBadRequest, map[string]any{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", r.PathValue("instance"))}) + paas.HTTPWriteJson(w, http.StatusBadRequest, map[string]any{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", r.PathValue("instance"))}) return } callback := Callback{} instance, err := GetInstance(&callback, &cluster, nodeid, vmid) if err != nil { - WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) } else { - WriteJson(w, http.StatusOK, map[string]any{"instance": instance}) + paas.HTTPWriteJson(w, http.StatusOK, map[string]any{"instance": instance}) } callback.Invoke() }) @@ -112,7 +113,7 @@ func Run() { err := cluster.Sync() if err != nil { log.Printf("[ERR ] failed to sync cluster: %s", err) - WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) return } else { return @@ -128,7 +129,7 @@ func Run() { node, err := GetNode(&callback, &cluster, nodeName) if err != nil { log.Printf("[ERR ] failed to sync %s: %s", nodeName, err.Error()) - WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) callback.Invoke() return } @@ -137,7 +138,7 @@ func Run() { err = node.Sync() if err != nil { log.Printf("[ERR ] failed to sync %s: %s", nodeName, err.Error()) - WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) } }) @@ -146,7 +147,7 @@ func Run() { vmid, err := strconv.ParseUint(r.PathValue("vmid"), 10, 64) if err != nil { log.Printf("[ERR ] failed to sync %s.%d: %s", nodeName, vmid, err.Error()) - WriteJson(w, http.StatusInternalServerError, map[string]any{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", r.PathValue("instance"))}) + paas.HTTPWriteJson(w, http.StatusInternalServerError, map[string]any{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", r.PathValue("instance"))}) return } @@ -156,7 +157,7 @@ func Run() { instance, err := GetInstance(&callback, &cluster, nodeName, vmid) if err != nil { log.Printf("[ERR ] failed to sync %s.%d: %s", nodeName, vmid, err.Error()) - WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) callback.Invoke() return } @@ -165,7 +166,7 @@ func Run() { err = instance.Sync() if err != nil { log.Printf("[ERR ] failed to sync %s.%d: %s", nodeName, vmid, err.Error()) - WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) + paas.HTTPWriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) } }) diff --git a/app/utils.go b/app/utils.go index 21be007..591e2a6 100644 --- a/app/utils.go +++ b/app/utils.go @@ -3,7 +3,6 @@ package app import ( "encoding/json" "log" - "net/http" "os" "strings" ) @@ -64,16 +63,3 @@ func AnyPrefixes(s string, prefixes []string) string { return "" } - -func WriteJson(w http.ResponseWriter, code int, obj any) { - w.Header().Set("Content-Type", "application/json") - json, err := json.Marshal(obj) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } else { - w.WriteHeader(code) - w.Write(json) - return - } -} diff --git a/proxmoxaas-common-lib b/proxmoxaas-common-lib index 8284ffb..35ecaf8 160000 --- a/proxmoxaas-common-lib +++ b/proxmoxaas-common-lib @@ -1 +1 @@ -Subproject commit 8284ffbc43a3be694652399942615f35784df4ef +Subproject commit 35ecaf84e01d0ef6ffd5473cc8219897425b6a65