mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-03 12:20:28 +08:00
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package profile
|
|
|
|
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("", "", "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"] = "no database configured in config file: api_db.yaml"
|
|
this.Show()
|
|
return
|
|
}
|
|
|
|
var dbConfig *dbs.DBConfig
|
|
for _, db := range config.DBs {
|
|
dbConfig = db
|
|
break
|
|
}
|
|
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()
|
|
}
|