refactor(server): 优化服务器启动脚本和数据库连接逻辑
修改run_server.sh脚本以支持环境变量和构建选项 移除mysql.go中无效的默认连接字符串并修复代码格式
This commit is contained in:
parent
558d834a4b
commit
874cbeca78
|
|
@ -1,5 +1,17 @@
|
|||
#!/usr/bin/env bash
|
||||
set -e
|
||||
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 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.
|
|
@ -1,17 +1,15 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"database/sql"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue