mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +08:00 
			
		
		
		
	数据库配置中支持特殊字符
This commit is contained in:
		@@ -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 测试连接
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user