2026-04-10 03:39:46 +00:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2026-04-25 17:28:56 +00:00
|
|
|
"sync"
|
2026-04-10 03:39:46 +00:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
const flushInterval = 4 * time.Hour
|
|
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
// Client appends JSON entries to an array stored at a WebDAV URL.
|
2026-04-25 17:28:56 +00:00
|
|
|
// Entries are buffered in memory and flushed every 4 hours. On a day rollover,
|
|
|
|
|
// buffered entries are flushed to the outgoing day before loading the new day.
|
2026-04-10 03:39:46 +00:00
|
|
|
type Client struct {
|
|
|
|
|
url string
|
|
|
|
|
username string
|
|
|
|
|
password string
|
|
|
|
|
http *http.Client
|
2026-04-25 17:28:56 +00:00
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
day string
|
|
|
|
|
dayURL string
|
|
|
|
|
entries []json.RawMessage
|
|
|
|
|
lastFlush time.Time
|
2026-04-10 03:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewClientFromEnv creates a Client from WEBDAV_URL, WEBDAV_USER, and
|
|
|
|
|
// WEBDAV_PASSWORD environment variables. Returns nil if WEBDAV_URL is unset.
|
|
|
|
|
func NewClientFromEnv() *Client {
|
|
|
|
|
url := os.Getenv("WEBDAV_URL")
|
|
|
|
|
if url == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &Client{
|
|
|
|
|
url: url,
|
|
|
|
|
username: os.Getenv("WEBDAV_USER"),
|
|
|
|
|
password: os.Getenv("WEBDAV_PASSWORD"),
|
|
|
|
|
http: &http.Client{Timeout: 15 * time.Second},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
// Append buffers entry in memory and flushes to WebDAV every 4 hours.
|
|
|
|
|
// On a day boundary, any buffered entries are flushed to the outgoing day's
|
|
|
|
|
// file before the new day's file is loaded.
|
|
|
|
|
func (c *Client) Append(entry any) error {
|
|
|
|
|
c.mu.Lock()
|
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
today := time.Now().UTC().Format("2006-01-02")
|
|
|
|
|
|
|
|
|
|
if c.day != today {
|
|
|
|
|
if len(c.entries) > 0 {
|
|
|
|
|
if err := c.flush(); err != nil {
|
|
|
|
|
return fmt.Errorf("webdav: flushing before day rollover: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err := c.loadDay(today); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entryBytes, err := json.Marshal(entry)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("webdav: marshaling entry: %w", err)
|
|
|
|
|
}
|
|
|
|
|
c.entries = append(c.entries, json.RawMessage(entryBytes))
|
|
|
|
|
|
|
|
|
|
if time.Since(c.lastFlush) >= flushInterval {
|
|
|
|
|
return c.flush()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2026-04-10 03:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
// loadDay fetches existing entries for the given day and resets the flush timer.
|
|
|
|
|
// Must be called with c.mu held.
|
|
|
|
|
func (c *Client) loadDay(day string) error {
|
|
|
|
|
url := fmt.Sprintf("%s/%s.json", c.url, day)
|
2026-04-10 03:39:46 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("webdav GET: %w", err)
|
|
|
|
|
}
|
|
|
|
|
req.SetBasicAuth(c.username, c.password)
|
|
|
|
|
|
|
|
|
|
resp, err := c.http.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("webdav GET: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
c.entries = nil
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
|
case http.StatusOK:
|
2026-04-10 03:39:46 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("webdav GET read body: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if len(bytes.TrimSpace(body)) > 0 {
|
2026-04-25 17:28:56 +00:00
|
|
|
if err := json.Unmarshal(body, &c.entries); err != nil {
|
2026-04-10 03:39:46 +00:00
|
|
|
return fmt.Errorf("webdav: parsing existing array: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-25 17:28:56 +00:00
|
|
|
log.Printf("webdav: loaded %d existing entries for %s", len(c.entries), day)
|
|
|
|
|
case http.StatusNotFound:
|
|
|
|
|
log.Printf("webdav: no existing file for %s, starting fresh", day)
|
|
|
|
|
default:
|
2026-04-10 03:39:46 +00:00
|
|
|
return fmt.Errorf("webdav GET returned %s", resp.Status)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
c.day = day
|
|
|
|
|
c.dayURL = url
|
|
|
|
|
c.lastFlush = time.Now()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-04-10 03:39:46 +00:00
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
// flush writes all buffered entries to the current day's file.
|
|
|
|
|
// Must be called with c.mu held.
|
|
|
|
|
func (c *Client) flush() error {
|
|
|
|
|
data, err := json.Marshal(c.entries)
|
2026-04-10 03:39:46 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("webdav: marshaling array: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPut, c.dayURL, bytes.NewReader(data))
|
2026-04-10 03:39:46 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("webdav PUT: %w", err)
|
|
|
|
|
}
|
2026-04-25 17:28:56 +00:00
|
|
|
req.SetBasicAuth(c.username, c.password)
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2026-04-10 03:39:46 +00:00
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
resp, err := c.http.Do(req)
|
2026-04-10 03:39:46 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("webdav PUT: %w", err)
|
|
|
|
|
}
|
2026-04-25 17:28:56 +00:00
|
|
|
defer resp.Body.Close()
|
2026-04-10 03:39:46 +00:00
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
|
return fmt.Errorf("webdav PUT returned %s", resp.Status)
|
2026-04-10 03:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 17:28:56 +00:00
|
|
|
log.Printf("webdav: flushed %d entries to %s (%s)", len(c.entries), c.dayURL, resp.Status)
|
|
|
|
|
c.lastFlush = time.Now()
|
2026-04-10 03:39:46 +00:00
|
|
|
return nil
|
|
|
|
|
}
|