feat: sqlite storage

This commit is contained in:
Nicolas Meienberger
2025-08-09 19:54:29 +02:00
parent 388114d536
commit d0bf890386
14 changed files with 189 additions and 26 deletions

View File

@@ -1,3 +1,3 @@
package constants
var VolumeRoot = "/tmp/ironmount"
var VolumeRoot = "/home/nicolas/ironmount/tmp"

92
internal/db/db.go Normal file
View File

@@ -0,0 +1,92 @@
package db
import (
"database/sql"
"log"
_ "modernc.org/sqlite"
)
type Volume struct {
Name string `json:"name"`
Path string `json:"path"`
}
// DB is the global database connection
var DB, err = sql.Open("sqlite", "file:ironmount.db")
// Init initializes the database and creates the volumes table if it doesn't exist
func Init() {
if err != nil {
panic(err)
}
_, err = DB.Exec(`
CREATE TABLE IF NOT EXISTS volumes (
name TEXT PRIMARY KEY,
path TEXT NOT NULL
);
`)
if err != nil {
panic(err)
}
}
func GetVolumeByName(n string) (*Volume, error) {
var path string
var name string
err := DB.QueryRow("SELECT name, path FROM volumes WHERE name = ?", n).Scan(&name, &path)
if err != nil {
return nil, err
}
return &Volume{
Name: name,
Path: path,
}, nil
}
func CreateVolume(name, path string) error {
_, err := DB.Exec("INSERT INTO volumes (name, path) VALUES (?, ?)", name, path)
if err != nil {
return err
}
return nil
}
func RemoveVolume(name string) error {
_, err := DB.Exec("DELETE FROM volumes WHERE name = ?", name)
log.Printf("Removing volume: %s", name)
log.Printf("Error removing volume: %v", err)
if err != nil {
return err
}
return nil
}
func ListVolumes() ([]Volume, error) {
rows, err := DB.Query("SELECT name, path FROM volumes")
if err != nil {
return nil, err
}
defer rows.Close()
var volumes []Volume
for rows.Next() {
var vol Volume
if err := rows.Scan(&vol.Name, &vol.Path); err != nil {
return nil, err
}
volumes = append(volumes, vol)
}
if err := rows.Err(); err != nil {
return nil, err
}
return volumes, nil
}

View File

@@ -3,6 +3,7 @@ package driver
import (
"encoding/json"
"ironmount/internal/constants"
"ironmount/internal/db"
"log"
"net/http"
"os"
@@ -24,12 +25,13 @@ func Create(w http.ResponseWriter, r *http.Request) {
return
}
db.CreateVolume(req.Name, volPath)
response := map[string]string{
"Name": req.Name,
"Mountpoint": volPath,
"Err": "",
}
volumes[req.Name] = Volume{Name: req.Name, Path: volPath}
_ = json.NewEncoder(w).Encode(response)
}

View File

@@ -2,6 +2,7 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
)
@@ -14,7 +15,18 @@ func Get(w http.ResponseWriter, r *http.Request) {
}
_ = json.NewDecoder(r.Body).Decode(&req)
vol := volumes[req.Name]
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Printf("Error retrieving volume: %s", err.Error())
response := map[string]string{
"Err": err.Error(),
}
_ = json.NewEncoder(w).Encode(response)
return
}
response := map[string]any{
"Volume": map[string]any{

View File

@@ -2,6 +2,7 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
)
@@ -9,14 +10,18 @@ import (
func List(w http.ResponseWriter, r *http.Request) {
log.Printf("Received list request: %s", r.URL.Path)
var vols []map[string]string
for _, v := range volumes {
vols = append(vols, map[string]string{
"Name": v.Name,
volumes, err := db.ListVolumes()
if err != nil {
log.Printf("Error listing volumes: %s", err.Error())
json.NewEncoder(w).Encode(map[string]any{
"Volumes": nil,
"Err": err.Error(),
})
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"Volumes": vols,
json.NewEncoder(w).Encode(map[string]any{
"Volumes": volumes,
"Err": "",
})
}

View File

@@ -2,6 +2,7 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
)
@@ -17,9 +18,12 @@ func Mount(w http.ResponseWriter, r *http.Request) {
return
}
vol, ok := volumes[req.Name]
if !ok {
_ = json.NewEncoder(w).Encode(map[string]string{"Err": "volume not found"})
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Printf("Error retrieving volume: %s", err.Error())
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
return
}

View File

@@ -2,6 +2,7 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
)
@@ -12,9 +13,11 @@ func Path(w http.ResponseWriter, r *http.Request) {
var req PathRequest
_ = json.NewDecoder(r.Body).Decode(&req)
vol, ok := volumes[req.Name]
if !ok {
_ = json.NewEncoder(w).Encode(map[string]string{"Err": "volume not found"})
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
return
}

View File

@@ -2,8 +2,10 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
"os"
)
func Remove(w http.ResponseWriter, r *http.Request) {
@@ -12,6 +14,17 @@ func Remove(w http.ResponseWriter, r *http.Request) {
var req RemoveRequest
_ = json.NewDecoder(r.Body).Decode(&req)
delete(volumes, req.Name)
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Printf("Error retrieving volume: %s", err.Error())
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
return
}
db.RemoveVolume(vol.Name)
os.RemoveAll(vol.Path)
_ = json.NewEncoder(w).Encode(map[string]string{"Err": ""})
}

View File

@@ -1,8 +0,0 @@
package driver
type Volume struct {
Name string
Path string
}
var volumes = map[string]Volume{}