mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-23 01:40:29 +08:00
可以在线修改数据库配置
This commit is contained in:
@@ -1,15 +1,87 @@
|
||||
package profile
|
||||
|
||||
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/go-yaml/yaml"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
this.Nav("", "", "index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
this.Data["error"] = ""
|
||||
|
||||
configFile := Tea.ConfigFile("api_db.yaml")
|
||||
data, err := ioutil.ReadFile(configFile)
|
||||
if err != nil {
|
||||
this.Data["error"] = "read config file failed: api_db.yaml: " + err.Error()
|
||||
this.Show()
|
||||
return
|
||||
}
|
||||
|
||||
config := &dbs.Config{}
|
||||
err = yaml.Unmarshal(data, config)
|
||||
if err != nil {
|
||||
this.Data["error"] = "parse config file failed: api_db.yaml: " + err.Error()
|
||||
this.Show()
|
||||
return
|
||||
}
|
||||
|
||||
if config.DBs == nil {
|
||||
this.Data["error"] = "can not find valid database config: api_db.yaml"
|
||||
this.Show()
|
||||
return
|
||||
}
|
||||
|
||||
dbConfig, ok := config.DBs[config.Default.DB]
|
||||
if !ok {
|
||||
this.Data["error"] = "can not find valid database config: api_db.yaml"
|
||||
this.Show()
|
||||
return
|
||||
}
|
||||
dsn := dbConfig.Dsn
|
||||
dsn = regexp.MustCompile(`tcp\((.+)\)`).ReplaceAllString(dsn, "$1")
|
||||
dsnURL, err := url.Parse("mysql://" + dsn)
|
||||
if err != nil {
|
||||
this.Data["error"] = "parse dsn failed: " + err.Error()
|
||||
this.Show()
|
||||
return
|
||||
}
|
||||
|
||||
host := dsnURL.Host
|
||||
port := "3306"
|
||||
index := strings.LastIndex(dsnURL.Host, ":")
|
||||
if index > 0 {
|
||||
host = dsnURL.Host[:index]
|
||||
port = dsnURL.Host[index+1:]
|
||||
}
|
||||
|
||||
password, _ := dsnURL.User.Password()
|
||||
if len(password) > 0 {
|
||||
password = strings.Repeat("*", len(password))
|
||||
}
|
||||
this.Data["dbConfig"] = maps.Map{
|
||||
"host": host,
|
||||
"port": port,
|
||||
"username": dsnURL.User.Username(),
|
||||
"password": password,
|
||||
"database": filepath.Base(dsnURL.Path),
|
||||
}
|
||||
|
||||
// TODO 测试连接
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user