From 826434a8597122847f643abe835d5c6e5dd74c4c Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 10:45:04 +0530 Subject: refactor(service): rename load to reload --- cmd/galleryd/main.go | 2 +- internal/api/api_test.go | 2 +- internal/service/load.go | 33 --------------------------------- internal/service/reload.go | 33 +++++++++++++++++++++++++++++++++ internal/service/service.go | 2 +- 5 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 internal/service/load.go create mode 100644 internal/service/reload.go diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 1edd68a..2c26a96 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -65,7 +65,7 @@ func main() { scanner, builder, ) - if err := galleryService.Load(ctx); err != nil { + if err := galleryService.Reload(ctx); err != nil { log.Fatal(err) } diff --git a/internal/api/api_test.go b/internal/api/api_test.go index c1980fd..0b1c68d 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -37,7 +37,7 @@ func (f fakeThumbnailGenerator) Generate( }, nil } -func (f fakeGalleryService) Load(ctx context.Context) error { +func (f fakeGalleryService) Reload(ctx context.Context) error { return nil } diff --git a/internal/service/load.go b/internal/service/load.go deleted file mode 100644 index 09bbc20..0000000 --- a/internal/service/load.go +++ /dev/null @@ -1,33 +0,0 @@ -package service - -import ( - "context" - - "github.com/RanXom/galleryd/internal/gallery" -) - -// Load scans the configured roots and rebuilds the in-memory gallery. -func (s *galleryService) Load(ctx context.Context) error { - files, err := s.scanner.Scan(ctx) - if err != nil { - return err - } - - photos, err := s.builder.Build(ctx, files) - if err != nil { - return err - } - - index := galleryIndex{ - photos: photos, - byID: make(map[string]gallery.Photo, len(photos)), - } - - for _, photo := range photos { - index.byID[photo.ID] = photo - } - - s.index = index - - return nil -} diff --git a/internal/service/reload.go b/internal/service/reload.go new file mode 100644 index 0000000..98e4710 --- /dev/null +++ b/internal/service/reload.go @@ -0,0 +1,33 @@ +package service + +import ( + "context" + + "github.com/RanXom/galleryd/internal/gallery" +) + +// Load scans the configured roots and rebuilds the in-memory gallery. +func (s *galleryService) Reload(ctx context.Context) error { + files, err := s.scanner.Scan(ctx) + if err != nil { + return err + } + + photos, err := s.builder.Build(ctx, files) + if err != nil { + return err + } + + index := galleryIndex{ + photos: photos, + byID: make(map[string]gallery.Photo, len(photos)), + } + + for _, photo := range photos { + index.byID[photo.ID] = photo + } + + s.index = index + + return nil +} diff --git a/internal/service/service.go b/internal/service/service.go index 68a764e..063c0e7 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -9,7 +9,7 @@ import ( // GalleryService provides gallery operations. type GalleryService interface { // Load scans the configured roots and builds the in-memory gallery. - Load(ctx context.Context) error + Reload(ctx context.Context) error // Gallery returns the currently loaded gallery. Gallery(ctx context.Context) ([]gallery.Photo, error) -- cgit v1.2.3 From ad97c2f3222dcdb8c124747feb4564ca9069fa1e Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 10:57:01 +0530 Subject: refactor(service): synchronize gallery access --- internal/service/gallery.go | 4 ++++ internal/service/gallery_service.go | 3 +++ internal/service/photo.go | 3 +++ internal/service/reload.go | 4 +++- internal/service/service.go | 2 +- 5 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/service/gallery.go b/internal/service/gallery.go index 30abefe..67d0106 100644 --- a/internal/service/gallery.go +++ b/internal/service/gallery.go @@ -1,6 +1,8 @@ package service import ( + "sync" + "github.com/RanXom/galleryd/internal/gallery" "github.com/RanXom/galleryd/internal/scanner" ) @@ -10,5 +12,7 @@ type galleryService struct { scanner *scanner.Scanner builder *gallery.Builder + mu sync.RWMutex + index galleryIndex } diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go index e500bec..23a8e85 100644 --- a/internal/service/gallery_service.go +++ b/internal/service/gallery_service.go @@ -9,6 +9,9 @@ import ( // Gallery scans the configured roots and builds the gallery. func (s *galleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { + s.mu.RLock() + defer s.mu.RUnlock() + select { case <-ctx.Done(): return nil, ctx.Err() diff --git a/internal/service/photo.go b/internal/service/photo.go index 018ba6b..7242d8e 100644 --- a/internal/service/photo.go +++ b/internal/service/photo.go @@ -14,6 +14,9 @@ func (s *galleryService) Photo( ctx context.Context, id string, ) (gallery.Photo, error) { + s.mu.RLock() + defer s.mu.RUnlock() + select { case <-ctx.Done(): return gallery.Photo{}, ctx.Err() diff --git a/internal/service/reload.go b/internal/service/reload.go index 98e4710..9460dbc 100644 --- a/internal/service/reload.go +++ b/internal/service/reload.go @@ -6,7 +6,7 @@ import ( "github.com/RanXom/galleryd/internal/gallery" ) -// Load scans the configured roots and rebuilds the in-memory gallery. +// Reload scans the configured roots and rebuilds the in-memory gallery. func (s *galleryService) Reload(ctx context.Context) error { files, err := s.scanner.Scan(ctx) if err != nil { @@ -27,7 +27,9 @@ func (s *galleryService) Reload(ctx context.Context) error { index.byID[photo.ID] = photo } + s.mu.Lock() s.index = index + s.mu.Unlock() return nil } diff --git a/internal/service/service.go b/internal/service/service.go index 063c0e7..0b9b253 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -8,7 +8,7 @@ import ( // GalleryService provides gallery operations. type GalleryService interface { - // Load scans the configured roots and builds the in-memory gallery. + // Reload scans the configured roots and rebuilds the in-memory gallery. Reload(ctx context.Context) error // Gallery returns the currently loaded gallery. -- cgit v1.2.3 From 79f4c3ba1319eeeca1c3654e91393adf533e1386 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 11:27:49 +0530 Subject: test(service): verify initial gallery reload --- internal/service/reload_test.go | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 internal/service/reload_test.go diff --git a/internal/service/reload_test.go b/internal/service/reload_test.go new file mode 100644 index 0000000..e1a1da1 --- /dev/null +++ b/internal/service/reload_test.go @@ -0,0 +1,48 @@ +package service + +import ( + "context" + "testing" + + "github.com/RanXom/galleryd/internal/gallery" + "github.com/RanXom/galleryd/internal/metadata" + "github.com/RanXom/galleryd/internal/scanner" +) + +func TestReloadInitialLoad(t *testing.T) { + ctx := context.Background() + + sc := scanner.New(scanner.Config{ + Roots: []string{ + "../scanner/testdata/gallery", + }, + }) + + builder := gallery.New(metadata.New()) + + service := New(sc, builder) + + if err := service.Reload(ctx); err != nil { + t.Fatalf("reload gallery: %v", err) + } + + photos, err := service.Gallery(ctx) + if err != nil { + t.Fatalf("gallery: %v", err) + } + + if len(photos) == 0 { + t.Fatal("expected gallery to contain photos") + } + + for _, photo := range photos { + got, err := service.Photo(ctx, photo.ID) + if err != nil { + t.Fatalf("lookup photo %q: %v", photo.ID, err) + } + + if got.ID != photo.ID { + t.Fatalf("expected id %q, got %q", photo.ID, got.ID) + } + } +} -- cgit v1.2.3 From 48fae70cf1a6b99966cd65c0229a936382984bc6 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 11:31:34 +0530 Subject: test(service): verify reload replaces gallery snapshot --- internal/service/reload_test.go | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/internal/service/reload_test.go b/internal/service/reload_test.go index e1a1da1..c8326ca 100644 --- a/internal/service/reload_test.go +++ b/internal/service/reload_test.go @@ -2,11 +2,14 @@ package service import ( "context" + "os" + "path/filepath" "testing" "github.com/RanXom/galleryd/internal/gallery" "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/scanner" + "github.com/RanXom/galleryd/internal/testfixtures" ) func TestReloadInitialLoad(t *testing.T) { @@ -46,3 +49,77 @@ func TestReloadInitialLoad(t *testing.T) { } } } + +func TestReloadReplacesGallery(t *testing.T) { + ctx := context.Background() + + root := t.TempDir() + + copyFile := func(src, dst string) { + t.Helper() + + data, err := os.ReadFile(src) + if err != nil { + t.Fatalf("read fixture: %v", err) + } + + if err := os.WriteFile(dst, data, 0o644); err != nil { + t.Fatalf("write fixture: %v", err) + } + } + + first := filepath.Join(root, "first.jpg") + second := filepath.Join(root, "second.jpg") + + copyFile(testfixtures.Canon40D(), first) + + sc := scanner.New(scanner.Config{ + Roots: []string{root}, + }) + + builder := gallery.New(metadata.New()) + + service := New(sc, builder) + + if err := service.Reload(ctx); err != nil { + t.Fatalf("first reload: %v", err) + } + + before, err := service.Gallery(ctx) + if err != nil { + t.Fatalf("gallery: %v", err) + } + + if len(before) != 1 { + t.Fatalf("expected 1 photo, got %d", len(before)) + } + + oldID := before[0].ID + + if err := os.Remove(first); err != nil { + t.Fatalf("remove first image: %v", err) + } + + copyFile(testfixtures.NikonD70(), second) + + if err := service.Reload(ctx); err != nil { + t.Fatalf("second reload: %v", err) + } + + after, err := service.Gallery(ctx) + if err != nil { + t.Fatalf("gallery: %v", err) + } + + if len(after) != 1 { + t.Fatalf("expected 1 photo, got %d", len(after)) + } + + if after[0].ID == oldID { + t.Fatal("gallery snapshot was not replaced") + } + + if _, err := service.Photo(ctx, oldID); err != ErrPhotoNotFound { + t.Fatalf("expected ErrPhotoNotFound, got %v", err) + } +} -- cgit v1.2.3 From ed87aebc0296379280fd65d618127b4eeb848a98 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 11:37:22 +0530 Subject: feat(api): add manual gallery reload endpoint --- internal/api/api_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- internal/api/new.go | 5 +++++ internal/api/reload.go | 25 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 internal/api/reload.go diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 0b1c68d..dce42e4 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -17,6 +17,9 @@ import ( type fakeGalleryService struct { photos []gallery.Photo err error + + reloadCalls int + reloadErr error } type fakeThumbnailGenerator struct { @@ -37,8 +40,9 @@ func (f fakeThumbnailGenerator) Generate( }, nil } -func (f fakeGalleryService) Reload(ctx context.Context) error { - return nil +func (f *fakeGalleryService) Reload(ctx context.Context) error { + f.reloadCalls++ + return f.reloadErr } func (f fakeGalleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { @@ -62,7 +66,7 @@ func TestPhotos(t *testing.T) { srv := New(Config{ Address: ":0", - Gallery: fakeGalleryService{ + Gallery: &fakeGalleryService{ photos: []gallery.Photo{ { ID: "abc123", @@ -146,7 +150,7 @@ func TestThumbnail(t *testing.T) { srv := New(Config{ Address: ":0", - Gallery: fakeGalleryService{ + Gallery: &fakeGalleryService{ photos: []gallery.Photo{ { ID: "abc123", @@ -184,3 +188,37 @@ func TestThumbnail(t *testing.T) { ) } } + +func TestReload(t *testing.T) { + gallery := &fakeGalleryService{} + + srv := New(Config{ + Address: ":0", + Gallery: gallery, + }) + + req := httptest.NewRequest( + http.MethodPost, + "/api/reload", + nil, + ) + + rec := httptest.NewRecorder() + + srv.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusNoContent { + t.Fatalf( + "expected %d, got %d", + http.StatusNoContent, + rec.Code, + ) + } + + if gallery.reloadCalls != 1 { + t.Fatalf( + "expected Reload() to be called once, got %d", + gallery.reloadCalls, + ) + } +} diff --git a/internal/api/new.go b/internal/api/new.go index 7febfe7..b95a909 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -40,5 +40,10 @@ func New(config Config) *Server { srv.handlePhoto, ) + srv.mux.HandleFunc( + "POST /api/reload", + srv.handleReload, + ) + return srv } diff --git a/internal/api/reload.go b/internal/api/reload.go new file mode 100644 index 0000000..bd3cffb --- /dev/null +++ b/internal/api/reload.go @@ -0,0 +1,25 @@ +package api + +import ( + "log" + "net/http" +) + +func (s *Server) handleReload( + w http.ResponseWriter, + r *http.Request, +) { + if err := s.gallery.Reload(r.Context()); err != nil { + log.Printf("reload gallery: %v", err) + + writeError( + w, + http.StatusInternalServerError, + "failed to reload gallery", + ) + + return + } + + w.WriteHeader(http.StatusNoContent) +} -- cgit v1.2.3 From 9fd2f9eabe93713c0e8fb7dd094e77109db84265 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 11:46:12 +0530 Subject: feat(watcher): introduce filesystem watcher abstraction --- internal/service/gallery.go | 2 +- internal/watcher/config.go | 11 +++++++++++ internal/watcher/doc.go | 7 +++++++ internal/watcher/watcher.go | 21 +++++++++++++++++++++ internal/watcher/watcher_test.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 internal/watcher/config.go create mode 100644 internal/watcher/doc.go create mode 100644 internal/watcher/watcher.go create mode 100644 internal/watcher/watcher_test.go diff --git a/internal/service/gallery.go b/internal/service/gallery.go index 67d0106..0d1e760 100644 --- a/internal/service/gallery.go +++ b/internal/service/gallery.go @@ -7,7 +7,7 @@ import ( "github.com/RanXom/galleryd/internal/scanner" ) -// GalleryService builds gallery photos +// galleryService implements GalleryService. type galleryService struct { scanner *scanner.Scanner builder *gallery.Builder diff --git a/internal/watcher/config.go b/internal/watcher/config.go new file mode 100644 index 0000000..ad12d0c --- /dev/null +++ b/internal/watcher/config.go @@ -0,0 +1,11 @@ +package watcher + +import "time" + +// Configures a filesystem watcher +type Config struct { + Roots []string + + Debounce time.Duration + OnChange func() +} diff --git a/internal/watcher/doc.go b/internal/watcher/doc.go new file mode 100644 index 0000000..95801aa --- /dev/null +++ b/internal/watcher/doc.go @@ -0,0 +1,7 @@ +// Package watcher watches configured directories for filesystem changes. +// +// The package is intentionally unaware of galleries, thumbnails and HTTP. +// +// Its sole responsibility is detecting filesystem changes and notifying +// the caller through a callback. +package watcher diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go new file mode 100644 index 0000000..5465554 --- /dev/null +++ b/internal/watcher/watcher.go @@ -0,0 +1,21 @@ +package watcher + +import "context" + +type Watcher struct { + config Config +} + +func New(config Config) *Watcher { + return &Watcher{ + config: config, + } +} + +// Run starts the watcher. +// +// Placeholder implementation. +func (w *Watcher) Run(ctx context.Context) error { + <-ctx.Done() + return nil +} diff --git a/internal/watcher/watcher_test.go b/internal/watcher/watcher_test.go new file mode 100644 index 0000000..53c0baa --- /dev/null +++ b/internal/watcher/watcher_test.go @@ -0,0 +1,31 @@ +package watcher + +import ( + "context" + "testing" + "time" +) + +func TestRunReturnsOnContextCancellation(t *testing.T) { + w := New(Config{}) + + ctx, cancel := context.WithCancel(context.Background()) + + done := make(chan error, 1) + + go func() { + done <- w.Run(ctx) + }() + + cancel() + + select { + case err := <-done: + if err != nil { + t.Fatalf("expected nil, got %v", err) + } + + case <-time.After(time.Second): + t.Fatal("watcher did not exit") + } +} -- cgit v1.2.3 From b336f4a1328e24ac77a1cfee8c52b18caa555620 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 12:03:34 +0530 Subject: feat(watcher): watch configured directories --- go.mod | 2 ++ go.sum | 4 +++ internal/watcher/config.go | 2 +- internal/watcher/fsnotify.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ internal/watcher/watcher.go | 10 ------- 5 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 internal/watcher/fsnotify.go diff --git a/go.mod b/go.mod index 0496f5f..96e8013 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,6 @@ require ( github.com/disintegration/imaging v1.6.2 github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd golang.org/x/image v0.43.0 + github.com/fsnotify/fsnotify v1.10.1 + golang.org/x/sys v0.13.0 // indirect ) diff --git a/go.sum b/go.sum index ef997d8..170a81e 100644 --- a/go.sum +++ b/go.sum @@ -2,9 +2,13 @@ github.com/chai2010/webp v1.4.0 h1:6DA2pkkRUPnbOHvvsmGI3He1hBKf/bkRlniAiSGuEko= github.com/chai2010/webp v1.4.0/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU= github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho= +github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.43.0 h1:FLxcP4ec2350nTfOC8ysKtqYSIFbk/QGjw1ZHNP4tsY= golang.org/x/image v0.43.0/go.mod h1:rrpelvGFt+kLPAjPM4HeWPgrl0FtafueU//e5N0qk/Q= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/internal/watcher/config.go b/internal/watcher/config.go index ad12d0c..a59474f 100644 --- a/internal/watcher/config.go +++ b/internal/watcher/config.go @@ -2,7 +2,7 @@ package watcher import "time" -// Configures a filesystem watcher +// Config configures a filesystem watcher type Config struct { Roots []string diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go new file mode 100644 index 0000000..e051c52 --- /dev/null +++ b/internal/watcher/fsnotify.go @@ -0,0 +1,62 @@ +package watcher + +import ( + "context" + "fmt" + "io/fs" + "log" + "path/filepath" + + "github.com/fsnotify/fsnotify" +) + +// Run starts watching the configured roots. +// +// Filesystem events are currently logged for debugging. +// Future commits will debounce these events and invoke OnChange. +func (w *Watcher) Run(ctx context.Context) error { + fswatcher, err := fsnotify.NewWatcher() + if err != nil { + return fmt.Errorf("create watcher: %w", err) + } + defer func() { + _ = fswatcher.Close() + }() + + for _, root := range w.config.Roots { + if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() { + return nil + } + + return fswatcher.Add(path) + }); err != nil { + return err + } + } + + for { + select { + case <-ctx.Done(): + return nil + + case event, ok := <-fswatcher.Events: + if !ok { + return nil + } + + log.Printf("watcher: %v %s", event.Op, event.Name) + + case err, ok := <-fswatcher.Errors: + if !ok { + return nil + } + + log.Printf("watcher error: %v", err) + } + } +} diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 5465554..1ca8e37 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -1,7 +1,5 @@ package watcher -import "context" - type Watcher struct { config Config } @@ -11,11 +9,3 @@ func New(config Config) *Watcher { config: config, } } - -// Run starts the watcher. -// -// Placeholder implementation. -func (w *Watcher) Run(ctx context.Context) error { - <-ctx.Done() - return nil -} -- cgit v1.2.3 From bd0b89ab49806b976eb0d50dd6f79afdec8b7d11 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 12:29:53 +0530 Subject: feat(watcher): start filesystem watcher --- cmd/galleryd/main.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 2c26a96..27faa5d 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -14,6 +14,7 @@ import ( "github.com/RanXom/galleryd/internal/scanner" "github.com/RanXom/galleryd/internal/service" "github.com/RanXom/galleryd/internal/thumbnail" + "github.com/RanXom/galleryd/internal/watcher" ) func main() { @@ -69,11 +70,21 @@ func main() { log.Fatal(err) } + fswatcher := watcher.New(watcher.Config{ + Roots: dirs, + }) + thumbnailGenerator, err := thumbnail.New(*cacheDir) if err != nil { log.Fatal(err) } + go func() { + if err := fswatcher.Run(ctx); err != nil { + log.Printf("watcher: %v", err) + } + }() + server := api.New(api.Config{ Address: *addr, Gallery: galleryService, -- cgit v1.2.3 From d2c0c970f690d52ee3675f377a5eebc22f84afab Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 12:35:51 +0530 Subject: feat(watcher): watch newly created directories --- internal/watcher/fsnotify.go | 32 +++++++++++++++++++------------- internal/watcher/watch.go | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 internal/watcher/watch.go diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go index e051c52..7251907 100644 --- a/internal/watcher/fsnotify.go +++ b/internal/watcher/fsnotify.go @@ -3,9 +3,8 @@ package watcher import ( "context" "fmt" - "io/fs" "log" - "path/filepath" + "os" "github.com/fsnotify/fsnotify" ) @@ -24,17 +23,7 @@ func (w *Watcher) Run(ctx context.Context) error { }() for _, root := range w.config.Roots { - if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - - if !d.IsDir() { - return nil - } - - return fswatcher.Add(path) - }); err != nil { + if err := watchTree(fswatcher, root); err != nil { return err } } @@ -51,6 +40,23 @@ func (w *Watcher) Run(ctx context.Context) error { log.Printf("watcher: %v %s", event.Op, event.Name) + if !event.Has(fsnotify.Create) { + continue + } + + info, err := os.Stat(event.Name) + if err != nil { + continue + } + + if !info.IsDir() { + continue + } + + if err := watchTree(fswatcher, event.Name); err != nil { + log.Printf("watch new directory: %v", err) + } + case err, ok := <-fswatcher.Errors: if !ok { return nil diff --git a/internal/watcher/watch.go b/internal/watcher/watch.go new file mode 100644 index 0000000..95146ce --- /dev/null +++ b/internal/watcher/watch.go @@ -0,0 +1,26 @@ +package watcher + +import ( + "io/fs" + "path/filepath" + + "github.com/fsnotify/fsnotify" +) + +// watchTree recursively watches root and all of its subdirectories. +func watchTree( + w *fsnotify.Watcher, + root string, +) error { + return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() { + return nil + } + + return w.Add(path) + }) +} -- cgit v1.2.3 From 9eb299c3ac08e2bea4dfe9e3355178566bcf0501 Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 00:36:31 +0530 Subject: refactor(watcher): isolate debounce loop --- internal/watcher/debounce.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ internal/watcher/fsnotify.go | 4 +++ internal/watcher/watcher.go | 11 +++++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 internal/watcher/debounce.go diff --git a/internal/watcher/debounce.go b/internal/watcher/debounce.go new file mode 100644 index 0000000..b2961bd --- /dev/null +++ b/internal/watcher/debounce.go @@ -0,0 +1,62 @@ +package watcher + +import ( + "context" + "time" +) + +// notify schedules a debounced notification. +// +// Multiple filesystem events collapse into a single callback. +func (w *Watcher) notify() { + if w.config.OnChange == nil { + return + } + + select { + case w.debounce <- struct{}{}: + default: + } +} + +func (w *Watcher) debounceLoop(ctx context.Context) { + if w.config.OnChange == nil { + return + } + + timer := time.NewTimer(time.Hour) + + if !timer.Stop() { + select { + case <-timer.C: + default: + } + } + + var pending bool + + for { + select { + case <-ctx.Done(): + return + + case <-w.debounce: + pending = true + + if !timer.Stop() { + select { + case <-timer.C: + default: + } + } + + timer.Reset(w.config.Debounce) + + case <-timer.C: + if pending { + pending = false + w.config.OnChange() + } + } + } +} diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go index 7251907..b759c50 100644 --- a/internal/watcher/fsnotify.go +++ b/internal/watcher/fsnotify.go @@ -22,6 +22,8 @@ func (w *Watcher) Run(ctx context.Context) error { _ = fswatcher.Close() }() + go w.debounceLoop(ctx) + for _, root := range w.config.Roots { if err := watchTree(fswatcher, root); err != nil { return err @@ -40,6 +42,8 @@ func (w *Watcher) Run(ctx context.Context) error { log.Printf("watcher: %v %s", event.Op, event.Name) + w.notify() + if !event.Has(fsnotify.Create) { continue } diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 1ca8e37..ee16b7b 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -1,11 +1,20 @@ package watcher +import "time" + type Watcher struct { config Config + + debounce chan struct{} } func New(config Config) *Watcher { + if config.Debounce <= 0 { + config.Debounce = 500 * time.Millisecond + } + return &Watcher{ - config: config, + config: config, + debounce: make(chan struct{}), } } -- cgit v1.2.3 From c8a8431158f07b75adc1fd86b344790075b9cf7a Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 10:33:58 +0530 Subject: refactor(watcher): buffer debounce notifications --- cmd/galleryd/main.go | 5 +++++ internal/watcher/watcher.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 27faa5d..e610cbc 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -72,6 +72,11 @@ func main() { fswatcher := watcher.New(watcher.Config{ Roots: dirs, + OnChange: func() { + if err := galleryService.Reload(context.Background()); err != nil { + log.Printf("reload gallery: %v", err) + } + }, }) thumbnailGenerator, err := thumbnail.New(*cacheDir) diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index ee16b7b..303eced 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -15,6 +15,6 @@ func New(config Config) *Watcher { return &Watcher{ config: config, - debounce: make(chan struct{}), + debounce: make(chan struct{}, 1), } } -- cgit v1.2.3 From 39445365fdc472087d429c7c81ffb02909168a99 Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 10:43:40 +0530 Subject: feat(watcher): reload gallery on filesystem changes --- cmd/galleryd/main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index e610cbc..ed1ee35 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -73,9 +73,14 @@ func main() { fswatcher := watcher.New(watcher.Config{ Roots: dirs, OnChange: func() { + log.Println("watcher: reloading gallery") + if err := galleryService.Reload(context.Background()); err != nil { log.Printf("reload gallery: %v", err) + return } + + log.Println("watcher: gallery reloaded") }, }) -- cgit v1.2.3 From cc42b82782eca935beb6b552e293e4458bb715bc Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 11:00:10 +0530 Subject: test(watcher): verify debounce behavior --- internal/watcher/debounce_test.go | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 internal/watcher/debounce_test.go diff --git a/internal/watcher/debounce_test.go b/internal/watcher/debounce_test.go new file mode 100644 index 0000000..a93ba05 --- /dev/null +++ b/internal/watcher/debounce_test.go @@ -0,0 +1,55 @@ +package watcher + +import ( + "sync/atomic" + "testing" + "time" +) + +func TestDebounceCollapsesNotifications(t *testing.T) { + var calls atomic.Int32 + + w := New(Config{ + Debounce: 25 * time.Millisecond, + OnChange: func() { + calls.Add(1) + }, + }) + + go w.debounceLoop(t.Context()) + + for range 10 { + w.notify() + } + + time.Sleep(75 * time.Millisecond) + + if got := calls.Load(); got != 1 { + t.Fatalf("expected 1 callback, got %d", got) + } +} + +func TestDebounceSeparateBursts(t *testing.T) { + var calls atomic.Int32 + + w := New(Config{ + Debounce: 20 * time.Millisecond, + OnChange: func() { + calls.Add(1) + }, + }) + + go w.debounceLoop(t.Context()) + + w.notify() + + time.Sleep(40 * time.Millisecond) + + w.notify() + + time.Sleep(40 * time.Millisecond) + + if got := calls.Load(); got != 2 { + t.Fatalf("expected 2 callbacks, got %d", got) + } +} -- cgit v1.2.3 From 868c02846962558a97a84c085c92baa5ba6e535f Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 11:02:36 +0530 Subject: refactor(watcher): refactor tests for faster testing time --- internal/watcher/debounce_test.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/internal/watcher/debounce_test.go b/internal/watcher/debounce_test.go index a93ba05..007dbd4 100644 --- a/internal/watcher/debounce_test.go +++ b/internal/watcher/debounce_test.go @@ -7,12 +7,18 @@ import ( ) func TestDebounceCollapsesNotifications(t *testing.T) { + done := make(chan struct{}, 1) var calls atomic.Int32 w := New(Config{ Debounce: 25 * time.Millisecond, OnChange: func() { calls.Add(1) + + select { + case done <- struct{}{}: + default: + } }, }) @@ -22,7 +28,11 @@ func TestDebounceCollapsesNotifications(t *testing.T) { w.notify() } - time.Sleep(75 * time.Millisecond) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for callback") + } if got := calls.Load(); got != 1 { t.Fatalf("expected 1 callback, got %d", got) @@ -30,12 +40,18 @@ func TestDebounceCollapsesNotifications(t *testing.T) { } func TestDebounceSeparateBursts(t *testing.T) { + done := make(chan struct{}, 2) var calls atomic.Int32 w := New(Config{ Debounce: 20 * time.Millisecond, OnChange: func() { calls.Add(1) + + select { + case done <- struct{}{}: + default: + } }, }) @@ -43,11 +59,19 @@ func TestDebounceSeparateBursts(t *testing.T) { w.notify() - time.Sleep(40 * time.Millisecond) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for first callback") + } w.notify() - time.Sleep(40 * time.Millisecond) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for second callback") + } if got := calls.Load(); got != 2 { t.Fatalf("expected 2 callbacks, got %d", got) -- cgit v1.2.3 From 5ae787be3d1d9de3355f8b5662fea857204e693c Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 11:03:51 +0530 Subject: refactor(watcher): remove debug logs from main --- cmd/galleryd/main.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index ed1ee35..455ed47 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -73,14 +73,10 @@ func main() { fswatcher := watcher.New(watcher.Config{ Roots: dirs, OnChange: func() { - log.Println("watcher: reloading gallery") - if err := galleryService.Reload(context.Background()); err != nil { log.Printf("reload gallery: %v", err) return } - - log.Println("watcher: gallery reloaded") }, }) -- cgit v1.2.3