refactor(server): 优化服务器启动脚本和数据库连接逻辑

修改run_server.sh脚本以支持环境变量和构建选项
移除mysql.go中无效的默认连接字符串并修复代码格式
This commit is contained in:
zhouyonggao 2025-11-25 11:06:09 +08:00
parent 558d834a4b
commit 874cbeca78
3 changed files with 21 additions and 11 deletions

View File

@ -1,5 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
cd "$(dirname "$0")/.."/server cd "$(dirname "$0")/.."/server
set -a
[ -f .env.local ] && source .env.local
set +a
if [ "${SKIP_YMT:-0}" = "1" ]; then
unset YMT_DB_USER YMT_DB_PASSWORD YMT_DB_HOST YMT_DB_PORT YMT_DB_NAME
fi
go mod tidy go mod tidy
go run ./cmd/server if [ "${BUILD:-0}" = "1" ]; then
mkdir -p bin
go build -o bin/marketing-data-server ./cmd/server
PORT=${PORT:-8077} ./bin/marketing-data-server
else
go run ./cmd/server
fi

Binary file not shown.

View File

@ -2,13 +2,11 @@ package db
import ( import (
"database/sql" "database/sql"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
func ConnectMySQL(dsn string) (*sql.DB, error) { func ConnectMySQL(dsn string) (*sql.DB, error) {
if dsn == "" {
return sql.Open("mysql", "invalid:invalid@tcp(localhost:3306)/invalid")
}
db, err := sql.Open("mysql", dsn) db, err := sql.Open("mysql", dsn)
if err != nil { if err != nil {
return nil, err return nil, err