增加页面优化相关API、配置、消息代号
This commit is contained in:
@@ -19,6 +19,7 @@ const (
|
||||
ConfigCodeAccessLog ConfigCode = "accessLog"
|
||||
ConfigCodeStat ConfigCode = "stat"
|
||||
ConfigCodeCompression ConfigCode = "compression"
|
||||
ConfigCodeOptimization ConfigCode = "optimization"
|
||||
ConfigCodePages ConfigCode = "pages"
|
||||
ConfigCodeHeaders ConfigCode = "headers"
|
||||
ConfigCodeWebsocket ConfigCode = "websocket"
|
||||
|
||||
171
pkg/serverconfigs/http_page_optimization_config.go
Normal file
171
pkg/serverconfigs/http_page_optimization_config.go
Normal file
@@ -0,0 +1,171 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"github.com/tdewolff/minify/v2"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var httpPageOptimizationLimiter = make(chan bool, 64)
|
||||
|
||||
type HTTPPageOptimizationMimeType = string
|
||||
|
||||
const (
|
||||
HTTPPageOptimizationMimeTypeHTML HTTPPageOptimizationMimeType = "text/html"
|
||||
HTTPPageOptimizationMimeTypeJavascript HTTPPageOptimizationMimeType = "text/javascript"
|
||||
HTTPPageOptimizationMimeTypeCSS HTTPPageOptimizationMimeType = "text/css"
|
||||
)
|
||||
|
||||
type HTTPPageOptimizationConfig struct {
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"`
|
||||
|
||||
HTML *HTTPHTMLOptimizationConfig `yaml:"html" json:"html"`
|
||||
Javascript *HTTPJavascriptOptimizationConfig `yaml:"javascript" json:"javascript"`
|
||||
CSS *HTTPCSSOptimizationConfig `yaml:"css" json:"css"`
|
||||
|
||||
isOn bool
|
||||
minifyInstance *minify.M
|
||||
}
|
||||
|
||||
func NewHTTPPageOptimizationConfig() *HTTPPageOptimizationConfig {
|
||||
return &HTTPPageOptimizationConfig{
|
||||
IsPrior: false,
|
||||
HTML: NewHTTPHTMLOptimizationConfig(),
|
||||
Javascript: NewHTTPJavascriptOptimizationConfig(),
|
||||
CSS: NewHTTPCSSOptimizationConfig(),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *HTTPPageOptimizationConfig) Init() error {
|
||||
this.isOn = this.CheckIsOn()
|
||||
|
||||
if this.isOn {
|
||||
// MUST NOT create instance for every config
|
||||
this.minifyInstance = minify.New()
|
||||
}
|
||||
|
||||
if this.HTML != nil {
|
||||
err := this.HTML.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if this.HTML.IsOn {
|
||||
this.isOn = true
|
||||
this.minifyInstance.Add(HTTPPageOptimizationMimeTypeHTML, this.HTML.AsMinifier())
|
||||
|
||||
}
|
||||
}
|
||||
if this.Javascript != nil {
|
||||
err := this.Javascript.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if this.Javascript.IsOn {
|
||||
this.isOn = true
|
||||
this.minifyInstance.Add(HTTPPageOptimizationMimeTypeJavascript, this.Javascript.AsMinifier())
|
||||
}
|
||||
}
|
||||
if this.CSS != nil {
|
||||
err := this.CSS.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if this.CSS.IsOn {
|
||||
this.isOn = true
|
||||
this.minifyInstance.Add(HTTPPageOptimizationMimeTypeCSS, this.CSS.AsMinifier())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPPageOptimizationConfig) IsOn() bool {
|
||||
return this.isOn
|
||||
}
|
||||
|
||||
func (this *HTTPPageOptimizationConfig) CheckIsOn() bool {
|
||||
return (this.HTML != nil && this.HTML.IsOn) ||
|
||||
(this.Javascript != nil && this.Javascript.IsOn) ||
|
||||
(this.CSS != nil && this.CSS.IsOn)
|
||||
}
|
||||
|
||||
func (this *HTTPPageOptimizationConfig) FilterResponse(resp *http.Response) error {
|
||||
if !this.isOn || this.minifyInstance == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var contentType = resp.Header.Get("Content-Type")
|
||||
if len(contentType) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// validate content length
|
||||
if resp.ContentLength <= 0 || resp.ContentLength > (1<<20) {
|
||||
return nil
|
||||
}
|
||||
|
||||
contentType, _, _ = strings.Cut(contentType, ";")
|
||||
var mimeType = ""
|
||||
switch contentType {
|
||||
case "text/html":
|
||||
if this.HTML != nil && this.HTML.IsOn {
|
||||
mimeType = HTTPPageOptimizationMimeTypeHTML
|
||||
}
|
||||
case "text/javascript", "application/javascript":
|
||||
if this.Javascript != nil && this.Javascript.IsOn {
|
||||
mimeType = HTTPPageOptimizationMimeTypeJavascript
|
||||
}
|
||||
case "text/css":
|
||||
if this.CSS != nil && this.CSS.IsOn {
|
||||
mimeType = HTTPPageOptimizationMimeTypeCSS
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(mimeType) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// concurrent limiter, to prevent memory overflow
|
||||
select {
|
||||
case httpPageOptimizationLimiter <- true:
|
||||
defer func() {
|
||||
<-httpPageOptimizationLimiter
|
||||
}()
|
||||
|
||||
var contentLength int64
|
||||
var err error
|
||||
resp.Body, contentLength, err = this.minify(mimeType, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// fix resp.ContentLength and Content-Length header
|
||||
resp.ContentLength = contentLength
|
||||
resp.Header.Set("Content-Length", types.String(contentLength))
|
||||
default:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPPageOptimizationConfig) minify(mimeType HTTPPageOptimizationMimeType, rawReader io.ReadCloser) (newReader io.ReadCloser, newContentLength int64, err error) {
|
||||
|
||||
var rawData []byte
|
||||
rawData, err = io.ReadAll(rawReader)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resultData, err := this.minifyInstance.Bytes(mimeType, rawData)
|
||||
if err != nil {
|
||||
return io.NopCloser(bytes.NewReader(rawData)), int64(len(rawData)), nil // return rawData, and ignore error
|
||||
}
|
||||
|
||||
return io.NopCloser(bytes.NewReader(resultData)), int64(len(resultData)), nil
|
||||
}
|
||||
29
pkg/serverconfigs/http_page_optimization_css.go
Normal file
29
pkg/serverconfigs/http_page_optimization_css.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/tdewolff/minify/v2/css"
|
||||
|
||||
type HTTPCSSOptimizationConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
|
||||
Precision int `yaml:"precision" json:"precision"`
|
||||
KeepCSS2 bool `yaml:"keepCSS2" json:"keepCSS2"`
|
||||
}
|
||||
|
||||
func NewHTTPCSSOptimizationConfig() *HTTPCSSOptimizationConfig {
|
||||
return &HTTPCSSOptimizationConfig{
|
||||
KeepCSS2: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *HTTPCSSOptimizationConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPCSSOptimizationConfig) AsMinifier() *css.Minifier {
|
||||
return &css.Minifier{
|
||||
KeepCSS2: this.KeepCSS2,
|
||||
Precision: this.Precision,
|
||||
}
|
||||
}
|
||||
42
pkg/serverconfigs/http_page_optimization_html.go
Normal file
42
pkg/serverconfigs/http_page_optimization_html.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/tdewolff/minify/v2/html"
|
||||
|
||||
type HTTPHTMLOptimizationConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
|
||||
KeepComments bool `yaml:"keepComments" json:"keepComments"`
|
||||
KeepConditionalComments bool `yaml:"keepConditionalComments" json:"keepConditionalComments"`
|
||||
KeepDefaultAttrVals bool `yaml:"keepDefaultAttrVals" json:"keepDefaultAttrVals"`
|
||||
KeepDocumentTags bool `yaml:"keepDocumentTags" json:"keepDocumentTags"`
|
||||
KeepEndTags bool `yaml:"keepEndTags" json:"keepEndTags"`
|
||||
KeepQuotes bool `yaml:"keepQuotes" json:"keepQuotes"`
|
||||
KeepWhitespace bool `yaml:"keepWhitespace" json:"keepWhitespace"`
|
||||
}
|
||||
|
||||
func NewHTTPHTMLOptimizationConfig() *HTTPHTMLOptimizationConfig {
|
||||
return &HTTPHTMLOptimizationConfig{
|
||||
KeepDefaultAttrVals: true,
|
||||
KeepDocumentTags: true,
|
||||
KeepEndTags: true,
|
||||
KeepQuotes: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *HTTPHTMLOptimizationConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPHTMLOptimizationConfig) AsMinifier() *html.Minifier {
|
||||
return &html.Minifier{
|
||||
KeepComments: this.KeepComments,
|
||||
KeepConditionalComments: this.KeepConditionalComments,
|
||||
KeepDefaultAttrVals: this.KeepDefaultAttrVals,
|
||||
KeepDocumentTags: this.KeepDocumentTags,
|
||||
KeepEndTags: this.KeepEndTags,
|
||||
KeepQuotes: this.KeepQuotes,
|
||||
KeepWhitespace: this.KeepWhitespace,
|
||||
}
|
||||
}
|
||||
29
pkg/serverconfigs/http_page_optimization_javascript.go
Normal file
29
pkg/serverconfigs/http_page_optimization_javascript.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/tdewolff/minify/v2/js"
|
||||
|
||||
type HTTPJavascriptOptimizationConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
|
||||
Precision int `yaml:"precision" json:"precision"`
|
||||
Version int `yaml:"version" json:"version"`
|
||||
KeepVarNames bool `yaml:"keepVarNames" json:"keepVarNames"`
|
||||
}
|
||||
|
||||
func NewHTTPJavascriptOptimizationConfig() *HTTPJavascriptOptimizationConfig {
|
||||
return &HTTPJavascriptOptimizationConfig{}
|
||||
}
|
||||
|
||||
func (this *HTTPJavascriptOptimizationConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPJavascriptOptimizationConfig) AsMinifier() *js.Minifier {
|
||||
return &js.Minifier{
|
||||
Precision: this.Precision,
|
||||
KeepVarNames: this.KeepVarNames,
|
||||
Version: this.Version,
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ type HTTPWebConfig struct {
|
||||
Locations []*HTTPLocationConfig `yaml:"locations" json:"locations"` // 路径规则 TODO
|
||||
LocationRefs []*HTTPLocationRef `yaml:"locationRefs" json:"locationRefs"` // 路径规则应用
|
||||
Compression *HTTPCompressionConfig `yaml:"compression" json:"compression"` // 压缩配置
|
||||
Optimization *HTTPPageOptimizationConfig `yaml:"optimization" json:"optimization"` // 页面优化配置
|
||||
WebP *WebPImageConfig `yaml:"webp" json:"webp"` // WebP配置
|
||||
Charset *HTTPCharsetConfig `yaml:"charset" json:"charset"` // 字符编码
|
||||
Shutdown *HTTPShutdownConfig `yaml:"shutdown" json:"shutdown"` // 临时关闭配置
|
||||
@@ -82,6 +83,14 @@ func (this *HTTPWebConfig) Init(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// optimization
|
||||
if this.Optimization != nil {
|
||||
err := this.Optimization.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// charset
|
||||
if this.Charset != nil {
|
||||
err := this.Charset.Init()
|
||||
|
||||
Reference in New Issue
Block a user