diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-07-13 19:45:40 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-13 19:45:40 +0530 |
| commit | 5d4eaf85434658f6fed621016e0bc2bae59e88d1 (patch) | |
| tree | 93ec34c177b6a48d90d5024313c3a10f5b7c5d2b | |
| parent | f41b0b92046e1bee512b3e4143ed9dc9a9ba9713 (diff) | |
| parent | 8e8f0bd38f0cc0ee435ae2ed392a206e1ea73a32 (diff) | |
| download | galleryd-5d4eaf85434658f6fed621016e0bc2bae59e88d1.tar.xz galleryd-5d4eaf85434658f6fed621016e0bc2bae59e88d1.zip | |
Merge pull request #8 from RanXom/feat/api
Feat/api: expose gallery HTTP API
| -rw-r--r-- | cmd/galleryd/main.go | 19 | ||||
| -rw-r--r-- | internal/api/api_test.go | 186 | ||||
| -rw-r--r-- | internal/api/config.go | 7 | ||||
| -rw-r--r-- | internal/api/error.go | 23 | ||||
| -rw-r--r-- | internal/api/health.go | 6 | ||||
| -rw-r--r-- | internal/api/new.go | 21 | ||||
| -rw-r--r-- | internal/api/photo.go | 43 | ||||
| -rw-r--r-- | internal/api/photos.go | 16 | ||||
| -rw-r--r-- | internal/api/server.go | 3 | ||||
| -rw-r--r-- | internal/api/thumb.go | 58 | ||||
| -rw-r--r-- | internal/api/thumbnail.go | 16 | ||||
| -rw-r--r-- | internal/api/url.go | 9 | ||||
| -rw-r--r-- | internal/service/gallery.go | 2 | ||||
| -rw-r--r-- | internal/service/gallery_service.go | 14 | ||||
| -rw-r--r-- | internal/service/index.go | 9 | ||||
| -rw-r--r-- | internal/service/load.go | 33 | ||||
| -rw-r--r-- | internal/service/photo.go | 29 | ||||
| -rw-r--r-- | internal/service/service.go | 7 |
18 files changed, 473 insertions, 28 deletions
diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 3b69474..41b62cf 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -5,6 +5,7 @@ import ( "log" "os" "os/signal" + "path/filepath" "syscall" "github.com/RanXom/galleryd/internal/api" @@ -12,6 +13,7 @@ import ( "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/scanner" "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" ) func main() { @@ -24,7 +26,7 @@ func main() { scanner := scanner.New(scanner.Config{ Roots: []string{ - ".", + filepath.Join(os.Getenv("HOME"), "Pictures"), }, }) @@ -32,14 +34,23 @@ func main() { builder := gallery.New(reader) - gallerySerice := service.New( + galleryService := service.New( scanner, builder, ) + if err := galleryService.Load(ctx); err != nil { + log.Fatal(err) + } + + thumbnailGenerator, err := thumbnail.New(".cache/galleryd") + if err != nil { + log.Fatal(err) + } server := api.New(api.Config{ - Address: ":8082", - Gallery: gallerySerice, + Address: ":8082", + Gallery: galleryService, + Thumbnails: thumbnailGenerator, }) log.Println("galleryd listening on :8082") diff --git a/internal/api/api_test.go b/internal/api/api_test.go new file mode 100644 index 0000000..c1980fd --- /dev/null +++ b/internal/api/api_test.go @@ -0,0 +1,186 @@ +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" + "github.com/RanXom/galleryd/internal/thumbnail" +) + +type fakeGalleryService struct { + photos []gallery.Photo + 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 +} + +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, + "/api/photos", + nil, + ) + + rec := httptest.NewRecorder() + + srv.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + 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, + ) + } +} + +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 d82147c..04057a4 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -1,10 +1,13 @@ package api -import "github.com/RanXom/galleryd/internal/service" +import ( + "github.com/RanXom/galleryd/internal/service" +) // Config configures the HTTP server type Config struct { Address string - Gallery service.GalleryService + Gallery service.GalleryService + Thumbnails ThumbnailGenerator } 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/new.go b/internal/api/new.go index a7c122b..7febfe7 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, + thumbnails: config.Thumbnails, } srv.mux.HandleFunc( @@ -27,5 +30,15 @@ func New(config Config) *Server { srv.handlePhotos, ) + srv.mux.HandleFunc( + "GET /thumb/{id}", + 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, + ) +} diff --git a/internal/api/photos.go b/internal/api/photos.go index 2d492b1..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 } @@ -30,18 +30,20 @@ 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), }) } 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", ) } } diff --git a/internal/api/server.go b/internal/api/server.go index c12c5bd..b1daf0d 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -13,5 +13,6 @@ type Server struct { mux *http.ServeMux http *http.Server - gallery service.GalleryService + gallery service.GalleryService + thumbnails ThumbnailGenerator } 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, + ) +} 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) +} 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 +} diff --git a/internal/service/gallery.go b/internal/service/gallery.go index f78e5df..30abefe 100644 --- a/internal/service/gallery.go +++ b/internal/service/gallery.go @@ -9,4 +9,6 @@ import ( type galleryService struct { scanner *scanner.Scanner builder *gallery.Builder + + index galleryIndex } diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go index 90e3c09..e500bec 100644 --- a/internal/service/gallery_service.go +++ b/internal/service/gallery_service.go @@ -2,16 +2,22 @@ package service import ( "context" + "errors" "github.com/RanXom/galleryd/internal/gallery" ) // Gallery scans the configured roots and builds the gallery. func (s *galleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { - files, err := s.scanner.Scan(ctx) - if err != nil { - return nil, err + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: } - return s.builder.Build(ctx, files) + if s.index.byID == nil { + return nil, errors.New("gallery not loaded") + } + + return s.index.photos, nil } diff --git a/internal/service/index.go b/internal/service/index.go new file mode 100644 index 0000000..dd39169 --- /dev/null +++ b/internal/service/index.go @@ -0,0 +1,9 @@ +package service + +import "github.com/RanXom/galleryd/internal/gallery" + +// galleryIndex stores an in-memory snapshot of the gallery. +type galleryIndex struct { + photos []gallery.Photo + byID map[string]gallery.Photo +} diff --git a/internal/service/load.go b/internal/service/load.go new file mode 100644 index 0000000..09bbc20 --- /dev/null +++ b/internal/service/load.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) 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/photo.go b/internal/service/photo.go new file mode 100644 index 0000000..018ba6b --- /dev/null +++ b/internal/service/photo.go @@ -0,0 +1,29 @@ +package service + +import ( + "context" + "errors" + + "github.com/RanXom/galleryd/internal/gallery" +) + +var ErrPhotoNotFound = errors.New("photo not found") + +// Photo returns a single photo by its ID. +func (s *galleryService) Photo( + ctx context.Context, + id string, +) (gallery.Photo, error) { + select { + case <-ctx.Done(): + return gallery.Photo{}, ctx.Err() + default: + } + + photo, ok := s.index.byID[id] + if !ok { + return gallery.Photo{}, ErrPhotoNotFound + } + + return photo, nil +} diff --git a/internal/service/service.go b/internal/service/service.go index 6183e26..68a764e 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -8,5 +8,12 @@ import ( // GalleryService provides gallery operations. type GalleryService interface { + // Load scans the configured roots and builds the in-memory gallery. + Load(ctx context.Context) error + + // Gallery returns the currently loaded gallery. Gallery(ctx context.Context) ([]gallery.Photo, error) + + // Photo returns a single photo by its ID. + Photo(ctx context.Context, id string) (gallery.Photo, error) } |