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
This commit is contained in:
Benny 2021-02-01 16:14:23 +08:00 committed by GitHub
parent f5e7ee5273
commit 9329ac65fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 20 deletions

View File

@ -21,7 +21,7 @@ var (
prefetch, proxyMode bool prefetch, proxyMode bool
remoteRaw = "remote-raw" remoteRaw = "remote-raw"
config Config config Config
version = "0.3.1" version = "0.3.2"
) )
const ( const (

6
go.mod
View File

@ -4,13 +4,13 @@ go 1.15
require ( require (
github.com/chai2010/webp v1.1.0 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/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.3.0 github.com/stretchr/testify v1.3.0
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 golang.org/x/image v0.0.0-20200119044424-58c23975cae1
) )
replace ( 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.2.0
github.com/chai2010/webp v1.1.0 => github.com/webp-sh/webp v1.1.1 github.com/gofiber/fiber/v2 v2.4.0 => github.com/webp-sh/fiber/v2 v2.4.0
) )

View File

@ -38,7 +38,7 @@ func autoUpdate() {
} }
var filename = fmt.Sprintf("webp-server-%s-%s", runtime.GOOS, runtime.GOARCH) var filename = fmt.Sprintf("webp-server-%s-%s", runtime.GOOS, runtime.GOARCH)
if runtime.GOARCH == "windows" { if runtime.GOOS == "windows" {
filename += ".exe" filename += ".exe"
} }
var releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" + filename var releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" + filename

View File

@ -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() { func main() {
// Our banner // Our banner
banner := fmt.Sprintf(` banner := fmt.Sprintf(`
@ -82,18 +96,7 @@ Develop by WebP Server team. https://github.com/webp-sh`, version)
go autoUpdate() go autoUpdate()
config = loadConfig(configPath) config = loadConfig(configPath)
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)
}
}
if prefetch { if prefetch {
go prefetchImages(config.ImgPath, config.ExhaustPath, config.Quality) go prefetchImages(config.ImgPath, config.ExhaustPath, config.Quality)

View File

@ -5,8 +5,10 @@
package main package main
import ( import (
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"net" "net"
"os"
"runtime" "runtime"
"testing" "testing"
"time" "time"
@ -34,17 +36,40 @@ func TestDeferInit(t *testing.T) {
} }
func TestMainFunction(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() go main()
time.Sleep(time.Second * 2) 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 // test read config value
assert.Equal(t, "config.json", configPath) assert.Equal(t, "config.json", configPath)
assert.False(t, prefetch)
assert.Equal(t, runtime.NumCPU(), jobs) assert.Equal(t, runtime.NumCPU(), jobs)
assert.Equal(t, false, dumpSystemd) assert.Equal(t, false, dumpSystemd)
assert.Equal(t, false, dumpConfig) assert.Equal(t, false, dumpConfig)
assert.False(t, verboseMode)
// test port // test port
conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", "3333"), time.Second*2) conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", "3333"), time.Second*2)
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, conn) 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)
}