diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-14 11:37:22 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-14 11:37:22 +0530 |
| commit | ed87aebc0296379280fd65d618127b4eeb848a98 (patch) | |
| tree | b407e464735cfabfe49df727149c9569c2ee0bea | |
| parent | 48fae70cf1a6b99966cd65c0229a936382984bc6 (diff) | |
| download | galleryd-ed87aebc0296379280fd65d618127b4eeb848a98.tar.xz galleryd-ed87aebc0296379280fd65d618127b4eeb848a98.zip | |
feat(api): add manual gallery reload endpoint
| -rw-r--r-- | internal/api/api_test.go | 46 | ||||
| -rw-r--r-- | internal/api/new.go | 5 | ||||
| -rw-r--r-- | internal/api/reload.go | 25 |
3 files changed, 72 insertions, 4 deletions
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) +} |