From 72806500690ab912afae951f65114fd66460a695 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 28 Aug 2026 17:02:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=B0=86=20MySQL=20DSN=20=E6=8B=86=E5=88=86=E4=B8=BA?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E5=AD=97=E6=AE=B5=EF=BC=88db=5Fname/username?= =?UTF-8?q?/password/host/port=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++-- config/config.go | 29 +++++++++++++++++++++++------ config/config_test.go | 14 ++++++++++++++ install_linux.sh | 6 +++++- models/db.go | 27 ++++++++++++++++++++++++--- 5 files changed, 76 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1ab2c66..3475e3c 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,11 @@ go run . # config.yaml database: type: sqlite # sqlite(默认)或 mysql - dsn: "" # MySQL 连接串,sqlite 模式下忽略 + db_name: "" # MySQL 数据库名,sqlite 模式下忽略 + username: "" # MySQL 用户名,sqlite 模式下忽略 + password: "" # MySQL 密码,sqlite 模式下忽略 + host: "" # MySQL IP 或主机名,sqlite 模式下忽略 + port: "" # MySQL 端口,sqlite 模式下忽略 web: port: "8080" # Web 服务端口,"" 或 "0" 可只启用 socket socket: "" # unix socket 路径(Linux 部署推荐,见 install_linux.sh) @@ -78,7 +82,11 @@ secret: <自动生成> # Session 加密密钥;缺失时拒绝启动 ```yaml database: type: mysql - dsn: user:password@tcp(127.0.0.1:3306)/blog_go?charset=utf8mb4&parseTime=True&loc=Local + db_name: blog_go + username: user + password: password + host: 127.0.0.1 + port: "3306" web: port: "8080" ``` diff --git a/config/config.go b/config/config.go index 134e8dd..ecc0d09 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "crypto/rand" "encoding/hex" + "fmt" "log" "os" "path/filepath" @@ -21,8 +22,20 @@ type Config struct { // DatabaseConfig 保存数据库相关配置。 type DatabaseConfig struct { - Type string `yaml:"type"` // "sqlite"(默认)或 "mysql" - DSN string `yaml:"dsn"` // MySQL 连接字符串(type 为 "mysql" 时必填) + Type string `yaml:"type"` // "sqlite"(默认)或 "mysql" + DBName string `yaml:"db_name"` // 数据库名(type 为 "mysql" 时必填) + Username string `yaml:"username"` // 用户名(type 为 "mysql" 时必填) + Password string `yaml:"password"` // 密码(type 为 "mysql" 时必填) + Host string `yaml:"host"` // IP 或主机名(type 为 "mysql" 时必填) + Port string `yaml:"port"` // 端口(type 为 "mysql" 时必填) +} + +// MySQLDSN 根据拆分字段构建 MySQL 连接字符串。 +func (d *DatabaseConfig) MySQLDSN() string { + return fmt.Sprintf( + "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", + d.Username, d.Password, d.Host, d.Port, d.DBName, + ) } // WebConfig 保存 Web 服务器监听配置。 @@ -41,8 +54,8 @@ var defaultTrustedProxies = []string{"127.0.0.1", "::1"} const defaultPort = "8080" -// mysqlExampleDSN 会写入新建的配置文件,作为参考示例。 -const mysqlExampleDSN = "user:password@tcp(127.0.0.1:3306)/blog_go?charset=utf8mb4&parseTime=True&loc=Local" +// mysqlExampleDBName 作为示例写入新建的配置文件。 +const mysqlExampleDBName = "blog_go" // getConfigPath 返回按操作系统区分的配置目录和配置文件路径。 func getConfigPath() (dir, file string) { @@ -107,8 +120,12 @@ func LoadConfig(customPath string) *Config { cfg := &Config{ Database: DatabaseConfig{ - Type: "sqlite", - DSN: mysqlExampleDSN, + Type: "sqlite", + DBName: mysqlExampleDBName, + Username: "user", + Password: "password", + Host: "127.0.0.1", + Port: "3306", }, Web: WebConfig{ Port: defaultPort, diff --git a/config/config_test.go b/config/config_test.go index 586067f..e94080a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -20,3 +20,17 @@ func TestConfigFileCreatedNotWorldReadable(t *testing.T) { t.Fatalf("config perms = %v, want 0640", perm) } } + +func TestMySQLDSN(t *testing.T) { + d := &DatabaseConfig{ + DBName: "blog_go", + Username: "user", + Password: "pass:word", + Host: "127.0.0.1", + Port: "3306", + } + want := "user:pass:word@tcp(127.0.0.1:3306)/blog_go?charset=utf8mb4&parseTime=True&loc=Local" + if got := d.MySQLDSN(); got != want { + t.Fatalf("MySQLDSN() = %q, want %q", got, want) + } +} diff --git a/install_linux.sh b/install_linux.sh index 18fb236..9bad507 100755 --- a/install_linux.sh +++ b/install_linux.sh @@ -53,7 +53,11 @@ if [[ ! -f "${CONFIG_DIR}/config.yaml" ]]; then cat > "${CONFIG_DIR}/config.yaml" <