feat(gallery): routes
This commit is contained in:
@@ -17,9 +17,6 @@ RUN pnpm run build:css && pnpm run build:js
|
||||
FROM golang:1.26-alpine AS go-builder
|
||||
WORKDIR /app
|
||||
|
||||
# Install git for potential private modules (though not strictly needed here)
|
||||
RUN apk add --no-cache git
|
||||
|
||||
# Download Go modules
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
@@ -41,9 +38,6 @@ RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/server ./cmd/schr
|
||||
FROM alpine:latest
|
||||
WORKDIR /app
|
||||
|
||||
# Install root certificates and timezone data
|
||||
RUN apk --no-cache add ca-certificates tzdata
|
||||
|
||||
# Copy the compiled binary from the builder stage
|
||||
COPY --from=go-builder /app/server .
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
.PHONY: run
|
||||
run: docker
|
||||
air
|
||||
|
||||
.PHONY: docker
|
||||
docker:
|
||||
docker compose up -d
|
||||
@@ -1,7 +1,6 @@
|
||||
services:
|
||||
valkey:
|
||||
image: valkey/valkey:8
|
||||
restart: always
|
||||
ports:
|
||||
- "6379:6379"
|
||||
command: valkey-server --requirepass "valkey"
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package gallery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"path"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||
)
|
||||
|
||||
func (s *Service) GetImages(ctx context.Context) (imgs []images.Image, err error) {
|
||||
files, err := s.fs.ReadDir(s.cfg.Path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
imagePath := path.Join(s.cfg.Path, file.Name())
|
||||
img, err := s.img.GetImage(imagePath)
|
||||
if err != nil {
|
||||
slog.Warn("can not read image in gallery", "path", imagePath, "err", err)
|
||||
}
|
||||
imgs = append(imgs, img)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package gallery
|
||||
|
||||
import (
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
||||
)
|
||||
|
||||
type getImager interface {
|
||||
GetImage(path string) (img images.Image, err error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
fs filesystem.FS
|
||||
cfg *config.Gallery
|
||||
img getImager
|
||||
}
|
||||
|
||||
func New(fs filesystem.FS, cfg *config.Gallery, img getImager) *Service {
|
||||
return &Service{
|
||||
fs: fs,
|
||||
cfg: cfg,
|
||||
img: img,
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
|
||||
var ErrNotAnImage = errors.New("not an image")
|
||||
|
||||
func (s *Service) getMime(path string) (mimeType string, err error) {
|
||||
func (s *Service) GetMime(path string) (mimeType string, err error) {
|
||||
info, err := s.fs.Stat(path)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("image file info could not be fetched: %w", err)
|
||||
@@ -35,13 +35,12 @@ func (s *Service) getMime(path string) (mimeType string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Service) getImage(uid string) (file []byte, mimeType string, err error) {
|
||||
path, err := PathFromUID(uid)
|
||||
func (s *Service) getImage(path string) (file []byte, mimeType string, err error) {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not get path: %w", err)
|
||||
return
|
||||
}
|
||||
mimeType, err = s.getMime(path)
|
||||
mimeType, err = s.GetMime(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -59,7 +58,19 @@ func (s *Service) getImage(uid string) (file []byte, mimeType string, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Service) GetImage(ctx context.Context, uid string, options Options) (img io.Reader, mimeType string, err error) {
|
||||
func (s *Service) GetImage(path string) (img Image, err error) {
|
||||
rawImage, _, err := s.getImage(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
img.Path = path
|
||||
img.Image, _, err = image.Decode(bytes.NewBuffer(rawImage))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Service) GetScaledImage(ctx context.Context, uid string, options Options) (img io.Reader, mimeType string, err error) {
|
||||
if err = s.seph.Acquire(ctx, 1); err != nil {
|
||||
err = fmt.Errorf("could not Acquire semaphore: %w", err)
|
||||
return
|
||||
@@ -67,7 +78,12 @@ func (s *Service) GetImage(ctx context.Context, uid string, options Options) (im
|
||||
}
|
||||
defer s.seph.Release(1)
|
||||
|
||||
rawImage, mimeType, err := s.getImage(uid)
|
||||
path, err := PathFromUID(uid)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
rawImage, mimeType, err := s.getImage(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
func Setup(ctx context.Context, ctrl *gomock.Controller) (*images.Service, *mock.Client) {
|
||||
valkey := mock.NewClient(ctrl)
|
||||
return images.New(testdata.FS(), &config.ImageConfig{Concurency: 1, Quality: 80}, valkey), valkey
|
||||
return images.New(testdata.FS(), &config.Image{Concurency: 1, Quality: 80}, valkey), valkey
|
||||
}
|
||||
|
||||
func BenchmarkService_GetImage(b *testing.B) {
|
||||
@@ -29,7 +29,7 @@ func BenchmarkService_GetImage(b *testing.B) {
|
||||
for b.Loop() {
|
||||
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.ErrorResult(errors.New("adf")))
|
||||
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.Result(valkey.ValkeyMessage{}))
|
||||
_, _, err = srv.GetImage(b.Context(), uid, images.Options{Width: 500, Quality: 80})
|
||||
_, _, err = srv.GetScaledImage(b.Context(), uid, images.Options{Width: 500, Quality: 80})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package images
|
||||
|
||||
import "image"
|
||||
|
||||
type Image struct {
|
||||
image.Image
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func (i Image) UID() (string, error) {
|
||||
return UIDFromPath(i.Path)
|
||||
}
|
||||
@@ -18,10 +18,10 @@ type Service struct {
|
||||
fs fileSystem
|
||||
cache valkey.Client
|
||||
seph *semaphore.Weighted
|
||||
cfg *config.ImageConfig
|
||||
cfg *config.Image
|
||||
}
|
||||
|
||||
func New(fs fileSystem, cfg *config.ImageConfig, cache valkey.Client) *Service {
|
||||
func New(fs fileSystem, cfg *config.Image, cache valkey.Client) *Service {
|
||||
return &Service{
|
||||
fs: fs,
|
||||
cache: cache,
|
||||
|
||||
@@ -56,10 +56,15 @@ func (s *Service) getPageHeaders(ctx context.Context, uid string) (page PageHead
|
||||
}
|
||||
}
|
||||
|
||||
if contentMD != nil {
|
||||
|
||||
page.md, err = s.fs.Read(contentMD.Path())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
page.md = []byte("# ...")
|
||||
}
|
||||
|
||||
title := titleRGX.FindStringSubmatch(string(page.md))
|
||||
if len(title) < 2 {
|
||||
|
||||
@@ -25,6 +25,7 @@ func SupportedLanguages(ctx context.Context) []string {
|
||||
s, ok := ctx.Value(serviceKey).(*Service)
|
||||
if !ok {
|
||||
slog.Error("could not extract translation service from context for getting supported languages")
|
||||
return []string{}
|
||||
}
|
||||
return s.SupportedLanguages()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -4,14 +4,20 @@ import "time"
|
||||
|
||||
func Default() Cfg {
|
||||
return Cfg{
|
||||
Image: &ImageConfig{
|
||||
Image: &Image{
|
||||
Concurency: 10,
|
||||
Quality: 80,
|
||||
CacheLifetime: time.Hour * 24 * 31,
|
||||
},
|
||||
Tracking: &UmamiConfig{
|
||||
Tracking: &Umami{
|
||||
ScriptSrc: "https://umami.schreifuchs.ch/script.js",
|
||||
WebsiteID: "54d8a379-77d5-4c20-b46d-5c9a6c9e39bd",
|
||||
},
|
||||
Gallery: &Gallery{
|
||||
Path: "gallery",
|
||||
},
|
||||
Cache: &Valkey{
|
||||
RefreshTime: time.Minute * 10,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,10 @@ import (
|
||||
|
||||
type Cfg struct {
|
||||
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
|
||||
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
||||
Image *ImageConfig `env:", prefix=IMAGE_"`
|
||||
Tracking *UmamiConfig `env:", prefix=TRACKING_"`
|
||||
Cache *Valkey `env:", prefix=CACHE_"`
|
||||
Image *Image `env:", prefix=IMAGE_"`
|
||||
Tracking *Umami `env:", prefix=TRACKING_"`
|
||||
Gallery *Gallery `env:", prefix=GALLERY_"`
|
||||
|
||||
LogLevel slog.Level `env:"LOG_LEVEL, default=DEBUG"`
|
||||
}
|
||||
@@ -23,11 +24,13 @@ type WebdavConfig struct {
|
||||
Password string `env:"PASSWORD"`
|
||||
}
|
||||
|
||||
type ValkeyConfig struct {
|
||||
type Valkey struct {
|
||||
ClientName string `env:"CLIENTNAME"`
|
||||
Username string `env:"USERNAME"`
|
||||
Password string `env:"PASSWORD"`
|
||||
|
||||
RefreshTime time.Duration `env:"REFRESH_TIME"`
|
||||
|
||||
// InitAddress point to valkey nodes.
|
||||
// Valkey will connect to them one by one and issue a CLUSTER SLOT command to initialize the cluster client until success.
|
||||
// If len(InitAddress) == 1 and the address is not running in cluster mode, valkey will fall back to the single client mode.
|
||||
@@ -36,13 +39,17 @@ type ValkeyConfig struct {
|
||||
InitAddress []string `env:"ADDRESS"`
|
||||
}
|
||||
|
||||
type ImageConfig struct {
|
||||
type Image struct {
|
||||
Concurency int64 `env:"CONCURENCY"`
|
||||
Quality int `env:"QUALITY"`
|
||||
CacheLifetime time.Duration `env:"CACHE_LIFETIME"`
|
||||
}
|
||||
|
||||
type UmamiConfig struct {
|
||||
type Gallery struct {
|
||||
Path string `env:"PATH"`
|
||||
}
|
||||
|
||||
type Umami struct {
|
||||
ScriptSrc string `env:"SCRIPT_SRC"`
|
||||
WebsiteID string `env:"WEBSITE_ID"`
|
||||
}
|
||||
@@ -64,5 +71,7 @@ func Read(ctx context.Context) (cfg Cfg, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("rft", "rft", cfg.Cache.RefreshTime)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ func NewCachedClient(impl FS, client valkey.Client, refreshtime time.Duration) *
|
||||
}
|
||||
|
||||
func (c *CachedClient) revalidate(ctx context.Context) {
|
||||
slog.Info("starting revalidation")
|
||||
go c.revalidateReadDir(ctx)
|
||||
go c.revalidateRead(ctx)
|
||||
go c.revalidateStat(ctx)
|
||||
|
||||
@@ -5,10 +5,12 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/gallery"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/page"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/rest"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restgallery"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/resthome"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restimage"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
||||
@@ -59,11 +61,12 @@ func (s *Server) dynamicRoutes() (h http.Handler, err error) {
|
||||
}
|
||||
if err != nil {
|
||||
} else {
|
||||
fs = filesystem.NewCachedClient(webdavClient, valkeyClient, 5*time.Hour)
|
||||
fs = filesystem.NewCachedClient(webdavClient, valkeyClient, s.cfg.Cache.RefreshTime)
|
||||
}
|
||||
imageSrv := images.New(fs, s.cfg.Image, valkeyClient)
|
||||
|
||||
mux.Handle("/images/", rest.Use(
|
||||
restimage.New(images.New(fs, s.cfg.Image, valkeyClient)),
|
||||
restimage.New(imageSrv),
|
||||
middleware.Cache(time.Hour*24*5),
|
||||
))
|
||||
|
||||
@@ -71,7 +74,14 @@ func (s *Server) dynamicRoutes() (h http.Handler, err error) {
|
||||
renderer := layouts.New(s.cfg)
|
||||
|
||||
mux.Handle("/", rest.Use(
|
||||
resthome.New(renderer, page.New(fs)),
|
||||
func() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/gallery", restgallery.New(renderer, gallery.New(fs, s.cfg.Gallery, imageSrv), s.cfg.Gallery))
|
||||
mux.Handle("/", resthome.New(renderer, page.New(fs)))
|
||||
|
||||
return mux
|
||||
}(),
|
||||
translator.Middleware,
|
||||
middleware.Cache(time.Minute),
|
||||
))
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package pages
|
||||
|
||||
import "git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||
|
||||
templ Gallery(images []images.Image) {
|
||||
<div class="grid gap-5">
|
||||
for _, img := range images {
|
||||
if uid, err := img.UID(); err == nil {
|
||||
<img src={ "/images/" + uid }/>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user