This commit is contained in:
2026-02-15 13:13:11 +01:00
parent 8b592db3dd
commit 2a5221c29a
8 changed files with 243 additions and 35 deletions

View File

@@ -9,12 +9,15 @@ import (
"io"
"net/http"
"os/exec"
"strings"
"time"
"github.com/mistifyio/go-zfs"
"github.com/pierrec/lz4/v4"
)
var uploadUrl = "/upload-stream/"
// SnapshotResult contains the result of a snapshot creation and send operation.
type SnapshotResult struct {
FullBackup bool
@@ -121,7 +124,14 @@ func (c *Client) SendIncrementalHTTP(snapshot *zfs.Dataset, base string) error {
}
reqBody, _ := json.Marshal(uploadReq)
resp, err := http.Post(c.config.ServerURL+"/upload", "application/json", bytes.NewBuffer(reqBody))
uploadURL := c.config.ServerURL
// Ensure proper URL format
if !strings.HasSuffix(uploadURL, "/") {
uploadURL += "/"
}
uploadURL += "upload"
resp, err := http.Post(uploadURL, "application/json", bytes.NewBuffer(reqBody))
if err != nil {
return fmt.Errorf("failed to request upload: %v", err)
}
@@ -134,7 +144,6 @@ func (c *Client) SendIncrementalHTTP(snapshot *zfs.Dataset, base string) error {
UploadMethod string `json:"upload_method"`
StorageKey string `json:"storage_key"`
}
if err := json.NewDecoder(resp.Body).Decode(&uploadResp); err != nil {
return fmt.Errorf("failed to decode response: %v", err)
}
@@ -193,7 +202,23 @@ func (c *Client) streamToServer(snapshot *zfs.Dataset, base, uploadURL, storageK
}
// Create HTTP request to server
req, err := http.NewRequest("POST", c.config.ServerURL+uploadURL, reader)
// Build full URL properly - check if uploadURL is already full URL
fullURL := uploadURL
// If uploadURL is a relative path, prepend server URL
if !strings.HasPrefix(uploadURL, "http://") && !strings.HasPrefix(uploadURL, "https://") {
fullURL = c.config.ServerURL
// Remove trailing slash from base URL if present
fullURL = strings.TrimRight(fullURL, "/")
// Add leading slash to upload URL if not present
if !strings.HasPrefix(uploadURL, "/") {
uploadURL = "/" + uploadURL
}
fullURL += uploadURL
}
fmt.Printf(" Streaming to: %s\n", fullURL)
req, err := http.NewRequest("POST", fullURL, reader)
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}

View File

@@ -19,7 +19,8 @@ type Config struct {
// LocalDataset is the ZFS dataset to backup
LocalDataset string `json:"local_dataset"`
// Compress enables LZ4 compression for transfers
Compress bool `json:"compress"`
Compress bool `json:"compress"`
UploadURL string `json:upload_url`
}
// LoadConfig loads client configuration from environment variables and .env file.
@@ -34,6 +35,7 @@ func LoadConfig() *Config {
ServerURL: getEnv("SERVER_URL", "http://localhost:8080"),
LocalDataset: getEnv("LOCAL_DATASET", "tank/data"),
Compress: getEnv("COMPRESS", "true") == "true",
UploadURL: "/upload-stream/",
}
}