46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package restgallery
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/gallery"
|
|
"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,
|
|
}
|
|
|
|
galleryPath := url.PathEscape(cfg.Path)
|
|
|
|
mux.HandleFunc(fmt.Sprintf("GET /%s/{image_uid}", galleryPath), h.image)
|
|
mux.HandleFunc(fmt.Sprintf("GET /%s", galleryPath), h.gallery)
|
|
return h
|
|
}
|
|
|
|
type galleryService interface {
|
|
GetGallerySet(ctx context.Context) (gallerySet gallery.Set, err error)
|
|
GetPrevAndNext(ctx context.Context, uid string) (previous *gallery.Image, next *gallery.Image, err error)
|
|
}
|
|
type renderer interface {
|
|
RenderPage(ctx context.Context, w io.Writer, r *http.Request, component templ.Component)
|
|
RenderComponent(ctx context.Context, w io.Writer, r *http.Request, component templ.Component)
|
|
}
|