feat(gallery): routes
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package restgallery
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/pages"
|
||||
)
|
||||
|
||||
func (h *Handler) gallery(w http.ResponseWriter, r *http.Request) {
|
||||
imgs, err := h.srv.GetImages(r.Context())
|
||||
if err != nil {
|
||||
slog.Error("could not get images", "err", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
h.r.Render(r.Context(), w, r, pages.Gallery(imgs))
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package restgallery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
http.Handler
|
||||
srv galleryService
|
||||
r renderer
|
||||
cfg *config.Gallery
|
||||
}
|
||||
|
||||
func New(renderer renderer, srv galleryService, cfg *config.Gallery) *Handler {
|
||||
mux := http.NewServeMux()
|
||||
h := &Handler{
|
||||
Handler: mux,
|
||||
srv: srv,
|
||||
r: renderer,
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
mux.HandleFunc(fmt.Sprintf("GET /%s", url.PathEscape(cfg.Path)), h.gallery)
|
||||
return h
|
||||
}
|
||||
|
||||
type galleryService interface {
|
||||
GetImages(ctx context.Context) (imgs []images.Image, err error)
|
||||
}
|
||||
type renderer interface {
|
||||
Render(ctx context.Context, w io.Writer, r *http.Request, component templ.Component)
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package resthome
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
@@ -24,6 +26,10 @@ func (h *Handler) page(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
page, err := h.src.GetPage(r.Context(), uid)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
w.WriteHeader(http.StatusRequestTimeout)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ func (h *Handler) getImage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
img, mime, err := h.img.GetImage(r.Context(), uid, options)
|
||||
img, mime, err := h.img.GetScaledImage(r.Context(), uid, options)
|
||||
if err != nil {
|
||||
slog.Error("error wile serving image", "err", err, "uid", uid)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@@ -25,5 +25,5 @@ func New(srv imageService) *Handler {
|
||||
}
|
||||
|
||||
type imageService interface {
|
||||
GetImage(ctx context.Context, uid string, options images.Options) (img io.Reader, mimeType string, err error)
|
||||
GetScaledImage(ctx context.Context, uid string, options images.Options) (img io.Reader, mimeType string, err error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user