From 56bb199787f8fc80b92779426644250f2709fd36 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 15:53:08 +0530 Subject: feat(api): expose gallery photos endpoint --- cmd/galleryd/main.go | 2 +- internal/api/new.go | 5 +++++ internal/api/photo.go | 12 ++++++++++++ internal/api/photos.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 internal/api/photo.go create mode 100644 internal/api/photos.go diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 3b69474..e23f8a2 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -24,7 +24,7 @@ func main() { scanner := scanner.New(scanner.Config{ Roots: []string{ - ".", + "/home/ranxom/Pictures", }, }) diff --git a/internal/api/new.go b/internal/api/new.go index 4941496..a7c122b 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -22,5 +22,10 @@ func New(config Config) *Server { srv.handleHealth, ) + srv.mux.HandleFunc( + "GET /api/photos", + srv.handlePhotos, + ) + return srv } diff --git a/internal/api/photo.go b/internal/api/photo.go new file mode 100644 index 0000000..7174491 --- /dev/null +++ b/internal/api/photo.go @@ -0,0 +1,12 @@ +package api + +import "time" + +type photoResponse struct { + ID string `json:"id"` + DateTaken time.Time `json:"dateTaken"` + Width int `json:"width"` + Height int `json:"height"` + ThumbnailURL string `json:"thumbnail"` + PhotoURL string `json:"photo"` +} diff --git a/internal/api/photos.go b/internal/api/photos.go new file mode 100644 index 0000000..2d492b1 --- /dev/null +++ b/internal/api/photos.go @@ -0,0 +1,47 @@ +package api + +import ( + "encoding/json" + "log" + "net/http" +) + +func (s *Server) handlePhotos( + w http.ResponseWriter, + r *http.Request, +) { + photos, err := s.gallery.Gallery(r.Context()) + if err != nil { + log.Printf("gallery: %v", err) + + http.Error( + w, + http.StatusText(http.StatusInternalServerError), + http.StatusInternalServerError, + ) + return + } + + response := make([]photoResponse, 0, len(photos)) + + for _, photo := range photos { + response = append(response, photoResponse{ + ID: photo.ID, + DateTaken: photo.DateTaken, + Width: photo.Width, + Height: photo.Height, + ThumbnailURL: "/thumb/" + photo.ID, + PhotoURL: "/photo/" + photo.ID, + }) + } + + w.Header().Set("Content-Type", "application/json") + + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error( + w, + http.StatusText(http.StatusInternalServerError), + http.StatusInternalServerError, + ) + } +} -- cgit v1.2.3