diff --git a/app/app.go b/app/app.go index 40886a6..6cb6b64 100644 --- a/app/app.go +++ b/app/app.go @@ -9,7 +9,6 @@ import ( "strconv" "time" - "github.com/gin-gonic/gin" "github.com/luthermonson/go-proxmox" ) @@ -19,7 +18,6 @@ var client ProxmoxClient func Run() { gob.Register(proxmox.Client{}) - gin.SetMode(gin.ReleaseMode) configPath := flag.String("config", "config.json", "path to config.json file") 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) client = NewClient(config.PVE.URL, token, config.PVE.Token.Secret) - router := gin.Default() + router := http.NewServeMux() cluster := Cluster{} 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() if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + + WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) } 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{} v, err := GetCluster(&callback, &cluster) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + WriteJson(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) } else { - c.JSON(http.StatusOK, gin.H{"cluster": v}) + WriteJson(w, http.StatusOK, map[string]any{"cluster": v}) } callback.Invoke() }) - router.GET("/nodes/:node", func(c *gin.Context) { - nodeid := c.Param("node") + router.HandleFunc("GET /nodes/{node}", func(w http.ResponseWriter, r *http.Request) { + nodeid := r.PathValue("node") callback := Callback{} node, err := GetNode(&callback, &cluster, nodeid) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) + WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) } else { - c.JSON(http.StatusOK, gin.H{"node": node}) + WriteJson(w, http.StatusOK, map[string]any{"node": node}) } callback.Invoke() }) - router.GET("/nodes/:node/instances/:vmid", func(c *gin.Context) { - nodeid := c.Param("node") - vmid, err := strconv.ParseUint(c.Param("vmid"), 10, 64) + router.HandleFunc("GET /nodes/{node}/instances/{vmid}", func(w http.ResponseWriter, r *http.Request) { + nodeid := r.PathValue("node") + vmid, err := strconv.ParseUint(r.PathValue("vmid"), 10, 64) 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 } callback := Callback{} instance, err := GetInstance(&callback, &cluster, nodeid, vmid) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) + WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) } else { - c.JSON(http.StatusOK, gin.H{"instance": instance}) + WriteJson(w, http.StatusOK, map[string]any{"instance": instance}) } 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") err := cluster.Sync() if err != nil { 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 } else { return } }) - router.POST("/nodes/:node/sync", func(c *gin.Context) { - nodeName := c.Param("node") + router.HandleFunc("POST /nodes/{node}/sync", func(w http.ResponseWriter, r *http.Request) { + nodeName := r.PathValue("node") log.Printf("[INFO] starting %s sync\n", nodeName) @@ -129,7 +128,7 @@ func Run() { node, err := GetNode(&callback, &cluster, nodeName) if err != nil { 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() return } @@ -138,16 +137,16 @@ func Run() { err = node.Sync() if err != nil { 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) { - nodeName := c.Param("node") - vmid, err := strconv.ParseUint(c.Param("vmid"), 10, 64) + router.HandleFunc("POST /nodes/{node}/instances/{vmid}/sync", func(w http.ResponseWriter, r *http.Request) { + nodeName := r.PathValue("node") + 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()) - 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 } @@ -157,7 +156,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()) - c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) + WriteJson(w, http.StatusNotFound, map[string]any{"error": err.Error()}) callback.Invoke() return } @@ -166,13 +165,20 @@ func Run() { err = instance.Sync() if err != nil { 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) - err = router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort)) - if err != nil { - log.Fatalf("[Err] Error starting router: %s", err.Error()) - } + log.Fatal("[ERR ] starting gin router: ", http.ListenAndServe("0.0.0.0:"+strconv.Itoa(config.ListenPort), server)) +} + +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)) + }) } diff --git a/app/model.go b/app/model.go index c73c3b2..cc760d5 100644 --- a/app/model.go +++ b/app/model.go @@ -27,7 +27,6 @@ func GetCluster(callback *Callback, cluster *Cluster) (*Cluster, error) { for _, node := range cluster.Nodes { node.lock.RLock() - defer node.lock.RUnlock() callback.Add(node.lock.RUnlock) for _, instance := range node.Instances { instance.lock.RLock() diff --git a/app/utils.go b/app/utils.go index 591e2a6..21be007 100644 --- a/app/utils.go +++ b/app/utils.go @@ -3,6 +3,7 @@ package app import ( "encoding/json" "log" + "net/http" "os" "strings" ) @@ -63,3 +64,16 @@ 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/go.mod b/go.mod index 2cb363e..6521939 100644 --- a/go.mod +++ b/go.mod @@ -1,50 +1,30 @@ module proxmoxaas-fabric -go 1.27.0 +go 1.27.3 require ( - github.com/gin-gonic/gin v1.12.0 - github.com/luthermonson/go-proxmox v0.8.1 - golang.org/x/sync v0.22.0 + github.com/luthermonson/go-proxmox v0.8.2 + golang.org/x/sync v0.24.0 proxmoxaas-common-lib v0.0.0 ) replace proxmoxaas-common-lib => ./proxmoxaas-common-lib require ( + github.com/anchore/go-lzo v0.1.0 // 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/djherbis/times v1.6.0 // indirect - github.com/gabriel-vasile/mimetype v1.4.15 // indirect - github.com/gin-contrib/sse v1.1.1 // 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/elliotwutingfeng/asciiset v0.0.0-20260129054604-cfde2086bc57 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/jinzhu/copier v0.4.0 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.4.0 // indirect - github.com/leodido/go-urn v1.5.0 // indirect + github.com/klauspost/compress v1.18.5 // indirect github.com/magefile/mage v1.17.2 // indirect - github.com/mattn/go-isatty v0.0.24 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.4.3 // indirect - github.com/quic-go/qpack v0.6.0 // indirect - github.com/quic-go/quic-go v0.61.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 + github.com/pierrec/lz4/v4 v4.1.26 // indirect + github.com/pkg/xattr v0.4.12 // indirect + github.com/sirupsen/logrus v1.9.4 // indirect + github.com/stretchr/testify v1.12.1 // indirect + github.com/ulikunitz/xz v0.5.15 // indirect + golang.org/x/sys v0.49.0 // indirect )