Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eab7eb3879 | ||
|
|
fca1030e1a | ||
|
|
2f7ff28c9a | ||
|
|
82e601fd7f |
@@ -3,14 +3,20 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
|
_ "net/http/pprof"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/server"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go func() {
|
||||||
|
http.ListenAndServe("localhost:6060", nil)
|
||||||
|
}()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx, cancle := context.WithCancel(ctx)
|
ctx, cancle := context.WithCancel(ctx)
|
||||||
defer cancle()
|
defer cancle()
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ require (
|
|||||||
github.com/a-h/templ v0.3.977
|
github.com/a-h/templ v0.3.977
|
||||||
github.com/alicebob/miniredis/v2 v2.36.1
|
github.com/alicebob/miniredis/v2 v2.36.1
|
||||||
github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab
|
github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab
|
||||||
|
github.com/sethvargo/go-envconfig v1.3.0
|
||||||
github.com/studio-b12/gowebdav v0.12.0
|
github.com/studio-b12/gowebdav v0.12.0
|
||||||
github.com/valkey-io/valkey-go v1.0.72
|
github.com/valkey-io/valkey-go v1.0.72
|
||||||
golang.org/x/image v0.36.0
|
golang.org/x/image v0.36.0
|
||||||
@@ -20,11 +21,9 @@ require (
|
|||||||
github.com/cli/browser v1.3.0 // indirect
|
github.com/cli/browser v1.3.0 // indirect
|
||||||
github.com/fatih/color v1.16.0 // indirect
|
github.com/fatih/color v1.16.0 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||||
github.com/joho/godotenv v1.5.1 // indirect
|
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/natefinch/atomic v1.0.1 // indirect
|
github.com/natefinch/atomic v1.0.1 // indirect
|
||||||
github.com/sethvargo/go-envconfig v1.3.0 // indirect
|
|
||||||
github.com/yuin/gopher-lua v1.1.1 // indirect
|
github.com/yuin/gopher-lua v1.1.1 // indirect
|
||||||
golang.org/x/mod v0.26.0 // indirect
|
golang.org/x/mod v0.26.0 // indirect
|
||||||
golang.org/x/net v0.48.0 // indirect
|
golang.org/x/net v0.48.0 // indirect
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab h1:VYNivV7P8IR
|
|||||||
github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ import (
|
|||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"image"
|
"image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
_ "image/jpeg"
|
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
_ "golang.org/x/image/webp"
|
||||||
|
|
||||||
"github.com/valkey-io/valkey-go"
|
"github.com/valkey-io/valkey-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,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) {
|
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)
|
rawImage, mimeType, err := s.getImage(uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -82,7 +90,7 @@ func (s *Service) GetImage(ctx context.Context, uid string, options Options) (im
|
|||||||
outBuff := bytes.NewBuffer(make([]byte, 0, 1024))
|
outBuff := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||||
|
|
||||||
if options.Quality == 0 {
|
if options.Quality == 0 {
|
||||||
options.Quality = 80
|
options.Quality = s.cfg.Quality
|
||||||
}
|
}
|
||||||
|
|
||||||
err = jpeg.Encode(outBuff, decImage, &jpeg.Options{Quality: options.Quality})
|
err = jpeg.Encode(outBuff, decImage, &jpeg.Options{Quality: options.Quality})
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package images
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
"github.com/valkey-io/valkey-go"
|
"github.com/valkey-io/valkey-go"
|
||||||
|
"golang.org/x/sync/semaphore"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
@@ -15,12 +17,16 @@ type Options struct {
|
|||||||
type Service struct {
|
type Service struct {
|
||||||
fs fileSystem
|
fs fileSystem
|
||||||
cache valkey.Client
|
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{
|
return &Service{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
|
seph: semaphore.NewWeighted(cfg.Concurency),
|
||||||
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
func Default() Cfg {
|
||||||
|
return Cfg{
|
||||||
|
Image: &ImageConfig{
|
||||||
|
Concurency: 10,
|
||||||
|
Quality: 80,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
|
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
|
||||||
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
||||||
|
Image *ImageConfig `env:", prefix=IMAGE_"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebdavConfig struct {
|
type WebdavConfig struct {
|
||||||
@@ -30,7 +31,13 @@ type ValkeyConfig struct {
|
|||||||
InitAddress []string `env:"ADDRESS"`
|
InitAddress []string `env:"ADDRESS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ImageConfig struct {
|
||||||
|
Concurency int64 `env:"CONCURENCY"`
|
||||||
|
Quality int `env:"QUALITY"`
|
||||||
|
}
|
||||||
|
|
||||||
func Read(ctx context.Context) (cfg Cfg, err error) {
|
func Read(ctx context.Context) (cfg Cfg, err error) {
|
||||||
|
cfg = Default()
|
||||||
err = envconfig.ProcessWith(ctx, &envconfig.Config{
|
err = envconfig.ProcessWith(ctx, &envconfig.Config{
|
||||||
Target: &cfg,
|
Target: &cfg,
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,7 +50,7 @@ func (s *Server) registerOther() (h http.Handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ = resthome.New(mux, page.New(fs))
|
_ = 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())))
|
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)
|
h = cacheMiddleware(time.Minute * 5)(h)
|
||||||
return 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),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -38,14 +37,3 @@ func registerStatic(mux *http.ServeMux) {
|
|||||||
panic(err)
|
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
+1976
File diff suppressed because it is too large
Load Diff
|
After Width: | Height: | Size: 112 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user