use json helper from common-lib HTTP helper
tronnet/workflows: Static Analysis -- Golang / static-go (push) Successful in 11s

This commit is contained in:
alu committed 2026-10-23 21:55:40 +00:00
1 parent 590486f153
commit 6726f785c1
3 files changed
+18 -31

No files matched your search

+17 -16
View File
@@ -9,6 +9,8 @@ import (
"strconv" "strconv"
"time" "time"
paas "proxmoxaas-common-lib"
"github.com/luthermonson/go-proxmox" "github.com/luthermonson/go-proxmox"
) )
@@ -60,10 +62,9 @@ func Run() {
router.HandleFunc("GET /version", func(w http.ResponseWriter, r *http.Request) { router.HandleFunc("GET /version", func(w http.ResponseWriter, r *http.Request) {
PVEVersion, err := client.Version() PVEVersion, err := client.Version()
if err != nil { if err != nil {
paas.HTTPWriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
} else { } 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{} callback := Callback{}
v, err := GetCluster(&callback, &cluster) v, err := GetCluster(&callback, &cluster)
if err != nil { 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 { } else {
WriteJson(w, http.StatusOK, map[string]any{"cluster": v}) paas.HTTPWriteJson(w, http.StatusOK, map[string]any{"cluster": v})
} }
callback.Invoke() callback.Invoke()
}) })
@@ -83,9 +84,9 @@ func Run() {
callback := Callback{} callback := Callback{}
node, err := GetNode(&callback, &cluster, nodeid) node, err := GetNode(&callback, &cluster, nodeid)
if err != nil { 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 { } else {
WriteJson(w, http.StatusOK, map[string]any{"node": node}) paas.HTTPWriteJson(w, http.StatusOK, map[string]any{"node": node})
} }
callback.Invoke() callback.Invoke()
}) })
@@ -94,15 +95,15 @@ func Run() {
nodeid := r.PathValue("node") nodeid := r.PathValue("node")
vmid, err := strconv.ParseUint(r.PathValue("vmid"), 10, 64) vmid, err := strconv.ParseUint(r.PathValue("vmid"), 10, 64)
if err != nil { 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 return
} }
callback := Callback{} callback := Callback{}
instance, err := GetInstance(&callback, &cluster, nodeid, vmid) instance, err := GetInstance(&callback, &cluster, nodeid, vmid)
if err != nil { 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 { } else {
WriteJson(w, http.StatusOK, map[string]any{"instance": instance}) paas.HTTPWriteJson(w, http.StatusOK, map[string]any{"instance": instance})
} }
callback.Invoke() callback.Invoke()
}) })
@@ -112,7 +113,7 @@ func Run() {
err := cluster.Sync() err := cluster.Sync()
if err != nil { if err != nil {
log.Printf("[ERR ] failed to sync cluster: %s", err) 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 return
} else { } else {
return return
@@ -128,7 +129,7 @@ func Run() {
node, err := GetNode(&callback, &cluster, nodeName) node, err := GetNode(&callback, &cluster, nodeName)
if err != nil { if err != nil {
log.Printf("[ERR ] failed to sync %s: %s", nodeName, err.Error()) 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() callback.Invoke()
return return
} }
@@ -137,7 +138,7 @@ func Run() {
err = node.Sync() err = node.Sync()
if err != nil { if err != nil {
log.Printf("[ERR ] failed to sync %s: %s", nodeName, err.Error()) 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) vmid, err := strconv.ParseUint(r.PathValue("vmid"), 10, 64)
if err != nil { if err != nil {
log.Printf("[ERR ] failed to sync %s.%d: %s", nodeName, vmid, err.Error()) 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 return
} }
@@ -156,7 +157,7 @@ func Run() {
instance, err := GetInstance(&callback, &cluster, nodeName, vmid) instance, err := GetInstance(&callback, &cluster, nodeName, vmid)
if err != nil { if err != nil {
log.Printf("[ERR ] failed to sync %s.%d: %s", nodeName, vmid, err.Error()) 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() callback.Invoke()
return return
} }
@@ -165,7 +166,7 @@ func Run() {
err = instance.Sync() err = instance.Sync()
if err != nil { if err != nil {
log.Printf("[ERR ] failed to sync %s.%d: %s", nodeName, vmid, err.Error()) 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()})
} }
}) })
-14
View File
@@ -3,7 +3,6 @@ package app
import ( import (
"encoding/json" "encoding/json"
"log" "log"
"net/http"
"os" "os"
"strings" "strings"
) )
@@ -64,16 +63,3 @@ func AnyPrefixes(s string, prefixes []string) string {
return "" 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
}
}