mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-02 22:10:26 +08:00
简化API节点的数据库配置(db.yaml)
This commit is contained in:
3
build/configs/.gitignore
vendored
3
build/configs/.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
api.yaml
|
api.yaml
|
||||||
db.yaml
|
db.yaml
|
||||||
|
.db.yaml
|
||||||
@@ -1,16 +1,4 @@
|
|||||||
default:
|
user: root
|
||||||
db: "prod"
|
password: 123456
|
||||||
prefix: ""
|
host: 127.0.0.1:3306
|
||||||
|
database: db_edge
|
||||||
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" ]
|
|
||||||
|
|
||||||
@@ -28,56 +28,63 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 候选文件
|
// 候选文件
|
||||||
localFile := Tea.ConfigFile("api.yaml")
|
var config = &APIConfig{}
|
||||||
isFromLocal := false
|
{
|
||||||
paths := []string{localFile}
|
var localFile = Tea.ConfigFile("api.yaml")
|
||||||
homeDir, homeErr := os.UserHomeDir()
|
var isFromLocal = false
|
||||||
if homeErr == nil {
|
var paths = []string{localFile}
|
||||||
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/api.yaml")
|
homeDir, homeErr := os.UserHomeDir()
|
||||||
}
|
if homeErr == nil {
|
||||||
paths = append(paths, "/etc/"+teaconst.ProcessName+"/api.yaml")
|
paths = append(paths, homeDir+"/."+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
|
|
||||||
}
|
}
|
||||||
}
|
paths = append(paths, "/etc/"+teaconst.ProcessName+"/api.yaml")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析内容
|
// 依次检查文件
|
||||||
config := &APIConfig{}
|
var data []byte
|
||||||
err = yaml.Unmarshal(data, config)
|
var err error
|
||||||
if err != nil {
|
var firstErr error
|
||||||
return nil, err
|
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 {
|
// 解析内容
|
||||||
// 恢复文件
|
err = yaml.Unmarshal(data, config)
|
||||||
_ = os.WriteFile(localFile, data, 0666)
|
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)
|
_, err := os.Stat(dbConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
paths := []string{}
|
var paths = []string{}
|
||||||
homeDir, homeErr := os.UserHomeDir()
|
homeDir, homeErr := os.UserHomeDir()
|
||||||
if homeErr == nil {
|
if homeErr == nil {
|
||||||
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/db.yaml")
|
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/db.yaml")
|
||||||
}
|
}
|
||||||
paths = append(paths, "/etc/"+teaconst.ProcessName+"/db.yaml")
|
paths = append(paths, "/etc/"+teaconst.ProcessName+"/db.yaml")
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
_, err := os.Stat(path)
|
_, err = os.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
data, err := os.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err == nil {
|
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()
|
homeDir, _ := os.UserHomeDir()
|
||||||
backupDirs := []string{"/etc/edge-api"}
|
var backupDirs = []string{"/etc/edge-api"}
|
||||||
if len(homeDir) > 0 {
|
if len(homeDir) > 0 {
|
||||||
backupDirs = append(backupDirs, homeDir+"/.edge-api")
|
backupDirs = append(backupDirs, homeDir+"/.edge-api")
|
||||||
}
|
}
|
||||||
@@ -135,7 +142,7 @@ func (this *APIConfig) WriteFile(path string) error {
|
|||||||
|
|
||||||
// ResetAPIConfig 重置配置
|
// ResetAPIConfig 重置配置
|
||||||
func ResetAPIConfig() error {
|
func ResetAPIConfig() error {
|
||||||
for _, filename := range []string{"api.yaml", "db.yaml"} {
|
for _, filename := range []string{"api.yaml", "db.yaml", ".db.yaml"} {
|
||||||
// 重置 configs/api.yaml
|
// 重置 configs/api.yaml
|
||||||
{
|
{
|
||||||
var configFile = Tea.ConfigFile(filename)
|
var configFile = Tea.ConfigFile(filename)
|
||||||
|
|||||||
25
internal/configs/db_config.go
Normal file
25
internal/configs/db_config.go
Normal file
@@ -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'")
|
||||||
|
}
|
||||||
58
internal/configs/simple_db_config.go
Normal file
58
internal/configs/simple_db_config.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -29,7 +29,6 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials"
|
"google.golang.org/grpc/credentials"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
@@ -300,6 +299,22 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
|
|||||||
func (this *APINode) checkDB() error {
|
func (this *APINode) checkDB() error {
|
||||||
logs.Println("[API_NODE]checking database connection ...")
|
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
|
// lookup mysqld_safe process
|
||||||
go dbutils.FindMySQLPathAndRemember()
|
go dbutils.FindMySQLPathAndRemember()
|
||||||
|
|
||||||
@@ -356,12 +371,7 @@ func (this *APINode) autoUpgrade() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 执行SQL
|
// 执行SQL
|
||||||
var config = &dbs.Config{}
|
config, err := configs.LoadDBConfig()
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("decode database config failed: %w", err)
|
return fmt.Errorf("decode database config failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/cmd"
|
"github.com/iwind/TeaGo/cmd"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -87,12 +85,7 @@ func (this *Setup) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 执行SQL
|
// 执行SQL
|
||||||
var config = &dbs.Config{}
|
config, err := configs.LoadDBConfig()
|
||||||
configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = yaml.Unmarshal(configData, config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -144,7 +137,7 @@ func (this *Setup) Run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if apiNodeId == 0 {
|
if apiNodeId == 0 {
|
||||||
addr := &serverconfigs.NetworkAddressConfig{
|
var addr = &serverconfigs.NetworkAddressConfig{
|
||||||
Protocol: serverconfigs.Protocol(this.config.APINodeProtocol),
|
Protocol: serverconfigs.Protocol(this.config.APINodeProtocol),
|
||||||
Host: this.config.APINodeHost,
|
Host: this.config.APINodeHost,
|
||||||
PortRange: strconv.Itoa(this.config.APINodePort),
|
PortRange: strconv.Itoa(this.config.APINodePort),
|
||||||
@@ -172,7 +165,7 @@ func (this *Setup) Run() error {
|
|||||||
}
|
}
|
||||||
if this.config.APINodeProtocol == "https" {
|
if this.config.APINodeProtocol == "https" {
|
||||||
// TODO 如果在安装过程中开启了HTTPS,需要同时上传SSL证书
|
// TODO 如果在安装过程中开启了HTTPS,需要同时上传SSL证书
|
||||||
httpsConfig := &serverconfigs.HTTPSProtocolConfig{}
|
var httpsConfig = &serverconfigs.HTTPSProtocolConfig{}
|
||||||
httpsConfig.IsOn = true
|
httpsConfig.IsOn = true
|
||||||
httpsConfig.Listen = []*serverconfigs.NetworkAddressConfig{
|
httpsConfig.Listen = []*serverconfigs.NetworkAddressConfig{
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
@@ -14,9 +15,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,12 +33,7 @@ func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
|
|||||||
|
|
||||||
func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
|
func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
|
||||||
// 执行SQL
|
// 执行SQL
|
||||||
var config = &dbs.Config{}
|
config, err := configs.LoadDBConfig()
|
||||||
configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = yaml.Unmarshal(configData, config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -321,6 +315,9 @@ func (this *SQLExecutor) checkCluster(db *dbs.DB) error {
|
|||||||
models.SharedNodeClusterDAO = models.NewNodeClusterDAO()
|
models.SharedNodeClusterDAO = models.NewNodeClusterDAO()
|
||||||
models.SharedNodeClusterDAO.Instance = db
|
models.SharedNodeClusterDAO.Instance = db
|
||||||
|
|
||||||
|
models.SharedIPListDAO = models.NewIPListDAO()
|
||||||
|
models.SharedIPListDAO.Instance = db
|
||||||
|
|
||||||
policyId, err = models.SharedHTTPFirewallPolicyDAO.CreateDefaultFirewallPolicy(nil, "默认集群")
|
policyId, err = models.SharedHTTPFirewallPolicyDAO.CreateDefaultFirewallPolicy(nil, "默认集群")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user