aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorSaiyed Shizain <saiyedshizain@gmail.com>2026-07-13 17:39:59 +0530
committerGitHub <noreply@github.com>2026-07-13 17:39:59 +0530
commitae29fa5ee155bb4828ea6eb8b164a30f7ec9fae8 (patch)
tree4fbbce3d746c22a4cbb42ec53a9abd3e698bc67b /internal
parentcc639337ed5ccb4634b2a338f764cf9c73b02e4f (diff)
parent56bb199787f8fc80b92779426644250f2709fd36 (diff)
downloadgalleryd-ae29fa5ee155bb4828ea6eb8b164a30f7ec9fae8.tar.xz
galleryd-ae29fa5ee155bb4828ea6eb8b164a30f7ec9fae8.zip
Merge pull request #6 from RanXom/feat/api
Feat/api: implement HTTP server and expose required endpoints
Diffstat (limited to 'internal')
-rw-r--r--internal/api/config.go10
-rw-r--r--internal/api/doc.go2
-rw-r--r--internal/api/health.go29
-rw-r--r--internal/api/new.go31
-rw-r--r--internal/api/photo.go12
-rw-r--r--internal/api/photos.go47
-rw-r--r--internal/api/run.go24
-rw-r--r--internal/api/server.go17
-rw-r--r--internal/service/doc.go2
-rw-r--r--internal/service/gallery.go12
-rw-r--r--internal/service/gallery_service.go17
-rw-r--r--internal/service/new.go16
-rw-r--r--internal/service/service.go12
13 files changed, 231 insertions, 0 deletions
diff --git a/internal/api/config.go b/internal/api/config.go
new file mode 100644
index 0000000..d82147c
--- /dev/null
+++ b/internal/api/config.go
@@ -0,0 +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/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/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
new file mode 100644
index 0000000..a7c122b
--- /dev/null
+++ b/internal/api/new.go
@@ -0,0 +1,31 @@
+package api
+
+import "net/http"
+
+func New(config Config) *Server {
+ mux := http.NewServeMux()
+
+ server := &http.Server{
+ Addr: config.Address,
+ Handler: mux,
+ }
+
+ srv := &Server{
+ config: config,
+ mux: mux,
+ http: server,
+ gallery: config.Gallery,
+ }
+
+ srv.mux.HandleFunc(
+ "GET /health",
+ 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,
+ )
+ }
+}
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
new file mode 100644
index 0000000..c12c5bd
--- /dev/null
+++ b/internal/api/server.go
@@ -0,0 +1,17 @@
+package api
+
+import (
+ "net/http"
+
+ "github.com/RanXom/galleryd/internal/service"
+)
+
+// Server exposes the gallery over HTTP
+type Server struct {
+ config Config
+
+ mux *http.ServeMux
+ http *http.Server
+
+ gallery service.GalleryService
+}
diff --git a/internal/service/doc.go b/internal/service/doc.go
new file mode 100644
index 0000000..efe624d
--- /dev/null
+++ b/internal/service/doc.go
@@ -0,0 +1,2 @@
+// Package service contains application-level orchestration.
+package service
diff --git a/internal/service/gallery.go b/internal/service/gallery.go
new file mode 100644
index 0000000..f78e5df
--- /dev/null
+++ b/internal/service/gallery.go
@@ -0,0 +1,12 @@
+package service
+
+import (
+ "github.com/RanXom/galleryd/internal/gallery"
+ "github.com/RanXom/galleryd/internal/scanner"
+)
+
+// GalleryService builds gallery photos
+type galleryService struct {
+ scanner *scanner.Scanner
+ builder *gallery.Builder
+}
diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go
new file mode 100644
index 0000000..90e3c09
--- /dev/null
+++ b/internal/service/gallery_service.go
@@ -0,0 +1,17 @@
+package service
+
+import (
+ "context"
+
+ "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
+ }
+
+ return s.builder.Build(ctx, files)
+}
diff --git a/internal/service/new.go b/internal/service/new.go
new file mode 100644
index 0000000..73d0af6
--- /dev/null
+++ b/internal/service/new.go
@@ -0,0 +1,16 @@
+package service
+
+import (
+ "github.com/RanXom/galleryd/internal/gallery"
+ "github.com/RanXom/galleryd/internal/scanner"
+)
+
+func New(
+ scanner *scanner.Scanner,
+ builder *gallery.Builder,
+) GalleryService {
+ return &galleryService{
+ scanner: scanner,
+ builder: builder,
+ }
+}
diff --git a/internal/service/service.go b/internal/service/service.go
new file mode 100644
index 0000000..6183e26
--- /dev/null
+++ b/internal/service/service.go
@@ -0,0 +1,12 @@
+package service
+
+import (
+ "context"
+
+ "github.com/RanXom/galleryd/internal/gallery"
+)
+
+// GalleryService provides gallery operations.
+type GalleryService interface {
+ Gallery(ctx context.Context) ([]gallery.Photo, error)
+}