From df98c281d3d742f83e21d11b0eb0d50b6366173b Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Sun, 22 Sep 2019 08:25:34 +0700 Subject: [PATCH] Make MySQL address changeable #156 --- internal/cmd/root.go | 5 ++++- internal/database/mysql.go | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 5a0a7f9..5fdb74d 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" fp "path/filepath" @@ -105,8 +106,10 @@ func openDatabase() (database.DB, error) { user, _ := os.LookupEnv("SHIORI_MYSQL_USER") password, _ := os.LookupEnv("SHIORI_MYSQL_PASS") dbName, _ := os.LookupEnv("SHIORI_MYSQL_NAME") + dbAddress, _ := os.LookupEnv("SHIORI_MYSQL_ADDRESS") - return database.OpenMySQLDatabase(user, password, dbName) + connString := fmt.Sprintf("%s:%s@%s/%s", user, password, dbAddress, dbName) + return database.OpenMySQLDatabase(connString) } // If not, just uses SQLite diff --git a/internal/database/mysql.go b/internal/database/mysql.go index 4f9da58..bf3bf5f 100644 --- a/internal/database/mysql.go +++ b/internal/database/mysql.go @@ -18,9 +18,8 @@ type MySQLDatabase struct { } // OpenMySQLDatabase creates and opens connection to a MySQL Database. -func OpenMySQLDatabase(username, password, dbName string) (mysqlDB *MySQLDatabase, err error) { +func OpenMySQLDatabase(connString string) (mysqlDB *MySQLDatabase, err error) { // Open database and start transaction - connString := fmt.Sprintf("%s:%s@/%s", username, password, dbName) db := sqlx.MustConnect("mysql", connString) db.SetMaxOpenConns(100) db.SetConnMaxLifetime(time.Second) // in case mysql client has longer timeout (driver issue #674)