feat: api

This commit is contained in:
Nicolas Meienberger
2025-08-10 16:51:16 +02:00
parent 2c38a551cc
commit fd91751673
8 changed files with 177 additions and 43 deletions

View File

@@ -1,46 +1,15 @@
package driver
import (
"ironmount/internal/constants"
"ironmount/internal/core"
"ironmount/internal/db"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
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
}
cfg := core.LoadConfig()
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")
c.JSON(http.StatusInternalServerError, gin.H{"Err": err.Error()})
return
}
db.CreateVolume(body.Name, volPathHost)
response := map[string]string{
"Name": body.Name,
"Mountpoint": volPathHost,
"Err": "",
}
c.JSON(http.StatusOK, response)
log.Error().Msg("Volumes can only be created through the API, not the driver interface")
c.JSON(http.StatusMethodNotAllowed, gin.H{
"Err": "Volumes can only be created through the API, not the driver interface",
})
}

View File

@@ -1,6 +1,7 @@
package driver
import (
"fmt"
"ironmount/internal/db"
"net/http"
@@ -19,6 +20,8 @@ func Get(c *gin.Context) {
vol, err := db.GetVolumeByName(body.Name)
fmt.Println("Get volume by name:", vol.Name)
if err != nil {
log.Warn().Err(err).Str("name", body.Name).Msg("Failed to get volume by name")
response := map[string]string{
@@ -34,6 +37,7 @@ func Get(c *gin.Context) {
"Name": vol.Name,
"Mountpoint": vol.Path,
"Status": map[string]string{},
// "CreatedAt": vol.CreatedAt,
},
"Err": "",
}