diff options
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/config.go | 10 | ||||
| -rw-r--r-- | internal/api/doc.go | 2 | ||||
| -rw-r--r-- | internal/api/health.go | 29 | ||||
| -rw-r--r-- | internal/api/new.go | 31 | ||||
| -rw-r--r-- | internal/api/photo.go | 12 | ||||
| -rw-r--r-- | internal/api/photos.go | 47 | ||||
| -rw-r--r-- | internal/api/run.go | 24 | ||||
| -rw-r--r-- | internal/api/server.go | 17 |
8 files changed, 172 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 +} |