From 9329ac65fef55faa7d29a7255d205b9d3fe7e386 Mon Sep 17 00:00:00 2001 From: Benny Date: Mon, 1 Feb 2021 16:14:23 +0800 Subject: [PATCH] fix runtime.GOOS=="windows, add more test cases, bump to newer version of fiber and libwebp (#68) * fix runtime.GOOS=="windows, add some test cases test cases are still limited for flag * update to latest fiber v2.4.0 & libwebp v1.2.0 manually test confirmed --- config.go | 2 +- go.mod | 8 ++++---- update.go | 2 +- webp-server.go | 27 +++++++++++++++------------ webp-server_test.go | 29 +++++++++++++++++++++++++++-- 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/config.go b/config.go index 38311bc..2c30063 100644 --- a/config.go +++ b/config.go @@ -21,7 +21,7 @@ var ( prefetch, proxyMode bool remoteRaw = "remote-raw" config Config - version = "0.3.1" + version = "0.3.2" ) const ( diff --git a/go.mod b/go.mod index 4347c7b..156ad42 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,13 @@ go 1.15 require ( github.com/chai2010/webp v1.1.0 - github.com/gofiber/fiber/v2 v2.1.4 + github.com/gofiber/fiber/v2 v2.4.0 github.com/sirupsen/logrus v1.6.0 github.com/stretchr/testify v1.3.0 golang.org/x/image v0.0.0-20200119044424-58c23975cae1 ) replace ( - github.com/gofiber/fiber/v2 v2.1.4 => github.com/webp-sh/fiber/v2 v2.1.4 - github.com/chai2010/webp v1.1.0 => github.com/webp-sh/webp v1.1.1 -) \ No newline at end of file + github.com/chai2010/webp v1.1.0 => github.com/webp-sh/webp v1.2.0 + github.com/gofiber/fiber/v2 v2.4.0 => github.com/webp-sh/fiber/v2 v2.4.0 +) diff --git a/update.go b/update.go index e7ce14e..1a1afab 100644 --- a/update.go +++ b/update.go @@ -38,7 +38,7 @@ func autoUpdate() { } var filename = fmt.Sprintf("webp-server-%s-%s", runtime.GOOS, runtime.GOARCH) - if runtime.GOARCH == "windows" { + if runtime.GOOS == "windows" { filename += ".exe" } var releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" + filename diff --git a/webp-server.go b/webp-server.go index 87e2b54..bbe9315 100644 --- a/webp-server.go +++ b/webp-server.go @@ -54,6 +54,20 @@ func deferInit() { } } +func switchProxyMode() { + // Check for remote address + matched, _ := regexp.MatchString(`^https?://`, config.ImgPath) + proxyMode = false + if matched { + proxyMode = true + } else { + _, err := os.Stat(config.ImgPath) + if err != nil { + log.Fatalf("Your image path %s is incorrect.Please check and confirm.", config.ImgPath) + } + } +} + func main() { // Our banner banner := fmt.Sprintf(` @@ -82,18 +96,7 @@ Develop by WebP Server team. https://github.com/webp-sh`, version) go autoUpdate() config = loadConfig(configPath) - - // Check for remote address - matched, _ := regexp.MatchString(`^https?://`, config.ImgPath) - proxyMode = false - if matched { - proxyMode = true - } else { - _, err := os.Stat(config.ImgPath) - if err != nil { - log.Fatalf("Your image path %s is incorrect.Please check and confirm.", config.ImgPath) - } - } + switchProxyMode() if prefetch { go prefetchImages(config.ImgPath, config.ExhaustPath, config.Quality) diff --git a/webp-server_test.go b/webp-server_test.go index d4565df..4cb069e 100644 --- a/webp-server_test.go +++ b/webp-server_test.go @@ -5,8 +5,10 @@ package main import ( + log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "net" + "os" "runtime" "testing" "time" @@ -34,17 +36,40 @@ func TestDeferInit(t *testing.T) { } func TestMainFunction(t *testing.T) { + // first test verbose mode + assert.False(t, verboseMode) + assert.Equal(t, log.GetLevel(), log.InfoLevel) + os.Args = append(os.Args, "-v", "-prefetch") + + // run main function go main() time.Sleep(time.Second * 2) + // verbose, prefetch + assert.Equal(t, log.GetLevel(), log.DebugLevel) + assert.True(t, verboseMode) + assert.True(t, prefetch) + // test read config value assert.Equal(t, "config.json", configPath) - assert.False(t, prefetch) assert.Equal(t, runtime.NumCPU(), jobs) assert.Equal(t, false, dumpSystemd) assert.Equal(t, false, dumpConfig) - assert.False(t, verboseMode) + // test port conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", "3333"), time.Second*2) assert.Nil(t, err) assert.NotNil(t, conn) } + +func TestProxySwitch(t *testing.T) { + // real proxy mode + assert.False(t, proxyMode) + config.ImgPath = "https://z.cn" + switchProxyMode() + assert.True(t, proxyMode) + + // normal + config.ImgPath = os.TempDir() + switchProxyMode() + assert.False(t, proxyMode) +}