From 655fb1dccadf9234d3d919dd8b8acde9db15c58a Mon Sep 17 00:00:00 2001 From: Nova Kwok Date: Tue, 17 Oct 2023 22:41:01 +0800 Subject: [PATCH] Add env on startup (#283) * Add READ_BUFFER_SIZE, CONCURRENCY,DISABLE_KEEPALIVE to ENV * Add TestGuessSupportedFormat --- README.md | 9 +------- config/config.go | 6 +++--- helper/helper.go | 8 +++++++ helper/helper_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++ webp-server.go | 15 +++++++++++++ 5 files changed, 77 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a31857e..1aea776 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,6 @@ services: restart: always environment: - MALLOC_ARENA_MAX=1 - # - LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 - # - LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.5.6 volumes: - ./path/to/pics:/opt/pics - ./path/to/exhaust:/opt/exhaust @@ -83,11 +81,6 @@ services: - ./config.json:/etc/config.json ports: - 127.0.0.1:3333:3333 - deploy: - resources: - limits: - memory: 400M - memswap_limit: 400M ``` You can refer to [Docker | WebP Server Documentation](https://docs.webp.sh/usage/docker/) for more info, such as custom config, AVIF support etc. @@ -96,7 +89,7 @@ You can refer to [Docker | WebP Server Documentation](https://docs.webp.sh/usage If you'd like to use with binary, please consult to [Use with Binary(Advanced) | WebP Server Documentation](https://docs.webp.sh/usage/usage-with-binary/) ->spoiler alert: you may encounter issues with `glibc` and some dependency libraries. +> spoiler alert: you may encounter issues with `glibc` and some dependency libraries. For `supervisor` or detailed Nginx configuration, please read our documentation at [https://docs.webp.sh/](https://docs.webp.sh/) diff --git a/config/config.go b/config/config.go index 84ae2bc..cb47989 100644 --- a/config/config.go +++ b/config/config.go @@ -59,7 +59,7 @@ var ( ProxyMode bool Prefetch bool Config jsonFile - Version = "0.9.11" + Version = "0.9.12" WriteLock = cache.New(5*time.Minute, 10*time.Minute) RemoteRaw = "./remote-raw" Metadata = "./metadata" @@ -86,9 +86,9 @@ type jsonFile struct { func init() { flag.StringVar(&ConfigPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)") - flag.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert image to webp") + flag.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert image to WebP format.") flag.IntVar(&Jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.") - flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json") + flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json.") flag.BoolVar(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.") flag.BoolVar(&ShowVersion, "V", false, "Show version information.") } diff --git a/helper/helper.go b/helper/helper.go index d832c05..68d3998 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -191,3 +191,11 @@ func HashFile(filepath string) string { buf, _ := os.ReadFile(filepath) return fmt.Sprintf("%x", xxhash.Sum64(buf)) } + +func GetEnv(key string, defaultVal ...string) string { + value := os.Getenv(key) + if value == "" && len(defaultVal) > 0 { + return defaultVal[0] + } + return value +} diff --git a/helper/helper_test.go b/helper/helper_test.go index efe80a9..799966b 100644 --- a/helper/helper_test.go +++ b/helper/helper_test.go @@ -1,10 +1,12 @@ package helper import ( + "slices" "testing" "webp_server_go/config" "github.com/stretchr/testify/assert" + "github.com/valyala/fasthttp" ) func TestMain(m *testing.M) { @@ -45,3 +47,51 @@ func TestCheckAllowedType(t *testing.T) { assert.True(t, CheckAllowedType("test.jpg")) }) } + +func TestGuessSupportedFormat(t *testing.T) { + tests := []struct { + name string + userAgent string + accept string + expected []string + }{ + { + name: "WebP/AVIF Supported", + userAgent: "iPhone OS 16", + accept: "image/webp, image/png", + expected: []string{"raw", "webp", "avif"}, + }, + { + name: "Both Supported", + userAgent: "iPhone OS 16", + accept: "image/webp, image/avif", + expected: []string{"raw", "webp", "avif"}, + }, + { + name: "No Supported Formats", + userAgent: "Unknown OS", + accept: "image/jpeg, image/gif", + expected: []string{"raw"}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + header := &fasthttp.RequestHeader{} + header.Set("user-agent", test.userAgent) + header.Set("accept", test.accept) + + result := GuessSupportedFormat(header) + + if len(result) != len(test.expected) { + t.Errorf("Expected %v, but got %v", test.expected, result) + } + + for _, format := range test.expected { + if !slices.Contains(result, format) { + t.Errorf("Expected format %s is not in the result", format) + } + } + }) + } +} diff --git a/webp-server.go b/webp-server.go index 266e3ec..7bb5079 100644 --- a/webp-server.go +++ b/webp-server.go @@ -5,9 +5,11 @@ import ( "fmt" "os" "runtime" + "strconv" "webp_server_go/config" "webp_server_go/encoder" "webp_server_go/handler" + "webp_server_go/helper" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/etag" @@ -16,11 +18,24 @@ import ( log "github.com/sirupsen/logrus" ) +var ( + ReadBufferSizeStr = helper.GetEnv("READ_BUFFER_SIZE", "4096") // Default: 4096 + ReadBufferSize, _ = strconv.Atoi(ReadBufferSizeStr) + ConcurrencyStr = helper.GetEnv("CONCURRENCY", "262144") // Default: 256 * 1024 + Concurrency, _ = strconv.Atoi(ConcurrencyStr) + DisableKeepaliveStr = helper.GetEnv("DISABLE_KEEPALIVE", "false") // Default: false + DisableKeepalive, _ = strconv.ParseBool(DisableKeepaliveStr) +) + +// https://docs.gofiber.io/api/fiber var app = fiber.New(fiber.Config{ ServerHeader: "WebP Server Go", AppName: "WebP Server Go", DisableStartupMessage: true, ProxyHeader: "X-Real-IP", + ReadBufferSize: ReadBufferSize, // per-connection buffer size for requests' reading. This also limits the maximum header size. Increase this buffer if your clients send multi-KB RequestURIs and/or multi-KB headers (for example, BIG cookies). + Concurrency: Concurrency, // Maximum number of concurrent connections. + DisableKeepalive: DisableKeepalive, // Disable keep-alive connections, the server will close incoming connections after sending the first response to the client }) func setupLogger() {