Make MySQL address changeable #156

This commit is contained in:
Radhi Fadlillah 2019-09-22 08:25:34 +07:00
parent 5b44801b87
commit df98c281d3
2 changed files with 5 additions and 3 deletions

View file

@ -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

View file

@ -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)