diff --git a/config.example.yaml b/config.example.yaml index d8ba217..19494ca 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -51,3 +51,5 @@ singleIpSearchNum: 10 maxPostIdCacheTime: 1h # 用户信息缓存时间 userInfoCacheTime: 24h +# Gzip +gzip: false diff --git a/go.mod b/go.mod index 2cc72d0..211730a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,9 @@ module github/fthvgb1/wp-go go 1.18 require ( + github.com/dlclark/regexp2 v1.7.0 + github.com/gin-contrib/gzip v0.0.6 + github.com/gin-contrib/pprof v1.4.0 github.com/gin-contrib/sessions v0.0.5 github.com/gin-gonic/gin v1.8.1 github.com/go-sql-driver/mysql v1.6.0 @@ -11,8 +14,6 @@ require ( ) require ( - github.com/dlclark/regexp2 v1.7.0 // indirect - github.com/gin-contrib/pprof v1.4.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect diff --git a/route/route.go b/route/route.go index 436f15c..0152453 100644 --- a/route/route.go +++ b/route/route.go @@ -1,6 +1,7 @@ package route import ( + "github.com/gin-contrib/gzip" "github.com/gin-contrib/pprof" "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" @@ -10,6 +11,7 @@ import ( "github/fthvgb1/wp-go/middleware" "github/fthvgb1/wp-go/static" "github/fthvgb1/wp-go/templates" + "github/fthvgb1/wp-go/vars" "html/template" "net/http" "time" @@ -29,10 +31,11 @@ func SetupRouter() *gin.Engine { }).SetTemplate() r.Use(gin.Logger(), middleware.FlowLimit(), gin.Recovery(), middleware.SetStaticFileCache) //gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用 - /*r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{ - "/wp-includes/", "/wp-content/", - })))*/ - + if vars.Conf.Gzip { + r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{ + "/wp-includes/", "/wp-content/", + }))) + } f := static.Fs{FS: static.FsEx, Path: "wp-includes"} r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.FsEx)) r.StaticFS("/wp-includes", http.FS(f)) diff --git a/vars/config.go b/vars/config.go index 2243ce4..f4fed55 100644 --- a/vars/config.go +++ b/vars/config.go @@ -30,6 +30,7 @@ type Config struct { SingleIpSearchNum int64 `yaml:"singleIpSearchNum"` MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"` UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` + Gzip bool `yaml:"gzip"` } type Mysql struct {