refactor: switch router to gin

This commit is contained in:
Nicolas Meienberger
2025-08-10 13:07:05 +02:00
parent e601d91955
commit 2c38a551cc
15 changed files with 277 additions and 131 deletions

View File

@@ -1,13 +1,15 @@
package driver
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
)
func Activate(w http.ResponseWriter, r *http.Request) {
resp := map[string]any{
"Implements": []string{"VolumeDriver"},
}
_ = json.NewEncoder(w).Encode(resp)
func Activate(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Implements": []string{
"VolumeDriver",
},
})
}

View File

@@ -0,0 +1,11 @@
package driver
import "github.com/gin-gonic/gin"
func Capabilities(c *gin.Context) {
c.JSON(200, gin.H{
"Capabilities": map[string]bool{
"Scope": true, // Indicates that the driver supports scope (local/global)
},
})
}

View File

@@ -1,7 +1,6 @@
package driver
import (
"encoding/json"
"ironmount/internal/constants"
"ironmount/internal/core"
"ironmount/internal/db"
@@ -9,34 +8,39 @@ import (
"os"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func Create(w http.ResponseWriter, r *http.Request) {
var req struct {
Name string
Opts map[string]string `json:"Opts,omitempty"`
func Create(c *gin.Context) {
var body CreateRequest
if err := c.BindJSON(&body); err != nil {
log.Error().Err(err).Msg("Failed to bind JSON for Create request")
c.JSON(http.StatusBadRequest, gin.H{"Err": err.Error()})
return
}
_ = json.NewDecoder(r.Body).Decode(&req)
cfg := core.LoadConfig()
volPathHost := filepath.Join(cfg.VolumeRootHost, req.Name)
volPathLocal := filepath.Join(constants.VolumeRootLocal, req.Name)
volPathHost := filepath.Join(cfg.VolumeRootHost, body.Name)
volPathLocal := filepath.Join(constants.VolumeRootLocal, body.Name)
log.Info().Str("path", volPathLocal).Msg("Creating volume directory")
if err := os.MkdirAll(volPathLocal, 0755); err != nil {
log.Error().Err(err).Str("path", volPathLocal).Msg("Failed to create volume directory")
_ = json.NewEncoder(w).Encode(map[string]string{"Err": err.Error()})
c.JSON(http.StatusInternalServerError, gin.H{"Err": err.Error()})
return
}
db.CreateVolume(req.Name, volPathHost)
db.CreateVolume(body.Name, volPathHost)
response := map[string]string{
"Name": req.Name,
"Name": body.Name,
"Mountpoint": volPathHost,
"Err": "",
}
_ = json.NewEncoder(w).Encode(response)
c.JSON(http.StatusOK, response)
}

View File

@@ -1,28 +1,30 @@
package driver
import (
"encoding/json"
"ironmount/internal/db"
"net/http"
"github.com/rs/zerolog/hlog"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func Get(w http.ResponseWriter, r *http.Request) {
var req struct {
Name string
}
_ = json.NewDecoder(r.Body).Decode(&req)
func Get(c *gin.Context) {
var body GetRequest
vol, err := db.GetVolumeByName(req.Name)
if err := c.BindJSON(&body); err != nil {
log.Error().Err(err).Msg("Failed to bind JSON for Get request")
c.JSON(http.StatusBadRequest, gin.H{"Err": err.Error()})
return
}
vol, err := db.GetVolumeByName(body.Name)
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Error retrieving volume")
log.Warn().Err(err).Str("name", body.Name).Msg("Failed to get volume by name")
response := map[string]string{
"Err": err.Error(),
}
_ = json.NewEncoder(w).Encode(response)
c.JSON(http.StatusNotFound, response)
return
}
@@ -36,5 +38,5 @@ func Get(w http.ResponseWriter, r *http.Request) {
"Err": "",
}
_ = json.NewEncoder(w).Encode(response)
c.JSON(http.StatusOK, response)
}

View File

@@ -1,17 +1,17 @@
package driver
import (
"net/http"
"github.com/gin-gonic/gin"
)
func SetupHandlers(mux *http.ServeMux) {
mux.HandleFunc("/Plugin.Activate", Activate)
mux.HandleFunc("/VolumeDriver.Create", Create)
mux.HandleFunc("/VolumeDriver.Remove", Remove)
mux.HandleFunc("/VolumeDriver.Mount", Mount)
mux.HandleFunc("/VolumeDriver.Unmount", Unmount)
mux.HandleFunc("/VolumeDriver.Path", Path)
mux.HandleFunc("/VolumeDriver.Get", Get)
mux.HandleFunc("/VolumeDriver.List", List)
func SetupHandlers(router *gin.Engine) {
router.POST("/Plugin.Activate", Activate)
router.POST("/VolumeDriver.Create", Create)
router.POST("/VolumeDriver.Remove", Remove)
router.POST("/VolumeDriver.Mount", Mount)
router.POST("/VolumeDriver.Unmount", Unmount)
router.POST("/VolumeDriver.Path", Path)
router.POST("/VolumeDriver.Get", Get)
router.POST("/VolumeDriver.List", List)
router.POST("/VolumeDriver.Capabilities", Capabilities)
}

View File

@@ -1,25 +1,27 @@
package driver
import (
"encoding/json"
"ironmount/internal/db"
"net/http"
"github.com/rs/zerolog/hlog"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func List(w http.ResponseWriter, r *http.Request) {
func List(c *gin.Context) {
volumes, err := db.ListVolumes()
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Error listing volumes")
json.NewEncoder(w).Encode(map[string]any{
log.Error().Err(err).Msg("Failed to list volumes")
c.JSON(http.StatusInternalServerError, gin.H{
"Volumes": nil,
"Err": err.Error(),
})
return
}
json.NewEncoder(w).Encode(map[string]any{
c.JSON(http.StatusOK, gin.H{
"Volumes": volumes,
"Err": "",
})

View File

@@ -1,37 +1,35 @@
package driver
import (
"encoding/json"
"ironmount/internal/db"
"net/http"
"github.com/rs/zerolog/hlog"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func Mount(w http.ResponseWriter, r *http.Request) {
func Mount(c *gin.Context) {
var req MountRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body")
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Invalid request body")
http.Error(w, "Invalid request body", http.StatusBadRequest)
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Error().Err(err).Str("volume", req.Name).Msg("Failed to get volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
c.JSON(http.StatusNotFound, gin.H{"Err": err.Error()})
return
}
log.Info().Str("volume", vol.Name).Str("path", vol.Path).Msg("Mounting volume")
_ = json.NewEncoder(w).Encode(map[string]string{
c.JSON(http.StatusOK, gin.H{
"Name": vol.Name,
"Mountpoint": vol.Path,
"Err": "",
})

View File

@@ -1,27 +1,31 @@
package driver
import (
"encoding/json"
"ironmount/internal/db"
"net/http"
"github.com/rs/zerolog/hlog"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func Path(w http.ResponseWriter, r *http.Request) {
func Path(c *gin.Context) {
var req PathRequest
_ = json.NewDecoder(r.Body).Decode(&req)
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Error retrieving volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body for Path")
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
_ = json.NewEncoder(w).Encode(map[string]string{
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Error().Err(err).Str("volume", req.Name).Msg("Failed to get volume by name")
c.JSON(http.StatusNotFound, gin.H{"Err": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"Mountpoint": vol.Path,
"Err": "",
})

View File

@@ -1,27 +1,30 @@
package driver
import (
"encoding/json"
"ironmount/internal/constants"
"ironmount/internal/db"
"net/http"
"os"
"path/filepath"
"github.com/rs/zerolog/hlog"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func Remove(w http.ResponseWriter, r *http.Request) {
func Remove(c *gin.Context) {
var req RemoveRequest
_ = json.NewDecoder(r.Body).Decode(&req)
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body for Remove")
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Error retrieving volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
log.Error().Err(err).Str("volume", req.Name).Msg("Failed to get volume by name")
c.JSON(http.StatusNotFound, gin.H{"Err": err.Error()})
return
}
@@ -31,5 +34,7 @@ func Remove(w http.ResponseWriter, r *http.Request) {
log.Info().Str("path", volPathLocal).Msg("Removing volume directory")
os.RemoveAll(volPathLocal)
_ = json.NewEncoder(w).Encode(map[string]string{"Err": ""})
c.JSON(http.StatusOK, gin.H{
"Err": "",
})
}

View File

@@ -5,6 +5,10 @@ type CreateRequest struct {
Name string
}
type GetRequest struct {
Name string
}
// RemoveRequest is the JSON request for Remove
type RemoveRequest struct {
Name string

View File

@@ -1,10 +1,13 @@
package driver
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
)
func Unmount(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]string{"Err": ""})
func Unmount(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Err": "",
})
}