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

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 ( import (
"fmt" "fmt"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs" "github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc" "github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/setup" "github.com/TeaOSLab/EdgeAdmin/internal/setup"
@@ -55,6 +56,12 @@ func (this *IndexAction) RunGet(params struct {
return return
} }
this.Data["systemName"] = config.AdminSystemName 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() this.Show()
} }

View File

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

View File

@@ -109,12 +109,6 @@ func (this *UpdateAction) RunPost(params struct {
Require("请输入连接数据库的用户名"). Require("请输入连接数据库的用户名").
Match(`^[\w\.-]+$`, "用户名中不能包含特殊字符") 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 dsn := params.Username + ":" + params.Password + "@tcp(" + params.Host + ":" + fmt.Sprintf("%d", params.Port) + ")/" + params.Database