Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify ENV #289

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
55 changes: 36 additions & 19 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand All @@ -96,6 +85,9 @@ func NewWebPConfig() *WebpConfig {
ExhaustPath: "./exhaust",
EnableAVIF: false,
EnableExtraParams: false,
ReadBufferSize: 4096,
Concurrency: 262144,
DisableKeepalive: false,
}
}

Expand All @@ -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.")
}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 3 additions & 18 deletions webp-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
Loading