mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
refactor: use domain pattern
This commit is contained in:
177
internal/modules/driver/handlers.go
Normal file
177
internal/modules/driver/handlers.go
Normal file
@@ -0,0 +1,177 @@
|
||||
// Package driver provides the HTTP handlers for the volume driver API.
|
||||
package driver
|
||||
|
||||
import (
|
||||
"ironmount/internal/modules/volumes"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func SetupHandlers(router *gin.Engine) {
|
||||
|
||||
volumeService := volumes.VolumeService{}
|
||||
|
||||
router.POST("/VolumeDriver.Capabilities", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"Capabilities": map[string]bool{
|
||||
"Scope": true, // Indicates that the driver supports scope (local/global)
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
router.POST("/Plugin.Activate", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"Implements": []string{
|
||||
"VolumeDriver",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
router.POST("/VolumeDriver.Create", func(c *gin.Context) {
|
||||
var req CreateRequest
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.Error().Err(err).Msg("Invalid request body for Create")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
volume, status, err := volumeService.CreateVolume(req.Name)
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create volume")
|
||||
c.JSON(status, gin.H{"Err": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(status, gin.H{
|
||||
"Name": volume.Name,
|
||||
"Mountpoint": volume.Path,
|
||||
"Err": "",
|
||||
})
|
||||
})
|
||||
|
||||
router.POST("/VolumeDriver.Remove", func(c *gin.Context) {
|
||||
var req RemoveRequest
|
||||
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.Error().Err(err).Msg("Invalid request body for Remove")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
status, err := volumeService.DeleteVolume(req.Name)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(status, gin.H{"Err": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"Err": "",
|
||||
})
|
||||
})
|
||||
|
||||
router.POST("/VolumeDriver.Mount", func(c *gin.Context) {
|
||||
var req MountRequest
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.Error().Err(err).Msg("Invalid request body")
|
||||
|
||||
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
volume, err := volumeService.GetVolume(req.Name)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if volume == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Volume not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"Name": volume.Name,
|
||||
"Mountpoint": volume.Path,
|
||||
"Err": "",
|
||||
})
|
||||
})
|
||||
|
||||
// VolumeDriver.Unmount is a no-op in this implementation
|
||||
router.POST("/VolumeDriver.Unmount", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"Err": "",
|
||||
})
|
||||
})
|
||||
|
||||
// VolumeDriver.Path returns the mount point of the volume
|
||||
router.POST("/VolumeDriver.Path", func(c *gin.Context) {
|
||||
var req PathRequest
|
||||
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.Error().Err(err).Msg("Invalid request body for Path")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
vol, err := volumeService.GetVolume(req.Name)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"Err": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if vol == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"Err": "Volume not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"Mountpoint": vol.Path,
|
||||
"Err": "",
|
||||
})
|
||||
})
|
||||
|
||||
router.POST("/VolumeDriver.Get", func(c *gin.Context) {
|
||||
var req GetRequest
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.Error().Err(err).Msg("Invalid request body for Get")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
vol, err := volumeService.GetVolume(req.Name)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"Err": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if vol == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"Err": "Volume not found"})
|
||||
return
|
||||
}
|
||||
|
||||
data := map[string]any{
|
||||
"Volume": map[string]string{
|
||||
"Name": vol.Name,
|
||||
"Mountpoint": vol.Path,
|
||||
"CreatedAt": vol.CreatedAt.Format(volumes.DateFormat),
|
||||
},
|
||||
"Err": "",
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, data)
|
||||
})
|
||||
|
||||
router.POST("/VolumeDriver.List", func(c *gin.Context) {
|
||||
volumesList := volumeService.ListVolumes()
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"Volumes": volumesList,
|
||||
})
|
||||
})
|
||||
}
|
||||
26
internal/modules/driver/types.go
Normal file
26
internal/modules/driver/types.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package driver
|
||||
|
||||
// CreateRequest is the JSON request for Create
|
||||
type CreateRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type GetRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// RemoveRequest is the JSON request for Remove
|
||||
type RemoveRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// MountRequest is the JSON request for Mount
|
||||
type MountRequest struct {
|
||||
Name string
|
||||
ID string
|
||||
}
|
||||
|
||||
// PathRequest is the JSON request for Path
|
||||
type PathRequest struct {
|
||||
Name string
|
||||
}
|
||||
73
internal/modules/volumes/handlers.go
Normal file
73
internal/modules/volumes/handlers.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Package volumes provides tools for managing volumes in the application.
|
||||
package volumes
|
||||
|
||||
import (
|
||||
"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,
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
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,
|
||||
"created_at": volume.CreatedAt.String(),
|
||||
"err": "",
|
||||
})
|
||||
})
|
||||
|
||||
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"})
|
||||
})
|
||||
}
|
||||
62
internal/modules/volumes/queries.go
Normal file
62
internal/modules/volumes/queries.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package volumes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ironmount/internal/db"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type VolumeQueries struct{}
|
||||
|
||||
func (q *VolumeQueries) QueryVolumeByName(n string) (*db.Volume, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
volume, err := gorm.G[*db.Volume](db.DB).Where("name = ?", n).First(ctx)
|
||||
|
||||
if err != nil {
|
||||
if (err.Error() == "record not found") || (err == gorm.ErrRecordNotFound) {
|
||||
log.Warn().Str("name", n).Msg("Volume not found")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return volume, nil
|
||||
}
|
||||
|
||||
func (q *VolumeQueries) InsertVolume(name, path string) error {
|
||||
ctx := context.Background()
|
||||
err := gorm.G[db.Volume](db.DB).Create(ctx, &db.Volume{Name: name, Path: path})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *VolumeQueries) RemoveVolume(name string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
log.Info().Str("volume", name).Msg("Removing volume")
|
||||
_, err := gorm.G[db.Volume](db.DB).Where("name = ?", name).Delete(ctx)
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("volume", name).Msg("Error removing volume")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *VolumeQueries) QueryVolumes() ([]db.Volume, error) {
|
||||
rows, err := gorm.G[db.Volume](db.DB).Select("name", "path", "created_at").Find(context.Background())
|
||||
|
||||
if err != nil {
|
||||
return []db.Volume{}, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
85
internal/modules/volumes/service.go
Normal file
85
internal/modules/volumes/service.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package volumes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"ironmount/internal/constants"
|
||||
"ironmount/internal/core"
|
||||
"ironmount/internal/db"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type VolumeService struct{}
|
||||
|
||||
var volumeQueries = VolumeQueries{}
|
||||
|
||||
// CreateVolume handles the creation of a new volume.
|
||||
func (v *VolumeService) CreateVolume(name string) (*db.Volume, int, error) {
|
||||
existingVol, _ := volumeQueries.QueryVolumeByName(name)
|
||||
|
||||
if existingVol != nil {
|
||||
return nil, http.StatusConflict, fmt.Errorf("volume %s already exists", name)
|
||||
}
|
||||
|
||||
cfg := core.LoadConfig()
|
||||
|
||||
volPathHost := filepath.Join(cfg.VolumeRootHost, name)
|
||||
volPathLocal := filepath.Join(constants.VolumeRootLocal, name)
|
||||
|
||||
if err := os.MkdirAll(volPathLocal, 0755); err != nil {
|
||||
return nil, http.StatusInternalServerError, fmt.Errorf("failed to create volume directory: %w", err)
|
||||
}
|
||||
|
||||
if err := volumeQueries.InsertVolume(name, volPathHost); err != nil {
|
||||
if strings.Contains(err.Error(), "UNIQUE") {
|
||||
return nil, http.StatusConflict, fmt.Errorf("volume %s already exists", name)
|
||||
}
|
||||
|
||||
return nil, http.StatusInternalServerError, fmt.Errorf("failed to create volume in database: %w", err)
|
||||
}
|
||||
|
||||
return &db.Volume{
|
||||
Name: name,
|
||||
Path: volPathHost,
|
||||
}, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (v *VolumeService) GetVolume(name string) (*db.Volume, error) {
|
||||
vol, err := volumeQueries.QueryVolumeByName(name)
|
||||
|
||||
return vol, err
|
||||
}
|
||||
|
||||
func (v *VolumeService) ListVolumes() []VolumeInfo {
|
||||
vols, _ := volumeQueries.QueryVolumes()
|
||||
volumes := []VolumeInfo{}
|
||||
|
||||
for _, vol := range vols {
|
||||
volumes = append(volumes, VolumeInfo{
|
||||
Name: vol.Name,
|
||||
Mountpoint: vol.Path,
|
||||
CreatedAt: vol.CreatedAt.Format(DateFormat),
|
||||
Err: "",
|
||||
})
|
||||
}
|
||||
|
||||
return volumes
|
||||
}
|
||||
|
||||
func (v *VolumeService) DeleteVolume(name string) (int, error) {
|
||||
vol, _ := volumeQueries.QueryVolumeByName(name)
|
||||
|
||||
if vol == nil {
|
||||
return http.StatusNotFound, fmt.Errorf("volume %s not found", name)
|
||||
}
|
||||
|
||||
if err := volumeQueries.RemoveVolume(name); err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("failed to remove volume from database: %w", err)
|
||||
}
|
||||
|
||||
// os.RemoveAll(vol.Path) ?? depends on whether we want to delete the actual directory
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
31
internal/modules/volumes/types.go
Normal file
31
internal/modules/volumes/types.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package volumes
|
||||
|
||||
var DateFormat = "2006-01-02T15:04:05Z"
|
||||
|
||||
type CreateVolumeBody struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
type CreateVolumeResponse struct {
|
||||
Name string `json:"name"`
|
||||
Mountpoint string `json:"mountpoint"`
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
type GetVolumeResponse struct {
|
||||
Name string `json:"name"`
|
||||
Mountpoint string `json:"mountpoint"`
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
type VolumeInfo struct {
|
||||
Name string `json:"name"`
|
||||
Mountpoint string `json:"mountpoint"`
|
||||
Err string `json:"err,omitempty"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
type ListVolumesResponse struct {
|
||||
Volumes []VolumeInfo `json:"volumes"`
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user