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,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)
}