aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/galleryd/main.go50
-rw-r--r--go.mod5
-rw-r--r--go.sum7
-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
16 files changed, 285 insertions, 8 deletions
diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go
new file mode 100644
index 0000000..e23f8a2
--- /dev/null
+++ b/cmd/galleryd/main.go
@@ -0,0 +1,50 @@
+package main
+
+import (
+ "context"
+ "log"
+ "os"
+ "os/signal"
+ "syscall"
+
+ "github.com/RanXom/galleryd/internal/api"
+ "github.com/RanXom/galleryd/internal/gallery"
+ "github.com/RanXom/galleryd/internal/metadata"
+ "github.com/RanXom/galleryd/internal/scanner"
+ "github.com/RanXom/galleryd/internal/service"
+)
+
+func main() {
+ ctx, stop := signal.NotifyContext(
+ context.Background(),
+ os.Interrupt,
+ syscall.SIGTERM,
+ )
+ defer stop()
+
+ scanner := scanner.New(scanner.Config{
+ Roots: []string{
+ "/home/ranxom/Pictures",
+ },
+ })
+
+ reader := metadata.New()
+
+ builder := gallery.New(reader)
+
+ gallerySerice := service.New(
+ scanner,
+ builder,
+ )
+
+ server := api.New(api.Config{
+ Address: ":8082",
+ Gallery: gallerySerice,
+ })
+
+ log.Println("galleryd listening on :8082")
+
+ if err := server.Run(ctx); err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/go.mod b/go.mod
index 4395569..0496f5f 100644
--- a/go.mod
+++ b/go.mod
@@ -3,9 +3,8 @@ module github.com/RanXom/galleryd
go 1.26.5
require (
+ github.com/chai2010/webp v1.4.0
github.com/disintegration/imaging v1.6.2
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
- golang.org/x/image v0.0.0-20211028202545-6944b10bf410
+ golang.org/x/image v0.43.0
)
-
-require github.com/chai2010/webp v1.4.0 // indirect
diff --git a/go.sum b/go.sum
index a7e23bf..ef997d8 100644
--- a/go.sum
+++ b/go.sum
@@ -4,10 +4,7 @@ github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
-golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
-golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
-golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
+golang.org/x/image v0.43.0 h1:FLxcP4ec2350nTfOC8ysKtqYSIFbk/QGjw1ZHNP4tsY=
+golang.org/x/image v0.43.0/go.mod h1:rrpelvGFt+kLPAjPM4HeWPgrl0FtafueU//e5N0qk/Q=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
-golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
-golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
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)
+}