diff --git a/config.json b/config.json index 831651abe..3979939d3 100644 --- a/config.json +++ b/config.json @@ -7,5 +7,8 @@ "ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif","svg","heic"], "IMG_MAP": {}, "ENABLE_AVIF": false, - "ENABLE_EXTRA_PARAMS": false + "ENABLE_EXTRA_PARAMS": false, + "READ_BUFFER_SIZE": 4096, + "CONCURRENCY": 262144, + "DISABLE_KEEPALIVE": false } diff --git a/config/config.go b/config/config.go index 4d89c827c..7bf2881af 100644 --- a/config/config.go +++ b/config/config.go @@ -28,27 +28,13 @@ const ( "IMG_PATH": "./pics", "EXHAUST_PATH": "./exhaust", "IMG_MAP": {}, - "ALLOWED_TYPES": ["jpg","png","jpeg","bmp","svg","nef"], + "ALLOWED_TYPES": ["jpg","png","jpeg","bmp","svg","heic","nef"], "ENABLE_AVIF": false, "ENABLE_EXTRA_PARAMS": false + "READ_BUFFER_SIZE": 4096, + "CONCURRENCY": 262144, + "DISABLE_KEEPALIVE": false }` - - SampleSystemd = ` -[Unit] -Description=WebP Server Go -Documentation=https://github.com/webp-sh/webp_server_go -After=nginx.target - -[Service] -Type=simple -StandardError=journal -WorkingDirectory=/opt/webps -ExecStart=/opt/webps/webp-server --config /opt/webps/config.json -Restart=always -RestartSec=3s - -[Install] -WantedBy=multi-user.target` ) var ( @@ -83,6 +69,9 @@ type WebpConfig struct { ExhaustPath string `json:"EXHAUST_PATH"` EnableAVIF bool `json:"ENABLE_AVIF"` EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"` + ReadBufferSize int `json:"READ_BUFFER_SIZE"` + Concurrency int `json:"CONCURRENCY"` + DisableKeepalive bool `json:"DISABLE_KEEPALIVE"` } func NewWebPConfig() *WebpConfig { @@ -96,6 +85,9 @@ func NewWebPConfig() *WebpConfig { ExhaustPath: "./exhaust", EnableAVIF: false, EnableExtraParams: false, + ReadBufferSize: 4096, + Concurrency: 262144, + DisableKeepalive: false, } } @@ -104,7 +96,6 @@ func init() { 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(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.") flag.BoolVar(&ShowVersion, "V", false, "Show version information.") } @@ -166,6 +157,32 @@ func LoadConfig() { if os.Getenv("WEBP_IMG_MAP") != "" { // TODO } + if os.Getenv("WEBP_READ_BUFFER_SIZE") != "" { + readBufferSize, err := strconv.Atoi(os.Getenv("WEBP_READ_BUFFER_SIZE")) + if err != nil { + log.Warnf("WEBP_READ_BUFFER_SIZE is not a valid integer, using value in config.json %d", Config.ReadBufferSize) + } else { + Config.ReadBufferSize = readBufferSize + } + } + if os.Getenv("WEBP_CONCURRENCY") != "" { + concurrency, err := strconv.Atoi(os.Getenv("WEBP_CONCURRENCY")) + if err != nil { + log.Warnf("WEBP_CONCURRENCY is not a valid integer, using value in config.json %d", Config.Concurrency) + } else { + Config.Concurrency = concurrency + } + } + if os.Getenv("WEBP_DISABLE_KEEPALIVE") != "" { + disableKeepalive := os.Getenv("WEBP_DISABLE_KEEPALIVE") + if disableKeepalive == "true" { + Config.DisableKeepalive = true + } else if disableKeepalive == "false" { + Config.DisableKeepalive = false + } else { + log.Warnf("WEBP_DISABLE_KEEPALIVE is not a valid boolean, using value in config.json %t", Config.DisableKeepalive) + } + } log.Debugln("Config init complete") log.Debugln("Config", Config) diff --git a/helper/helper.go b/helper/helper.go index 68d399897..d832c0573 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -191,11 +191,3 @@ 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/webp-server.go b/webp-server.go index 7bb5079bb..9ed7455d7 100644 --- a/webp-server.go +++ b/webp-server.go @@ -5,11 +5,9 @@ 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" @@ -18,24 +16,15 @@ 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 + ReadBufferSize: config.Config.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: config.Config.Concurrency, // Maximum number of concurrent connections. + DisableKeepalive: config.Config.DisableKeepalive, // Disable keep-alive connections, the server will close incoming connections after sending the first response to the client }) func setupLogger() { @@ -78,10 +67,6 @@ func init() { fmt.Println(config.SampleConfig) os.Exit(0) } - if config.DumpSystemd { - fmt.Println(config.SampleSystemd) - os.Exit(0) - } if config.ShowVersion { fmt.Printf("\n %c[1;32m%s%c[0m\n\n", 0x1B, banner+"", 0x1B) os.Exit(0)