mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: api
This commit is contained in:
@@ -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",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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": "",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user