mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Add env on startup (#283)
* Add READ_BUFFER_SIZE, CONCURRENCY,DISABLE_KEEPALIVE to ENV * Add TestGuessSupportedFormat
This commit is contained in:
parent
0b0e5b1216
commit
655fb1dcca
@ -74,8 +74,6 @@ services:
|
|||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
- MALLOC_ARENA_MAX=1
|
- 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:
|
volumes:
|
||||||
- ./path/to/pics:/opt/pics
|
- ./path/to/pics:/opt/pics
|
||||||
- ./path/to/exhaust:/opt/exhaust
|
- ./path/to/exhaust:/opt/exhaust
|
||||||
@ -83,11 +81,6 @@ services:
|
|||||||
- ./config.json:/etc/config.json
|
- ./config.json:/etc/config.json
|
||||||
ports:
|
ports:
|
||||||
- 127.0.0.1:3333:3333
|
- 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.
|
You can refer to [Docker | WebP Server Documentation](https://docs.webp.sh/usage/docker/) for more info, such as custom config, AVIF support etc.
|
||||||
|
@ -59,7 +59,7 @@ var (
|
|||||||
ProxyMode bool
|
ProxyMode bool
|
||||||
Prefetch bool
|
Prefetch bool
|
||||||
Config jsonFile
|
Config jsonFile
|
||||||
Version = "0.9.11"
|
Version = "0.9.12"
|
||||||
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
|
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
|
||||||
RemoteRaw = "./remote-raw"
|
RemoteRaw = "./remote-raw"
|
||||||
Metadata = "./metadata"
|
Metadata = "./metadata"
|
||||||
@ -86,9 +86,9 @@ type jsonFile struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&ConfigPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)")
|
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.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(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
|
||||||
flag.BoolVar(&ShowVersion, "V", false, "Show version information.")
|
flag.BoolVar(&ShowVersion, "V", false, "Show version information.")
|
||||||
}
|
}
|
||||||
|
@ -191,3 +191,11 @@ func HashFile(filepath string) string {
|
|||||||
buf, _ := os.ReadFile(filepath)
|
buf, _ := os.ReadFile(filepath)
|
||||||
return fmt.Sprintf("%x", xxhash.Sum64(buf))
|
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
|
||||||
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
"webp_server_go/config"
|
"webp_server_go/config"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
@ -45,3 +47,51 @@ func TestCheckAllowedType(t *testing.T) {
|
|||||||
assert.True(t, CheckAllowedType("test.jpg"))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,9 +5,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"webp_server_go/config"
|
"webp_server_go/config"
|
||||||
"webp_server_go/encoder"
|
"webp_server_go/encoder"
|
||||||
"webp_server_go/handler"
|
"webp_server_go/handler"
|
||||||
|
"webp_server_go/helper"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/etag"
|
"github.com/gofiber/fiber/v2/middleware/etag"
|
||||||
@ -16,11 +18,24 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
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{
|
var app = fiber.New(fiber.Config{
|
||||||
ServerHeader: "WebP Server Go",
|
ServerHeader: "WebP Server Go",
|
||||||
AppName: "WebP Server Go",
|
AppName: "WebP Server Go",
|
||||||
DisableStartupMessage: true,
|
DisableStartupMessage: true,
|
||||||
ProxyHeader: "X-Real-IP",
|
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() {
|
func setupLogger() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user