From ff89e036d4c4c39e52b9ce90b32bcec299d294df Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Mon, 6 May 2024 17:31:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96API=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D=E7=BD=AE=EF=BC=88?= =?UTF-8?q?db.yaml=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/configs/.gitignore | 3 +- build/configs/db.template.yaml | 20 ++----- internal/configs/api_config.go | 83 +++++++++++++++------------- internal/configs/db_config.go | 25 +++++++++ internal/configs/simple_db_config.go | 58 +++++++++++++++++++ internal/nodes/api_node.go | 24 +++++--- internal/setup/setup.go | 13 +---- internal/setup/sql_executor.go | 13 ++--- 8 files changed, 159 insertions(+), 80 deletions(-) create mode 100644 internal/configs/db_config.go create mode 100644 internal/configs/simple_db_config.go diff --git a/build/configs/.gitignore b/build/configs/.gitignore index 237771ba..2493eeac 100644 --- a/build/configs/.gitignore +++ b/build/configs/.gitignore @@ -1,2 +1,3 @@ api.yaml -db.yaml \ No newline at end of file +db.yaml +.db.yaml \ No newline at end of file diff --git a/build/configs/db.template.yaml b/build/configs/db.template.yaml index 6be852d9..04fc5021 100644 --- a/build/configs/db.template.yaml +++ b/build/configs/db.template.yaml @@ -1,16 +1,4 @@ -default: - db: "prod" - prefix: "" - -dbs: - prod: - driver: "mysql" - dsn: "root:123456@tcp(127.0.0.1:3306)/db_edge?charset=utf8mb4&timeout=30s" - prefix: "edge" - models: - package: internal/web/models - - -fields: - bool: [ "uamIsOn", "followPort", "requestHostExcludingPort", "autoRemoteStart", "autoInstallNftables", "enableIPLists", "detectAgents", "checkingPorts", "enableRecordHealthCheck", "offlineIsNotified", "http2Enabled", "http3Enabled", "enableHTTP2", "retry50X", "retry40X", "autoSystemTuning", "disableDefaultDB", "autoTrimDisks","enableGlobalPages" ] - +user: root +password: 123456 +host: 127.0.0.1:3306 +database: db_edge \ No newline at end of file diff --git a/internal/configs/api_config.go b/internal/configs/api_config.go index 0712ecc2..286eaf6d 100644 --- a/internal/configs/api_config.go +++ b/internal/configs/api_config.go @@ -28,56 +28,63 @@ func SharedAPIConfig() (*APIConfig, error) { } // 候选文件 - localFile := Tea.ConfigFile("api.yaml") - isFromLocal := false - paths := []string{localFile} - homeDir, homeErr := os.UserHomeDir() - if homeErr == nil { - paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/api.yaml") - } - paths = append(paths, "/etc/"+teaconst.ProcessName+"/api.yaml") - - // 依次检查文件 - var data []byte - var err error - for _, path := range paths { - data, err = os.ReadFile(path) - if err == nil { - if path == localFile { - isFromLocal = true - } - break + var config = &APIConfig{} + { + var localFile = Tea.ConfigFile("api.yaml") + var isFromLocal = false + var paths = []string{localFile} + homeDir, homeErr := os.UserHomeDir() + if homeErr == nil { + paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/api.yaml") } - } - if err != nil { - return nil, err - } + paths = append(paths, "/etc/"+teaconst.ProcessName+"/api.yaml") - // 解析内容 - config := &APIConfig{} - err = yaml.Unmarshal(data, config) - if err != nil { - return nil, err - } + // 依次检查文件 + var data []byte + var err error + var firstErr error + for _, path := range paths { + data, err = os.ReadFile(path) + if err != nil { + if firstErr == nil { + firstErr = err + } + } else { + if path == localFile { + isFromLocal = true + } + break + } + } + if firstErr != nil { + return nil, firstErr + } - if !isFromLocal { - // 恢复文件 - _ = os.WriteFile(localFile, data, 0666) + // 解析内容 + err = yaml.Unmarshal(data, config) + if err != nil { + return nil, err + } + + if !isFromLocal { + // 恢复文件 + _ = os.WriteFile(localFile, data, 0666) + } } // 恢复数据库文件 { - dbConfigFile := Tea.ConfigFile("db.yaml") + var dbConfigFile = Tea.ConfigFile("db.yaml") _, err := os.Stat(dbConfigFile) if err != nil { - paths := []string{} + var paths = []string{} homeDir, homeErr := os.UserHomeDir() if homeErr == nil { paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/db.yaml") } paths = append(paths, "/etc/"+teaconst.ProcessName+"/db.yaml") for _, path := range paths { - _, err := os.Stat(path) + _, err = os.Stat(path) if err == nil { data, err := os.ReadFile(path) if err == nil { @@ -112,9 +119,9 @@ func (this *APIConfig) WriteFile(path string) error { } // 生成备份文件 - filename := filepath.Base(path) + var filename = filepath.Base(path) homeDir, _ := os.UserHomeDir() - backupDirs := []string{"/etc/edge-api"} + var backupDirs = []string{"/etc/edge-api"} if len(homeDir) > 0 { backupDirs = append(backupDirs, homeDir+"/.edge-api") } @@ -135,7 +142,7 @@ func (this *APIConfig) WriteFile(path string) error { // ResetAPIConfig 重置配置 func ResetAPIConfig() error { - for _, filename := range []string{"api.yaml", "db.yaml"} { + for _, filename := range []string{"api.yaml", "db.yaml", ".db.yaml"} { // 重置 configs/api.yaml { var configFile = Tea.ConfigFile(filename) diff --git a/internal/configs/db_config.go b/internal/configs/db_config.go new file mode 100644 index 00000000..e113e7a9 --- /dev/null +++ b/internal/configs/db_config.go @@ -0,0 +1,25 @@ +// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package configs + +import ( + "errors" + "github.com/iwind/TeaGo/Tea" + "github.com/iwind/TeaGo/dbs" + "gopkg.in/yaml.v3" + "os" +) + +func LoadDBConfig() (*dbs.Config, error) { + var config = &dbs.Config{} + for _, filename := range []string{".db.yaml", "db.yaml"} { + configData, err := os.ReadFile(Tea.ConfigFile(filename)) + if err != nil { + continue + } + err = yaml.Unmarshal(configData, config) + return config, err + } + + return nil, errors.New("could not find database config file '.db.yaml' or 'db.yaml'") +} diff --git a/internal/configs/simple_db_config.go b/internal/configs/simple_db_config.go new file mode 100644 index 00000000..cbc66c21 --- /dev/null +++ b/internal/configs/simple_db_config.go @@ -0,0 +1,58 @@ +// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package configs + +import ( + "fmt" + "github.com/iwind/TeaGo/Tea" + "github.com/iwind/TeaGo/dbs" + "gopkg.in/yaml.v3" + "net/url" + "os" +) + +type SimpleDBConfig struct { + User string `yaml:"user"` + Password string `yaml:"password"` + Database string `yaml:"database"` + Host string `yaml:"host"` + BoolFields []string `yaml:"boolFields,omitempty"` +} + +func ParseSimpleDBConfig(data []byte) (*SimpleDBConfig, error) { + var config = &SimpleDBConfig{} + err := yaml.Unmarshal(data, config) + return config, err +} + +func (this *SimpleDBConfig) GenerateOldConfig() error { + var dbConfig = &dbs.DBConfig{ + Driver: "mysql", + Dsn: url.QueryEscape(this.User) + ":" + url.QueryEscape(this.Password) + "@tcp(" + this.Host + ")/" + url.PathEscape(this.Database) + "?charset=utf8mb4&timeout=30s&multiStatements=true", + Prefix: "edge", + } + dbConfig.Models.Package = "internal/db/models" + + var config = &dbs.Config{ + DBs: map[string]*dbs.DBConfig{ + Tea.Env: dbConfig, + }, + } + config.Default.DB = Tea.Env + config.Fields = map[string][]string{ + "bool": this.BoolFields, + } + + oldConfigYAML, encodeErr := yaml.Marshal(config) + if encodeErr != nil { + return encodeErr + } + + var targetFile = Tea.ConfigFile(".db.yaml") + err := os.WriteFile(targetFile, oldConfigYAML, 0666) + if err != nil { + return fmt.Errorf("create database config file failed: %w", err) + } + + return nil +} diff --git a/internal/nodes/api_node.go b/internal/nodes/api_node.go index 54616fbe..217b5001 100644 --- a/internal/nodes/api_node.go +++ b/internal/nodes/api_node.go @@ -29,7 +29,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/status" - "gopkg.in/yaml.v3" "log" "net" "os" @@ -300,6 +299,22 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err func (this *APINode) checkDB() error { logs.Println("[API_NODE]checking database connection ...") + // generate .db.yaml + { + data, err := os.ReadFile(Tea.ConfigFile("db.yaml")) + if err != nil { + return errors.New("could not find database config file 'db.yaml' (at " + Tea.ConfigFile("db.yaml") + ")") + } + + simpleConfig, err := configs.ParseSimpleDBConfig(data) + if err == nil && len(simpleConfig.Host) > 0 { + err = simpleConfig.GenerateOldConfig() + if err != nil { + return err + } + } + } + // lookup mysqld_safe process go dbutils.FindMySQLPathAndRemember() @@ -356,12 +371,7 @@ func (this *APINode) autoUpgrade() error { } // 执行SQL - var config = &dbs.Config{} - configData, err := os.ReadFile(Tea.ConfigFile("db.yaml")) - if err != nil { - return fmt.Errorf("read database config file failed: %w", err) - } - err = yaml.Unmarshal(configData, config) + config, err := configs.LoadDBConfig() if err != nil { return fmt.Errorf("decode database config failed: %w", err) } diff --git a/internal/setup/setup.go b/internal/setup/setup.go index e5df0a6f..27134953 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -9,9 +9,7 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/cmd" - "github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/types" - "gopkg.in/yaml.v3" "os" "strconv" "strings" @@ -87,12 +85,7 @@ func (this *Setup) Run() error { } // 执行SQL - var config = &dbs.Config{} - configData, err := os.ReadFile(Tea.ConfigFile("db.yaml")) - if err != nil { - return err - } - err = yaml.Unmarshal(configData, config) + config, err := configs.LoadDBConfig() if err != nil { return err } @@ -144,7 +137,7 @@ func (this *Setup) Run() error { return err } if apiNodeId == 0 { - addr := &serverconfigs.NetworkAddressConfig{ + var addr = &serverconfigs.NetworkAddressConfig{ Protocol: serverconfigs.Protocol(this.config.APINodeProtocol), Host: this.config.APINodeHost, PortRange: strconv.Itoa(this.config.APINodePort), @@ -172,7 +165,7 @@ func (this *Setup) Run() error { } if this.config.APINodeProtocol == "https" { // TODO 如果在安装过程中开启了HTTPS,需要同时上传SSL证书 - httpsConfig := &serverconfigs.HTTPSProtocolConfig{} + var httpsConfig = &serverconfigs.HTTPSProtocolConfig{} httpsConfig.IsOn = true httpsConfig.Listen = []*serverconfigs.NetworkAddressConfig{ { diff --git a/internal/setup/sql_executor.go b/internal/setup/sql_executor.go index 1bbc804a..f2ba6d2a 100644 --- a/internal/setup/sql_executor.go +++ b/internal/setup/sql_executor.go @@ -4,6 +4,7 @@ import ( "crypto/rand" "encoding/json" "fmt" + "github.com/TeaOSLab/EdgeAPI/internal/configs" "github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" @@ -14,9 +15,7 @@ import ( "github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/types" stringutil "github.com/iwind/TeaGo/utils/string" - "gopkg.in/yaml.v3" "io" - "os" "time" ) @@ -34,12 +33,7 @@ func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor { func NewSQLExecutorFromCmd() (*SQLExecutor, error) { // 执行SQL - var config = &dbs.Config{} - configData, err := os.ReadFile(Tea.ConfigFile("db.yaml")) - if err != nil { - return nil, err - } - err = yaml.Unmarshal(configData, config) + config, err := configs.LoadDBConfig() if err != nil { return nil, err } @@ -321,6 +315,9 @@ func (this *SQLExecutor) checkCluster(db *dbs.DB) error { models.SharedNodeClusterDAO = models.NewNodeClusterDAO() models.SharedNodeClusterDAO.Instance = db + models.SharedIPListDAO = models.NewIPListDAO() + models.SharedIPListDAO.Instance = db + policyId, err = models.SharedHTTPFirewallPolicyDAO.CreateDefaultFirewallPolicy(nil, "默认集群") if err != nil { return err