migrate to net/http, update go mod
tronnet/workflows: Static Analysis -- Golang / static-go (push) Successful in 31s
tronnet/workflows: Static Analysis -- Golang / static-go (push) Successful in 31s
This commit is contained in:
1 parent
b9538a6337
commit
590486f153
4 files changed
+68
-69
No files matched your search
+41
-35
@@ -9,7 +9,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/luthermonson/go-proxmox"
|
"github.com/luthermonson/go-proxmox"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,7 +18,6 @@ var client ProxmoxClient
|
|||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
gob.Register(proxmox.Client{})
|
gob.Register(proxmox.Client{})
|
||||||
gin.SetMode(gin.ReleaseMode)
|
|
||||||
|
|
||||||
configPath := flag.String("config", "config.json", "path to config.json file")
|
configPath := flag.String("config", "config.json", "path to config.json file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -30,7 +28,7 @@ func Run() {
|
|||||||
token := fmt.Sprintf(`%s@%s!%s`, config.PVE.Token.User, config.PVE.Token.Realm, config.PVE.Token.ID)
|
token := fmt.Sprintf(`%s@%s!%s`, config.PVE.Token.User, config.PVE.Token.Realm, config.PVE.Token.ID)
|
||||||
client = NewClient(config.PVE.URL, token, config.PVE.Token.Secret)
|
client = NewClient(config.PVE.URL, token, config.PVE.Token.Secret)
|
||||||
|
|
||||||
router := gin.Default()
|
router := http.NewServeMux()
|
||||||
|
|
||||||
cluster := Cluster{}
|
cluster := Cluster{}
|
||||||
cluster.Init(client)
|
cluster.Init(client)
|
||||||
@@ -59,69 +57,70 @@ func Run() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
router.GET("/version", func(c *gin.Context) {
|
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 {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
||||||
|
WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, gin.H{"api-version": APIVersion, "pve-version": PVEVersion})
|
WriteJson(w, http.StatusOK, map[string]any{"api-version": APIVersion, "pve-version": PVEVersion})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.GET("/", func(c *gin.Context) {
|
router.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||||
callback := Callback{}
|
callback := Callback{}
|
||||||
v, err := GetCluster(&callback, &cluster)
|
v, err := GetCluster(&callback, &cluster)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, gin.H{"cluster": v})
|
WriteJson(w, http.StatusOK, map[string]any{"cluster": v})
|
||||||
}
|
}
|
||||||
callback.Invoke()
|
callback.Invoke()
|
||||||
})
|
})
|
||||||
|
|
||||||
router.GET("/nodes/:node", func(c *gin.Context) {
|
router.HandleFunc("GET /nodes/{node}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
nodeid := c.Param("node")
|
nodeid := r.PathValue("node")
|
||||||
callback := Callback{}
|
callback := Callback{}
|
||||||
node, err := GetNode(&callback, &cluster, nodeid)
|
node, err := GetNode(&callback, &cluster, nodeid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, gin.H{"node": node})
|
WriteJson(w, http.StatusOK, map[string]any{"node": node})
|
||||||
}
|
}
|
||||||
callback.Invoke()
|
callback.Invoke()
|
||||||
})
|
})
|
||||||
|
|
||||||
router.GET("/nodes/:node/instances/:vmid", func(c *gin.Context) {
|
router.HandleFunc("GET /nodes/{node}/instances/{vmid}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
nodeid := c.Param("node")
|
nodeid := r.PathValue("node")
|
||||||
vmid, err := strconv.ParseUint(c.Param("vmid"), 10, 64)
|
vmid, err := strconv.ParseUint(r.PathValue("vmid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", c.Param("instance"))})
|
WriteJson(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 {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, gin.H{"instance": instance})
|
WriteJson(w, http.StatusOK, map[string]any{"instance": instance})
|
||||||
}
|
}
|
||||||
callback.Invoke()
|
callback.Invoke()
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/sync", func(c *gin.Context) {
|
router.HandleFunc("POST /sync", func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("[INFO] starting cluster sync\n")
|
log.Printf("[INFO] starting cluster sync\n")
|
||||||
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)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/nodes/:node/sync", func(c *gin.Context) {
|
router.HandleFunc("POST /nodes/{node}/sync", func(w http.ResponseWriter, r *http.Request) {
|
||||||
nodeName := c.Param("node")
|
nodeName := r.PathValue("node")
|
||||||
|
|
||||||
log.Printf("[INFO] starting %s sync\n", nodeName)
|
log.Printf("[INFO] starting %s sync\n", nodeName)
|
||||||
|
|
||||||
@@ -129,7 +128,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())
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()})
|
||||||
callback.Invoke()
|
callback.Invoke()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -138,16 +137,16 @@ 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())
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/nodes/:node/instances/:vmid/sync", func(c *gin.Context) {
|
router.HandleFunc("POST /nodes/{node}/instances/{vmid}/sync", func(w http.ResponseWriter, r *http.Request) {
|
||||||
nodeName := c.Param("node")
|
nodeName := r.PathValue("node")
|
||||||
vmid, err := strconv.ParseUint(c.Param("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())
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", c.Param("instance"))})
|
WriteJson(w, http.StatusInternalServerError, map[string]any{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", r.PathValue("instance"))})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +156,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())
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()})
|
||||||
callback.Invoke()
|
callback.Invoke()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -166,13 +165,20 @@ 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())
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
server := LoggingMiddleware(router)
|
||||||
|
|
||||||
log.Printf("[INFO] starting API listening on 0.0.0.0:%d", config.ListenPort)
|
log.Printf("[INFO] starting API listening on 0.0.0.0:%d", config.ListenPort)
|
||||||
err = router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort))
|
log.Fatal("[ERR ] starting gin router: ", http.ListenAndServe("0.0.0.0:"+strconv.Itoa(config.ListenPort), server))
|
||||||
if err != nil {
|
}
|
||||||
log.Fatalf("[Err] Error starting router: %s", err.Error())
|
|
||||||
}
|
func LoggingMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,6 @@ func GetCluster(callback *Callback, cluster *Cluster) (*Cluster, error) {
|
|||||||
|
|
||||||
for _, node := range cluster.Nodes {
|
for _, node := range cluster.Nodes {
|
||||||
node.lock.RLock()
|
node.lock.RLock()
|
||||||
defer node.lock.RUnlock()
|
|
||||||
callback.Add(node.lock.RUnlock)
|
callback.Add(node.lock.RUnlock)
|
||||||
for _, instance := range node.Instances {
|
for _, instance := range node.Instances {
|
||||||
instance.lock.RLock()
|
instance.lock.RLock()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -63,3 +64,16 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,50 +1,30 @@
|
|||||||
module proxmoxaas-fabric
|
module proxmoxaas-fabric
|
||||||
|
|
||||||
go 1.27.0
|
go 1.27.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.12.0
|
github.com/luthermonson/go-proxmox v0.8.2
|
||||||
github.com/luthermonson/go-proxmox v0.8.1
|
golang.org/x/sync v0.24.0
|
||||||
golang.org/x/sync v0.22.0
|
|
||||||
proxmoxaas-common-lib v0.0.0
|
proxmoxaas-common-lib v0.0.0
|
||||||
)
|
)
|
||||||
|
|
||||||
replace proxmoxaas-common-lib => ./proxmoxaas-common-lib
|
replace proxmoxaas-common-lib => ./proxmoxaas-common-lib
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/anchore/go-lzo v0.1.0 // indirect
|
||||||
github.com/buger/goterm v1.0.4 // indirect
|
github.com/buger/goterm v1.0.4 // indirect
|
||||||
github.com/bytedance/gopkg v0.1.4 // indirect
|
|
||||||
github.com/bytedance/sonic v1.15.2 // indirect
|
|
||||||
github.com/bytedance/sonic/loader v0.5.2 // indirect
|
|
||||||
github.com/cloudwego/base64x v0.1.7 // indirect
|
|
||||||
github.com/diskfs/go-diskfs v1.9.4 // indirect
|
github.com/diskfs/go-diskfs v1.9.4 // indirect
|
||||||
github.com/djherbis/times v1.6.0 // indirect
|
github.com/djherbis/times v1.6.0 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.15 // indirect
|
github.com/elliotwutingfeng/asciiset v0.0.0-20260129054604-cfde2086bc57 // indirect
|
||||||
github.com/gin-contrib/sse v1.1.1 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
|
||||||
github.com/go-playground/validator/v10 v10.30.3 // indirect
|
|
||||||
github.com/goccy/go-json v0.10.6 // indirect
|
|
||||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
|
||||||
github.com/gorilla/websocket v1.5.3 // indirect
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
github.com/jinzhu/copier v0.4.0 // indirect
|
github.com/jinzhu/copier v0.4.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/klauspost/compress v1.18.5 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
|
||||||
github.com/leodido/go-urn v1.5.0 // indirect
|
|
||||||
github.com/magefile/mage v1.17.2 // indirect
|
github.com/magefile/mage v1.17.2 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
github.com/pierrec/lz4/v4 v4.1.26 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/pkg/xattr v0.4.12 // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/sirupsen/logrus v1.9.4 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.4.3 // indirect
|
github.com/stretchr/testify v1.12.1 // indirect
|
||||||
github.com/quic-go/qpack v0.6.0 // indirect
|
github.com/ulikunitz/xz v0.5.15 // indirect
|
||||||
github.com/quic-go/quic-go v0.61.0 // indirect
|
golang.org/x/sys v0.49.0 // indirect
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
|
||||||
github.com/ugorji/go/codec v1.3.2 // indirect
|
|
||||||
go.mongodb.org/mongo-driver/v2 v2.8.1 // indirect
|
|
||||||
golang.org/x/arch v0.30.0 // indirect
|
|
||||||
golang.org/x/crypto v0.55.0 // indirect
|
|
||||||
golang.org/x/net v0.58.0 // indirect
|
|
||||||
golang.org/x/sys v0.47.0 // indirect
|
|
||||||
golang.org/x/text v0.41.0 // indirect
|
|
||||||
google.golang.org/protobuf v1.36.12 // indirect
|
|
||||||
)
|
)
|
||||||
Reference in new issue
Block a user