feat(frontend): create volume

This commit is contained in:
Nicolas Meienberger
2025-08-16 14:22:24 +02:00
parent 03a1203f7e
commit d13763995e
7 changed files with 254 additions and 33 deletions

View File

@@ -0,0 +1,18 @@
package core
import (
"regexp"
"strings"
)
var nonAlnum = regexp.MustCompile(`[^a-z0-9_-]+`)
var hyphenRuns = regexp.MustCompile(`[-_]{2,}`)
func Slugify(input string) string {
s := strings.ToLower(strings.TrimSpace(input))
s = nonAlnum.ReplaceAllString(s, "-")
s = hyphenRuns.ReplaceAllString(s, "-")
s = strings.Trim(s, "-")
return s
}

View File

@@ -2,56 +2,57 @@
package volumes
import (
"ironmount/internal/core"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
// SetupHandlers sets up the API routes for the application.
func SetupHandlers(router *gin.Engine) {
volumeService := VolumeService{}
router.GET("/api/volumes", func(c *gin.Context) {
volumes := volumeService.ListVolumes()
log.Debug().Msgf("Listing volumes: %v", volumes)
c.JSON(200, gin.H{
"volumes": volumes,
})
c.JSON(200, gin.H{"volumes": volumes})
})
router.POST("/api/volumes", func(c *gin.Context) {
var req struct {
Name string `json:"name" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": "Invalid request body"})
return
}
volume, status, err := volumeService.CreateVolume(req.Name)
clean := core.Slugify(req.Name)
if clean == "" || clean != req.Name {
c.JSON(400, gin.H{"error": "invalid volume name"})
return
}
volume, status, err := volumeService.CreateVolume(clean)
if err != nil {
c.JSON(status, gin.H{"error": err.Error()})
return
}
c.JSON(status, volume)
})
router.GET("/api/volumes/:name", func(c *gin.Context) {
volume, err := volumeService.GetVolume(c.Param("name"))
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
if volume == nil {
c.JSON(404, gin.H{"error": "Volume not found"})
return
}
c.JSON(200, gin.H{
"name": volume.Name,
"mountpoint": volume.Path,
@@ -62,12 +63,10 @@ func SetupHandlers(router *gin.Engine) {
router.DELETE("/api/volumes/:name", func(c *gin.Context) {
status, err := volumeService.DeleteVolume(c.Param("name"))
if err != nil {
c.JSON(status, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "Volume deleted successfully"})
})
}