From 5d6347e2faa5419bf5469858a2335aa9e2746c9a Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 14:55:39 +0530 Subject: feat(api): initialize HTTP API package --- internal/api/config.go | 6 ++++++ internal/api/doc.go | 2 ++ internal/api/new.go | 5 +++++ internal/api/server.go | 4 ++++ 4 files changed, 17 insertions(+) create mode 100644 internal/api/config.go create mode 100644 internal/api/doc.go create mode 100644 internal/api/new.go create mode 100644 internal/api/server.go (limited to 'internal/api') diff --git a/internal/api/config.go b/internal/api/config.go new file mode 100644 index 0000000..953421d --- /dev/null +++ b/internal/api/config.go @@ -0,0 +1,6 @@ +package api + +// Config configures the HTTP server +type Config struct { + Address string +} diff --git a/internal/api/doc.go b/internal/api/doc.go new file mode 100644 index 0000000..004c051 --- /dev/null +++ b/internal/api/doc.go @@ -0,0 +1,2 @@ +// Package api exposes galleryd over HTTP. +package api diff --git a/internal/api/new.go b/internal/api/new.go new file mode 100644 index 0000000..3e949d3 --- /dev/null +++ b/internal/api/new.go @@ -0,0 +1,5 @@ +package api + +func New(config Config) *Server { + return &Server{} +} diff --git a/internal/api/server.go b/internal/api/server.go new file mode 100644 index 0000000..0151378 --- /dev/null +++ b/internal/api/server.go @@ -0,0 +1,4 @@ +package api + +// Server exposes the gallery over HTTP +type Server struct{} -- cgit v1.2.3 From cee3fcb2be34f86bdd6a3e19cb57a2e3c1cf34b4 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 15:02:39 +0530 Subject: feat(api): implement HTTP server --- internal/api/new.go | 14 +++++++++++++- internal/api/run.go | 24 ++++++++++++++++++++++++ internal/api/server.go | 8 +++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 internal/api/run.go (limited to 'internal/api') diff --git a/internal/api/new.go b/internal/api/new.go index 3e949d3..d5f2b12 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -1,5 +1,17 @@ package api +import "net/http" + func New(config Config) *Server { - return &Server{} + mux := http.NewServeMux() + + server := &http.Server{ + Addr: config.Address, + Handler: mux, + } + + return &Server{ + config: config, + http: server, + } } diff --git a/internal/api/run.go b/internal/api/run.go new file mode 100644 index 0000000..0bd8a9b --- /dev/null +++ b/internal/api/run.go @@ -0,0 +1,24 @@ +package api + +import ( + "context" + "errors" + "net/http" +) + +// Run starts the HTTP server and blocks until it exits. +func (s *Server) Run(ctx context.Context) error { + go func() { + <-ctx.Done() + + _ = s.http.Shutdown(context.Background()) + }() + + err := s.http.ListenAndServe() + + if errors.Is(err, http.ErrServerClosed) { + return nil + } + + return err +} diff --git a/internal/api/server.go b/internal/api/server.go index 0151378..512ced3 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -1,4 +1,10 @@ package api +import "net/http" + // Server exposes the gallery over HTTP -type Server struct{} +type Server struct { + config Config + + http *http.Server +} -- cgit v1.2.3 From cbad596b7825550b2e9cb1d6322f50acd7f1c9a7 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 15:05:20 +0530 Subject: refactor(api): store HTTP multiplexer --- internal/api/new.go | 1 + internal/api/server.go | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'internal/api') diff --git a/internal/api/new.go b/internal/api/new.go index d5f2b12..a9a08eb 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -12,6 +12,7 @@ func New(config Config) *Server { return &Server{ config: config, + mux: mux, http: server, } } diff --git a/internal/api/server.go b/internal/api/server.go index 512ced3..c455683 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -1,10 +1,13 @@ package api -import "net/http" +import ( + "net/http" +) // Server exposes the gallery over HTTP type Server struct { config Config + mux *http.ServeMux http *http.Server } -- cgit v1.2.3 From 735e7310c904abf18293456d37044dd9c5e796b0 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 15:12:51 +0530 Subject: feat(api): create a /health API endpoint --- internal/api/health.go | 29 +++++++++++++++++++++++++++++ internal/api/new.go | 9 ++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 internal/api/health.go (limited to 'internal/api') diff --git a/internal/api/health.go b/internal/api/health.go new file mode 100644 index 0000000..61d5185 --- /dev/null +++ b/internal/api/health.go @@ -0,0 +1,29 @@ +package api + +import ( + "encoding/json" + "net/http" +) + +type HealthResponse struct { + Status string `json:"status"` +} + +func (s *Server) handleHealth( + w http.ResponseWriter, + r *http.Request, +) { + w.Header().Set("Content-Type", "application/json") + + response := HealthResponse{ + Status: "ok", + } + + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error( + w, + http.StatusText(http.StatusInternalServerError), + http.StatusInternalServerError, + ) + } +} diff --git a/internal/api/new.go b/internal/api/new.go index a9a08eb..d225b02 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -10,9 +10,16 @@ func New(config Config) *Server { Handler: mux, } - return &Server{ + srv := &Server{ config: config, mux: mux, http: server, } + + srv.mux.HandleFunc( + "GET /health", + srv.handleHealth, + ) + + return srv } -- cgit v1.2.3 From 0bf573c82118aaa5b60ab381dee26f43b10004b3 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 15:43:59 +0530 Subject: feat(api): wire application services --- internal/api/config.go | 4 ++++ internal/api/new.go | 7 ++++--- internal/api/server.go | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) (limited to 'internal/api') diff --git a/internal/api/config.go b/internal/api/config.go index 953421d..d82147c 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -1,6 +1,10 @@ package api +import "github.com/RanXom/galleryd/internal/service" + // Config configures the HTTP server type Config struct { Address string + + Gallery service.GalleryService } diff --git a/internal/api/new.go b/internal/api/new.go index d225b02..4941496 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -11,9 +11,10 @@ func New(config Config) *Server { } srv := &Server{ - config: config, - mux: mux, - http: server, + config: config, + mux: mux, + http: server, + gallery: config.Gallery, } srv.mux.HandleFunc( diff --git a/internal/api/server.go b/internal/api/server.go index c455683..c12c5bd 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -2,6 +2,8 @@ package api import ( "net/http" + + "github.com/RanXom/galleryd/internal/service" ) // Server exposes the gallery over HTTP @@ -10,4 +12,6 @@ type Server struct { mux *http.ServeMux http *http.Server + + gallery service.GalleryService } -- cgit v1.2.3 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 --- internal/api/new.go | 5 +++++ internal/api/photo.go | 12 ++++++++++++ internal/api/photos.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 internal/api/photo.go create mode 100644 internal/api/photos.go (limited to 'internal/api') 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