mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	升级原有gzip配置到compression配置
This commit is contained in:
		@@ -9,6 +9,7 @@ import (
 | 
				
			|||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
 | 
				
			||||||
	"github.com/iwind/TeaGo/dbs"
 | 
						"github.com/iwind/TeaGo/dbs"
 | 
				
			||||||
	"github.com/iwind/TeaGo/lists"
 | 
						"github.com/iwind/TeaGo/lists"
 | 
				
			||||||
@@ -52,6 +53,9 @@ var upgradeFuncs = []*upgradeVersion{
 | 
				
			|||||||
	{
 | 
						{
 | 
				
			||||||
		"0.3.1", upgradeV0_3_1,
 | 
							"0.3.1", upgradeV0_3_1,
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							"0.3.2", upgradeV0_3_2,
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// UpgradeSQLData 升级SQL数据
 | 
					// UpgradeSQLData 升级SQL数据
 | 
				
			||||||
@@ -393,3 +397,101 @@ func upgradeV0_3_1(db *dbs.DB) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// v0.3.2
 | 
				
			||||||
 | 
					func upgradeV0_3_2(db *dbs.DB) error {
 | 
				
			||||||
 | 
						// gzip => compression
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						type HTTPGzipRef struct {
 | 
				
			||||||
 | 
							IsPrior bool  `yaml:"isPrior" json:"isPrior"` // 是否覆盖
 | 
				
			||||||
 | 
							IsOn    bool  `yaml:"isOn" json:"isOn"`       // 是否开启
 | 
				
			||||||
 | 
							GzipId  int64 `yaml:"gzipId" json:"gzipId"`   // 使用的配置ID
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						webOnes, _, err := db.FindOnes("SELECT id, gzip FROM edgeHTTPWebs WHERE gzip IS NOT NULL")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, webOne := range webOnes {
 | 
				
			||||||
 | 
							var gzipRef = &HTTPGzipRef{}
 | 
				
			||||||
 | 
							err = json.Unmarshal([]byte(webOne.GetString("gzip")), gzipRef)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if gzipRef == nil || gzipRef.GzipId <= 0 {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							var webId = webOne.GetInt("id")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var compressionConfig = &serverconfigs.HTTPCompressionConfig{
 | 
				
			||||||
 | 
								UseDefaultTypes: true,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							compressionConfig.IsPrior = gzipRef.IsPrior
 | 
				
			||||||
 | 
							compressionConfig.IsOn = gzipRef.IsOn
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							gzipOne, err := db.FindOne("SELECT * FROM edgeHTTPGzips WHERE id=?", gzipRef.GzipId)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if len(gzipOne) == 0 {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							level := gzipOne.GetInt("level")
 | 
				
			||||||
 | 
							if level <= 0 {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if level > 0 && level <= 10 {
 | 
				
			||||||
 | 
								compressionConfig.Level = types.Int8(level)
 | 
				
			||||||
 | 
							} else if level > 10 {
 | 
				
			||||||
 | 
								compressionConfig.Level = 10
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var minLengthBytes = []byte(gzipOne.GetString("minLength"))
 | 
				
			||||||
 | 
							if len(minLengthBytes) > 0 {
 | 
				
			||||||
 | 
								var sizeCapacity = &shared.SizeCapacity{}
 | 
				
			||||||
 | 
								err = json.Unmarshal(minLengthBytes, sizeCapacity)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if sizeCapacity != nil {
 | 
				
			||||||
 | 
									compressionConfig.MinLength = sizeCapacity
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var maxLengthBytes = []byte(gzipOne.GetString("maxLength"))
 | 
				
			||||||
 | 
							if len(maxLengthBytes) > 0 {
 | 
				
			||||||
 | 
								var sizeCapacity = &shared.SizeCapacity{}
 | 
				
			||||||
 | 
								err = json.Unmarshal(maxLengthBytes, sizeCapacity)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if sizeCapacity != nil {
 | 
				
			||||||
 | 
									compressionConfig.MaxLength = sizeCapacity
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var condsBytes = []byte(gzipOne.GetString("conds"))
 | 
				
			||||||
 | 
							if len(condsBytes) > 0 {
 | 
				
			||||||
 | 
								var conds = &shared.HTTPRequestCondsConfig{}
 | 
				
			||||||
 | 
								err = json.Unmarshal(condsBytes, conds)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if conds != nil {
 | 
				
			||||||
 | 
									compressionConfig.Conds = conds
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							configJSON, err := json.Marshal(compressionConfig)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							_, err = db.Exec("UPDATE edgeHTTPWebs SET compression=? WHERE id=?", string(configJSON), webId)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,4 +36,20 @@ func TestUpgradeSQLData_v1_3_1(t *testing.T) {
 | 
				
			|||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	t.Log("ok")
 | 
						t.Log("ok")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestUpgradeSQLData_v1_3_2(t *testing.T) {
 | 
				
			||||||
 | 
						db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
 | 
				
			||||||
 | 
							Driver: "mysql",
 | 
				
			||||||
 | 
							Dsn:    "root:123456@tcp(127.0.0.1:3306)/db_edge?charset=utf8mb4&timeout=30s",
 | 
				
			||||||
 | 
							Prefix: "edge",
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = upgradeV0_3_2(db)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						t.Log("ok")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user