3 Commits
Author SHA1 Message Date
schreifuchs eab7eb3879 chore: better config
/ publish (push) Successful in 3m1s
2026-03-07 20:55:04 +01:00
schreifuchs fca1030e1a feat: limit image request
/ publish (push) Successful in 3m9s
2026-03-07 20:26:53 +01:00
schreifuchs 2f7ff28c9a feat: better loging
/ publish (push) Successful in 3m7s
2026-03-07 19:31:19 +01:00
14 changed files with 2062 additions and 28 deletions
+6
View File
@@ -3,14 +3,20 @@ package main
import (
"context"
"log/slog"
"net/http"
"os"
"os/signal"
_ "net/http/pprof"
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/server"
)
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
ctx := context.Background()
ctx, cancle := context.WithCancel(ctx)
defer cancle()
+8 -1
View File
@@ -60,6 +60,13 @@ func (s *Service) getImage(uid string) (file []byte, mimeType string, err error)
}
func (s *Service) GetImage(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
}
defer s.seph.Release(1)
rawImage, mimeType, err := s.getImage(uid)
if err != nil {
return
@@ -83,7 +90,7 @@ func (s *Service) GetImage(ctx context.Context, uid string, options Options) (im
outBuff := bytes.NewBuffer(make([]byte, 0, 1024))
if options.Quality == 0 {
options.Quality = 80
options.Quality = s.cfg.Quality
}
err = jpeg.Encode(outBuff, decImage, &jpeg.Options{Quality: options.Quality})
+7 -1
View File
@@ -3,7 +3,9 @@ package images
import (
"os"
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
"github.com/valkey-io/valkey-go"
"golang.org/x/sync/semaphore"
)
type Options struct {
@@ -15,12 +17,16 @@ type Options struct {
type Service struct {
fs fileSystem
cache valkey.Client
seph *semaphore.Weighted
cfg *config.ImageConfig
}
func New(fs fileSystem, cache valkey.Client) *Service {
func New(fs fileSystem, cfg *config.ImageConfig, cache valkey.Client) *Service {
return &Service{
fs: fs,
cache: cache,
seph: semaphore.NewWeighted(cfg.Concurency),
cfg: cfg,
}
}
+10
View File
@@ -0,0 +1,10 @@
package config
func Default() Cfg {
return Cfg{
Image: &ImageConfig{
Concurency: 10,
Quality: 80,
},
}
}
+7
View File
@@ -9,6 +9,7 @@ import (
type Cfg struct {
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
Cache *ValkeyConfig `env:", prefix=CACHE_"`
Image *ImageConfig `env:", prefix=IMAGE_"`
}
type WebdavConfig struct {
@@ -30,7 +31,13 @@ type ValkeyConfig struct {
InitAddress []string `env:"ADDRESS"`
}
type ImageConfig struct {
Concurency int64 `env:"CONCURENCY"`
Quality int `env:"QUALITY"`
}
func Read(ctx context.Context) (cfg Cfg, err error) {
cfg = Default()
err = envconfig.ProcessWith(ctx, &envconfig.Config{
Target: &cfg,
+46
View File
@@ -0,0 +1,46 @@
package server
import (
"fmt"
"log/slog"
"net/http"
"time"
)
type captureWriter struct {
http.ResponseWriter
code int
}
func (c *captureWriter) WriteHeader(statusCode int) {
c.code = statusCode
c.ResponseWriter.WriteHeader(statusCode)
}
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
cw := &captureWriter{
ResponseWriter: w,
code: 200,
}
next.ServeHTTP(cw, r)
slog.Info("request",
"method", r.Method,
"path", r.URL.Path,
"duration", time.Since(start),
"status", cw.code,
)
})
}
// cacheMiddleware adds Cache-Control headers to the response.
func cacheMiddleware(maxAge time.Duration) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds()))
w.Header().Set("Vary", "HX-Request,Accept-Language,Accept-Encoding")
next.ServeHTTP(w, r)
})
}
}
+1 -13
View File
@@ -50,7 +50,7 @@ func (s *Server) registerOther() (h http.Handler) {
}
_ = resthome.New(mux, page.New(fs))
_ = restimage.New(mux, images.New(fs, valkeyClient))
_ = restimage.New(mux, images.New(fs, s.cfg.Image, valkeyClient))
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
@@ -61,15 +61,3 @@ func (s *Server) registerOther() (h http.Handler) {
h = cacheMiddleware(time.Minute * 5)(h)
return h
}
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
slog.Info("request",
"method", r.Method,
"path", r.URL.Path,
"duration", time.Since(start),
)
})
}
-12
View File
@@ -1,7 +1,6 @@
package server
import (
"fmt"
"io/fs"
"log/slog"
"net/http"
@@ -38,14 +37,3 @@ func registerStatic(mux *http.ServeMux) {
panic(err)
}
}
// cacheMiddleware adds Cache-Control headers to the response.
func cacheMiddleware(maxAge time.Duration) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds()))
w.Header().Set("Vary", "HX-Request,Accept-Language,Accept-Encoding")
next.ServeHTTP(w, r)
})
}
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 439 KiB

+1976
View File
File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 112 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 439 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 439 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 439 KiB

File diff suppressed because one or more lines are too long