mirror of
				https://github.com/TeaOSLab/EdgeCommon.git
				synced 2025-11-04 13:10:24 +08:00 
			
		
		
		
	页面优化增加例外URL和限制URL
This commit is contained in:
		
							
								
								
									
										62
									
								
								pkg/serverconfigs/http_page_optimization_base.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								pkg/serverconfigs/http_page_optimization_base.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
				
			|||||||
 | 
					// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package serverconfigs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type HTTPBaseOptimizationConfig struct {
 | 
				
			||||||
 | 
						OnlyURLPatterns   []*shared.URLPattern `yaml:"onlyURLPatterns" json:"onlyURLPatterns"`     // 仅限的URL
 | 
				
			||||||
 | 
						ExceptURLPatterns []*shared.URLPattern `yaml:"exceptURLPatterns" json:"exceptURLPatterns"` // 排除的URL
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (this *HTTPBaseOptimizationConfig) Init() error {
 | 
				
			||||||
 | 
						// only url
 | 
				
			||||||
 | 
						for _, pattern := range this.OnlyURLPatterns {
 | 
				
			||||||
 | 
							err := pattern.Init()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// except url
 | 
				
			||||||
 | 
						for _, pattern := range this.ExceptURLPatterns {
 | 
				
			||||||
 | 
							err := pattern.Init()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (this *HTTPBaseOptimizationConfig) MatchURL(url string) bool {
 | 
				
			||||||
 | 
						// 去除问号
 | 
				
			||||||
 | 
						var index = strings.Index(url, "?")
 | 
				
			||||||
 | 
						if index >= 0 {
 | 
				
			||||||
 | 
							url = url[:index]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// except
 | 
				
			||||||
 | 
						if len(this.ExceptURLPatterns) > 0 {
 | 
				
			||||||
 | 
							for _, pattern := range this.ExceptURLPatterns {
 | 
				
			||||||
 | 
								if pattern.Match(url) {
 | 
				
			||||||
 | 
									return false
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// only
 | 
				
			||||||
 | 
						if len(this.OnlyURLPatterns) > 0 {
 | 
				
			||||||
 | 
							for _, pattern := range this.OnlyURLPatterns {
 | 
				
			||||||
 | 
								if pattern.Match(url) {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -94,7 +94,7 @@ func (this *HTTPPageOptimizationConfig) CheckIsOn() bool {
 | 
				
			|||||||
		(this.CSS != nil && this.CSS.IsOn)
 | 
							(this.CSS != nil && this.CSS.IsOn)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *HTTPPageOptimizationConfig) FilterResponse(resp *http.Response) error {
 | 
					func (this *HTTPPageOptimizationConfig) FilterResponse(url string, resp *http.Response) error {
 | 
				
			||||||
	if !this.isOn || this.minifyInstance == nil {
 | 
						if !this.isOn || this.minifyInstance == nil {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -113,15 +113,15 @@ func (this *HTTPPageOptimizationConfig) FilterResponse(resp *http.Response) erro
 | 
				
			|||||||
	var mimeType = ""
 | 
						var mimeType = ""
 | 
				
			||||||
	switch contentType {
 | 
						switch contentType {
 | 
				
			||||||
	case "text/html":
 | 
						case "text/html":
 | 
				
			||||||
		if this.HTML != nil && this.HTML.IsOn {
 | 
							if this.HTML != nil && this.HTML.IsOn && this.HTML.MatchURL(url) {
 | 
				
			||||||
			mimeType = HTTPPageOptimizationMimeTypeHTML
 | 
								mimeType = HTTPPageOptimizationMimeTypeHTML
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case "text/javascript", "application/javascript":
 | 
						case "text/javascript", "application/javascript":
 | 
				
			||||||
		if this.Javascript != nil && this.Javascript.IsOn {
 | 
							if this.Javascript != nil && this.Javascript.IsOn && this.Javascript.MatchURL(url) {
 | 
				
			||||||
			mimeType = HTTPPageOptimizationMimeTypeJavascript
 | 
								mimeType = HTTPPageOptimizationMimeTypeJavascript
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case "text/css":
 | 
						case "text/css":
 | 
				
			||||||
		if this.CSS != nil && this.CSS.IsOn {
 | 
							if this.CSS != nil && this.CSS.IsOn && this.CSS.MatchURL(url) {
 | 
				
			||||||
			mimeType = HTTPPageOptimizationMimeTypeCSS
 | 
								mimeType = HTTPPageOptimizationMimeTypeCSS
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,8 @@ package serverconfigs
 | 
				
			|||||||
import "github.com/tdewolff/minify/v2/css"
 | 
					import "github.com/tdewolff/minify/v2/css"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HTTPCSSOptimizationConfig struct {
 | 
					type HTTPCSSOptimizationConfig struct {
 | 
				
			||||||
 | 
						HTTPBaseOptimizationConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	IsOn bool `yaml:"isOn" json:"isOn"`
 | 
						IsOn bool `yaml:"isOn" json:"isOn"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Precision int  `yaml:"precision" json:"precision"`
 | 
						Precision int  `yaml:"precision" json:"precision"`
 | 
				
			||||||
@@ -18,6 +20,10 @@ func NewHTTPCSSOptimizationConfig() *HTTPCSSOptimizationConfig {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *HTTPCSSOptimizationConfig) Init() error {
 | 
					func (this *HTTPCSSOptimizationConfig) Init() error {
 | 
				
			||||||
 | 
						err := this.HTTPBaseOptimizationConfig.Init()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,9 +2,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package serverconfigs
 | 
					package serverconfigs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "github.com/tdewolff/minify/v2/html"
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/tdewolff/minify/v2/html"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HTTPHTMLOptimizationConfig struct {
 | 
					type HTTPHTMLOptimizationConfig struct {
 | 
				
			||||||
 | 
						HTTPBaseOptimizationConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	IsOn bool `yaml:"isOn" json:"isOn"`
 | 
						IsOn bool `yaml:"isOn" json:"isOn"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	KeepComments            bool `yaml:"keepComments" json:"keepComments"`
 | 
						KeepComments            bool `yaml:"keepComments" json:"keepComments"`
 | 
				
			||||||
@@ -26,6 +30,10 @@ func NewHTTPHTMLOptimizationConfig() *HTTPHTMLOptimizationConfig {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *HTTPHTMLOptimizationConfig) Init() error {
 | 
					func (this *HTTPHTMLOptimizationConfig) Init() error {
 | 
				
			||||||
 | 
						err := this.HTTPBaseOptimizationConfig.Init()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,8 @@ package serverconfigs
 | 
				
			|||||||
import "github.com/tdewolff/minify/v2/js"
 | 
					import "github.com/tdewolff/minify/v2/js"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HTTPJavascriptOptimizationConfig struct {
 | 
					type HTTPJavascriptOptimizationConfig struct {
 | 
				
			||||||
 | 
						HTTPBaseOptimizationConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	IsOn bool `yaml:"isOn" json:"isOn"`
 | 
						IsOn bool `yaml:"isOn" json:"isOn"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Precision    int  `yaml:"precision" json:"precision"`
 | 
						Precision    int  `yaml:"precision" json:"precision"`
 | 
				
			||||||
@@ -17,6 +19,10 @@ func NewHTTPJavascriptOptimizationConfig() *HTTPJavascriptOptimizationConfig {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *HTTPJavascriptOptimizationConfig) Init() error {
 | 
					func (this *HTTPJavascriptOptimizationConfig) Init() error {
 | 
				
			||||||
 | 
						err := this.HTTPBaseOptimizationConfig.Init()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user