refactor: use different path inside container

This commit is contained in:
Nicolas Meienberger
2025-08-10 10:39:58 +02:00
parent 18654426fa
commit 0f9ace4dfa
9 changed files with 129 additions and 13 deletions

View File

@@ -1,3 +1,3 @@
package constants
var VolumeRoot = "/tmp/ironmount"
const VolumeRootLocal = "/mounts"

33
internal/core/config.go Normal file
View File

@@ -0,0 +1,33 @@
package core
import (
"github.com/go-playground/validator/v10"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
type Config struct {
VolumeRootHost string `mapstructure:"volume_root" validate:"required"`
}
func LoadConfig() Config {
var config Config
viper.AutomaticEnv()
viper.BindEnv("volume_root", "VOLUME_ROOT")
if err := viper.Unmarshal(&config); err != nil {
log.Error().Err(err).Msg("Failed to load configuration")
panic("Failed to load configuration: " + err.Error())
}
validator := validator.New()
if err := validator.Struct(config); err != nil {
log.Error().Err(err).Msg("Configuration validation failed")
panic("Configuration validation failed: " + err.Error())
}
log.Info().Msgf("Loaded configuration: %+v", config)
return config
}

View File

@@ -2,6 +2,7 @@ package driver
import (
"encoding/json"
"ironmount/internal/constants"
"ironmount/internal/core"
"ironmount/internal/db"
"net/http"
@@ -19,20 +20,21 @@ func Create(w http.ResponseWriter, r *http.Request) {
_ = json.NewDecoder(r.Body).Decode(&req)
cfg := core.LoadConfig()
volPath := filepath.Join(cfg.VolumeRoot, req.Name)
volPathHost := filepath.Join(cfg.VolumeRootHost, req.Name)
volPathLocal := filepath.Join(constants.VolumeRootLocal, req.Name)
log.Info().Str("path", volPath).Msg("Creating volume directory")
if err := os.MkdirAll(volPath, 0755); err != nil {
log.Error().Err(err).Str("path", volPath).Msg("Failed to create volume directory")
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()})
return
}
db.CreateVolume(req.Name, volPath)
db.CreateVolume(req.Name, volPathHost)
response := map[string]string{
"Name": req.Name,
"Mountpoint": volPath,
"Mountpoint": volPathHost,
"Err": "",
}

View File

@@ -2,9 +2,11 @@ package driver
import (
"encoding/json"
"ironmount/internal/constants"
"ironmount/internal/db"
"net/http"
"os"
"path/filepath"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
@@ -25,8 +27,9 @@ func Remove(w http.ResponseWriter, r *http.Request) {
db.RemoveVolume(vol.Name)
log.Info().Str("path", vol.Path).Msg("Removing volume directory")
os.RemoveAll(vol.Path)
volPathLocal := filepath.Join(constants.VolumeRootLocal, req.Name)
log.Info().Str("path", volPathLocal).Msg("Removing volume directory")
os.RemoveAll(volPathLocal)
_ = json.NewEncoder(w).Encode(map[string]string{"Err": ""})
}