数据库配置中支持特殊字符

This commit is contained in:
GoEdgeLab
2020-11-24 09:50:22 +08:00
parent 688c5742bc
commit 8e97b5f512
3 changed files with 17 additions and 19 deletions

View File

@@ -2,6 +2,7 @@ package index
import (
"fmt"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/setup"
@@ -55,6 +56,12 @@ func (this *IndexAction) RunGet(params struct {
return
}
this.Data["systemName"] = config.AdminSystemName
this.Data["showVersion"] = config.ShowVersion
if len(config.Version) > 0 {
this.Data["version"] = config.Version
} else {
this.Data["version"] = teaconst.Version
}
this.Show()
}

View File

@@ -2,14 +2,12 @@ package profile
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/go-sql-driver/mysql"
"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"
)
@@ -52,32 +50,31 @@ func (this *IndexAction) RunGet(params struct{}) {
break
}
dsn := dbConfig.Dsn
dsn = regexp.MustCompile(`tcp\((.+)\)`).ReplaceAllString(dsn, "$1")
dsnURL, err := url.Parse("mysql://" + dsn)
cfg, err := mysql.ParseDSN(dsn)
if err != nil {
this.Data["error"] = "parse dsn failed: " + err.Error()
this.Data["error"] = "parse dsn error: " + err.Error()
this.Show()
return
}
host := dsnURL.Host
host := cfg.Addr
port := "3306"
index := strings.LastIndex(dsnURL.Host, ":")
index := strings.LastIndex(host, ":")
if index > 0 {
host = dsnURL.Host[:index]
port = dsnURL.Host[index+1:]
port = host[index+1:]
host = host[:index]
}
password, _ := dsnURL.User.Password()
password := cfg.Passwd
if len(password) > 0 {
password = strings.Repeat("*", len(password))
}
this.Data["dbConfig"] = maps.Map{
"host": host,
"port": port,
"username": dsnURL.User.Username(),
"username": cfg.User,
"password": password,
"database": filepath.Base(dsnURL.Path),
"database": cfg.DBName,
}
// TODO 测试连接

View File

@@ -109,12 +109,6 @@ func (this *UpdateAction) RunPost(params struct {
Require("请输入连接数据库的用户名").
Match(`^[\w\.-]+$`, "用户名中不能包含特殊字符")
if len(params.Password) > 0 {
params.Must.
Field("password", params.Password).
Match(`^[\w\.-]+$`, "密码中不能包含特殊字符")
}
// 保存
dsn := params.Username + ":" + params.Password + "@tcp(" + params.Host + ":" + fmt.Sprintf("%d", params.Port) + ")/" + params.Database