From d87995c3629cac4671b05073915214f45d0c7112 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 17:49:32 +0530 Subject: refactor(api): centralize resource URLs --- internal/api/photos.go | 4 ++-- internal/api/url.go | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 internal/api/url.go (limited to 'internal/api') diff --git a/internal/api/photos.go b/internal/api/photos.go index 2d492b1..92c5c04 100644 --- a/internal/api/photos.go +++ b/internal/api/photos.go @@ -30,8 +30,8 @@ func (s *Server) handlePhotos( DateTaken: photo.DateTaken, Width: photo.Width, Height: photo.Height, - ThumbnailURL: "/thumb/" + photo.ID, - PhotoURL: "/photo/" + photo.ID, + ThumbnailURL: thumbnailURL(photo.ID), + PhotoURL: photoURL(photo.ID), }) } diff --git a/internal/api/url.go b/internal/api/url.go new file mode 100644 index 0000000..06a944d --- /dev/null +++ b/internal/api/url.go @@ -0,0 +1,9 @@ +package api + +func thumbnailURL(id string) string { + return "/thumb/" + id +} + +func photoURL(id string) string { + return "/photo/" + id +} -- cgit v1.2.3 From f108043068c8818c616cc2e760f53839d4ba9b01 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 17:53:46 +0530 Subject: refactor(api): return structured error responses --- internal/api/error.go | 23 +++++++++++++++++++++++ internal/api/health.go | 6 +----- internal/api/photos.go | 12 +++++++----- 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 internal/api/error.go (limited to 'internal/api') diff --git a/internal/api/error.go b/internal/api/error.go new file mode 100644 index 0000000..b00f326 --- /dev/null +++ b/internal/api/error.go @@ -0,0 +1,23 @@ +package api + +import ( + "encoding/json" + "net/http" +) + +type errorResponse struct { + Error string `json:"error"` +} + +func writeError( + w http.ResponseWriter, + status int, + message string, +) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + + _ = json.NewEncoder(w).Encode(errorResponse{ + Error: message, + }) +} diff --git a/internal/api/health.go b/internal/api/health.go index 61d5185..814fef5 100644 --- a/internal/api/health.go +++ b/internal/api/health.go @@ -20,10 +20,6 @@ func (s *Server) handleHealth( } if err := json.NewEncoder(w).Encode(response); err != nil { - http.Error( - w, - http.StatusText(http.StatusInternalServerError), - http.StatusInternalServerError, - ) + writeError(w, http.StatusInternalServerError, "failed to encode response") } } diff --git a/internal/api/photos.go b/internal/api/photos.go index 92c5c04..ca6b038 100644 --- a/internal/api/photos.go +++ b/internal/api/photos.go @@ -12,12 +12,12 @@ func (s *Server) handlePhotos( ) { photos, err := s.gallery.Gallery(r.Context()) if err != nil { - log.Printf("gallery: %v", err) + log.Printf("build gallery: %v", err) - http.Error( + writeError( w, - http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError, + "failed to build gallery", ) return } @@ -38,10 +38,12 @@ func (s *Server) handlePhotos( w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(response); err != nil { - http.Error( + log.Printf("encode photos response: %v", err) + + writeError( w, - http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError, + "failed to encode response", ) } } -- cgit v1.2.3 From fdf1148db3c37d1be83ea4d52ac370c5da9593a4 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:45:47 +0530 Subject: feat(api): inject thumbnail generator --- internal/api/config.go | 8 ++++++-- internal/api/new.go | 11 +++++++---- internal/api/server.go | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) (limited to 'internal/api') diff --git a/internal/api/config.go b/internal/api/config.go index d82147c..a9a72ae 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -1,10 +1,14 @@ package api -import "github.com/RanXom/galleryd/internal/service" +import ( + "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" +) // Config configures the HTTP server type Config struct { Address string - Gallery service.GalleryService + Gallery service.GalleryService + Thumbnails *thumbnail.Generator } diff --git a/internal/api/new.go b/internal/api/new.go index a7c122b..7066d90 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -11,10 +11,13 @@ func New(config Config) *Server { } srv := &Server{ - config: config, - mux: mux, - http: server, - gallery: config.Gallery, + config: config, + + mux: mux, + http: server, + + gallery: config.Gallery, + thumbnail: config.Thumbnails, } srv.mux.HandleFunc( diff --git a/internal/api/server.go b/internal/api/server.go index c12c5bd..2bec8e2 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" ) // Server exposes the gallery over HTTP @@ -13,5 +14,6 @@ type Server struct { mux *http.ServeMux http *http.Server - gallery service.GalleryService + gallery service.GalleryService + thumbnail *thumbnail.Generator } -- cgit v1.2.3 From 87c5ba8c5f859fd26a038f6a704e856167c78758 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:52:32 +0530 Subject: feat(api): serve cached thumbnails --- internal/api/new.go | 9 ++++++-- internal/api/server.go | 4 ++-- internal/api/thumb.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 internal/api/thumb.go (limited to 'internal/api') diff --git a/internal/api/new.go b/internal/api/new.go index 7066d90..e2399e5 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -16,8 +16,8 @@ func New(config Config) *Server { mux: mux, http: server, - gallery: config.Gallery, - thumbnail: config.Thumbnails, + gallery: config.Gallery, + thumbnails: config.Thumbnails, } srv.mux.HandleFunc( @@ -30,5 +30,10 @@ func New(config Config) *Server { srv.handlePhotos, ) + srv.mux.HandleFunc( + "GET /thumb/{id}", + srv.handleThumbnail, + ) + return srv } diff --git a/internal/api/server.go b/internal/api/server.go index 2bec8e2..ad2d776 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -14,6 +14,6 @@ type Server struct { mux *http.ServeMux http *http.Server - gallery service.GalleryService - thumbnail *thumbnail.Generator + gallery service.GalleryService + thumbnails *thumbnail.Generator } diff --git a/internal/api/thumb.go b/internal/api/thumb.go new file mode 100644 index 0000000..0e18ade --- /dev/null +++ b/internal/api/thumb.go @@ -0,0 +1,58 @@ +package api + +import ( + "errors" + "log" + "net/http" + + "github.com/RanXom/galleryd/internal/service" +) + +func (s *Server) handleThumbnail( + w http.ResponseWriter, + r *http.Request, +) { + id := r.PathValue("id") + + photo, err := s.gallery.Photo(r.Context(), id) + if err != nil { + if errors.Is(err, service.ErrPhotoNotFound) { + writeError( + w, + http.StatusNotFound, + "photo not found", + ) + return + } + + log.Printf("lookup photo: %v", err) + + writeError( + w, + http.StatusInternalServerError, + "failed to lookup photo", + ) + return + } + + thumb, err := s.thumbnails.Generate( + r.Context(), + photo, + ) + if err != nil { + log.Printf("generate thumbnail: %v", err) + + writeError( + w, + http.StatusInternalServerError, + "failed to generate thumbnail", + ) + return + } + + http.ServeFile( + w, + r, + thumb.Path, + ) +} -- cgit v1.2.3 From 887d212288db292dbbc557191ef1507b70ca8507 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:30:02 +0530 Subject: refactor(api): abstract thumbnail generator --- internal/api/api_test.go | 27 +++++++++++++++++++++++++++ internal/api/server.go | 3 +-- internal/api/thumbnail.go | 16 ++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 internal/api/api_test.go create mode 100644 internal/api/thumbnail.go (limited to 'internal/api') diff --git a/internal/api/api_test.go b/internal/api/api_test.go new file mode 100644 index 0000000..3940f6e --- /dev/null +++ b/internal/api/api_test.go @@ -0,0 +1,27 @@ +package api + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestHealth(t *testing.T) { + server := New(Config{ + Address: ":0", + }) + + req := httptest.NewRequest( + http.MethodGet, + "/health", + nil, + ) + + rec := httptest.NewRecorder() + + server.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d", rec.Code) + } +} diff --git a/internal/api/server.go b/internal/api/server.go index ad2d776..b1daf0d 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -4,7 +4,6 @@ import ( "net/http" "github.com/RanXom/galleryd/internal/service" - "github.com/RanXom/galleryd/internal/thumbnail" ) // Server exposes the gallery over HTTP @@ -15,5 +14,5 @@ type Server struct { http *http.Server gallery service.GalleryService - thumbnails *thumbnail.Generator + thumbnails ThumbnailGenerator } diff --git a/internal/api/thumbnail.go b/internal/api/thumbnail.go new file mode 100644 index 0000000..9cea4df --- /dev/null +++ b/internal/api/thumbnail.go @@ -0,0 +1,16 @@ +package api + +import ( + "context" + + "github.com/RanXom/galleryd/internal/gallery" + "github.com/RanXom/galleryd/internal/thumbnail" +) + +// ThumbnailGenerator generates thumbnails. +type ThumbnailGenerator interface { + Generate( + ctx context.Context, + photo gallery.Photo, + ) (thumbnail.Thumbnail, error) +} -- cgit v1.2.3 From b0857d4bf6f899426c5e1a115a60799332636afc Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:35:37 +0530 Subject: test(api): add photos endpoint tests --- internal/api/api_test.go | 107 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 5 deletions(-) (limited to 'internal/api') diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 3940f6e..c54514d 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -1,27 +1,124 @@ package api import ( + "context" + "encoding/json" "net/http" "net/http/httptest" "testing" + "time" + + "github.com/RanXom/galleryd/internal/gallery" + "github.com/RanXom/galleryd/internal/metadata" + "github.com/RanXom/galleryd/internal/service" ) -func TestHealth(t *testing.T) { - server := New(Config{ +type fakeGalleryService struct { + photos []gallery.Photo + err error +} + +func (f fakeGalleryService) Load(ctx context.Context) error { + return nil +} + +func (f fakeGalleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { + return f.photos, f.err +} + +func (f fakeGalleryService) Photo( + ctx context.Context, + id string, +) (gallery.Photo, error) { + for _, photo := range f.photos { + if photo.ID == id { + return photo, nil + } + } + + return gallery.Photo{}, service.ErrPhotoNotFound +} + +func TestPhotos(t *testing.T) { + srv := New(Config{ Address: ":0", + + Gallery: fakeGalleryService{ + photos: []gallery.Photo{ + { + ID: "abc123", + + Metadata: metadata.Metadata{ + Width: 100, + Height: 50, + DateTaken: time.Date( + 2025, + 1, + 1, + 12, + 0, + 0, + 0, + time.UTC, + ), + }, + }, + }, + }, }) req := httptest.NewRequest( http.MethodGet, - "/health", + "/api/photos", nil, ) rec := httptest.NewRecorder() - server.mux.ServeHTTP(rec, req) + srv.mux.ServeHTTP(rec, req) if rec.Code != http.StatusOK { - t.Fatalf("expected 200, got %d", rec.Code) + t.Fatalf( + "expected %d, got %d", + http.StatusOK, + rec.Code, + ) + } + + var response []photoResponse + + if err := json.NewDecoder(rec.Body).Decode(&response); err != nil { + t.Fatalf("decode response: %v", err) + } + + if len(response) != 1 { + t.Fatalf( + "expected 1 photo, got %d", + len(response), + ) + } + + photo := response[0] + + if photo.ID != "abc123" { + t.Fatalf( + "expected id %q, got %q", + "abc123", + photo.ID, + ) + } + + if photo.ThumbnailURL != "/thumb/abc123" { + t.Fatalf( + "unexpected thumbnail url: %q", + photo.ThumbnailURL, + ) + } + + if photo.PhotoURL != "/photo/abc123" { + t.Fatalf( + "unexpected photo url: %q", + photo.PhotoURL, + ) } } -- cgit v1.2.3 From 5f9a07afe0fd42cd9870c864eb30941343bf51e9 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:39:06 +0530 Subject: test(api): add thumbnail endpoint tests --- internal/api/api_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ internal/api/config.go | 3 +-- 2 files changed, 63 insertions(+), 2 deletions(-) (limited to 'internal/api') diff --git a/internal/api/api_test.go b/internal/api/api_test.go index c54514d..c1980fd 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -11,6 +11,7 @@ import ( "github.com/RanXom/galleryd/internal/gallery" "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" ) type fakeGalleryService struct { @@ -18,6 +19,24 @@ type fakeGalleryService struct { err error } +type fakeThumbnailGenerator struct { + path string + err error +} + +func (f fakeThumbnailGenerator) Generate( + ctx context.Context, + photo gallery.Photo, +) (thumbnail.Thumbnail, error) { + if f.err != nil { + return thumbnail.Thumbnail{}, f.err + } + + return thumbnail.Thumbnail{ + Path: f.path, + }, nil +} + func (f fakeGalleryService) Load(ctx context.Context) error { return nil } @@ -122,3 +141,46 @@ func TestPhotos(t *testing.T) { ) } } + +func TestThumbnail(t *testing.T) { + srv := New(Config{ + Address: ":0", + + Gallery: fakeGalleryService{ + photos: []gallery.Photo{ + { + ID: "abc123", + }, + }, + }, + + Thumbnails: fakeThumbnailGenerator{ + path: "../scanner/testdata/gallery/vac/image.webp", + }, + }) + + req := httptest.NewRequest( + http.MethodGet, + "/thumb/abc123", + nil, + ) + + rec := httptest.NewRecorder() + + srv.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf( + "expected %d, got %d", + http.StatusOK, + rec.Code, + ) + } + + if got := rec.Header().Get("Content-Type"); got != "image/webp" { + t.Fatalf( + "expected image/webp, got %q", + got, + ) + } +} diff --git a/internal/api/config.go b/internal/api/config.go index a9a72ae..04057a4 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -2,7 +2,6 @@ package api import ( "github.com/RanXom/galleryd/internal/service" - "github.com/RanXom/galleryd/internal/thumbnail" ) // Config configures the HTTP server @@ -10,5 +9,5 @@ type Config struct { Address string Gallery service.GalleryService - Thumbnails *thumbnail.Generator + Thumbnails ThumbnailGenerator } -- cgit v1.2.3 From 8e8f0bd38f0cc0ee435ae2ed392a206e1ea73a32 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:42:27 +0530 Subject: feat(api): serve original photos --- internal/api/new.go | 5 +++++ internal/api/photo.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) (limited to 'internal/api') diff --git a/internal/api/new.go b/internal/api/new.go index e2399e5..7febfe7 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -35,5 +35,10 @@ func New(config Config) *Server { srv.handleThumbnail, ) + srv.mux.HandleFunc( + "GET /photo/{id}", + srv.handlePhoto, + ) + return srv } diff --git a/internal/api/photo.go b/internal/api/photo.go index 7174491..37f9587 100644 --- a/internal/api/photo.go +++ b/internal/api/photo.go @@ -1,6 +1,13 @@ package api -import "time" +import ( + "errors" + "log" + "net/http" + "time" + + "github.com/RanXom/galleryd/internal/service" +) type photoResponse struct { ID string `json:"id"` @@ -10,3 +17,37 @@ type photoResponse struct { ThumbnailURL string `json:"thumbnail"` PhotoURL string `json:"photo"` } + +func (s *Server) handlePhoto( + w http.ResponseWriter, + r *http.Request, +) { + id := r.PathValue("id") + + photo, err := s.gallery.Photo(r.Context(), id) + if err != nil { + if errors.Is(err, service.ErrPhotoNotFound) { + writeError( + w, + http.StatusNotFound, + "photo not found", + ) + return + } + + log.Printf("lookup photo: %v", err) + + writeError( + w, + http.StatusInternalServerError, + "failed to lookup photo", + ) + return + } + + http.ServeFile( + w, + r, + photo.Path, + ) +} -- cgit v1.2.3