Created
December 12, 2020 04:56
-
-
Save krittawatcode/28be5ab38665d64e55a178bd14d54e53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package database | |
import ( | |
"fmt" | |
"os" | |
"github.com/jinzhu/gorm" | |
) | |
// DB is a global var for connect DB | |
var DB *gorm.DB | |
// DBConfig represents db configuration | |
type DBConfig struct { | |
Host string | |
Port int | |
User string | |
DBName string | |
Password string | |
} | |
// BuildDBConfig use for building DB config | |
func BuildDBConfig() *DBConfig { | |
dbConfig := DBConfig{ | |
Host: "0.0.0.0", | |
Port: 3306, | |
User: "root", | |
DBName: "todo", | |
Password: os.Getenv("DB_PASSWORD"), | |
} | |
return &dbConfig | |
} | |
// DbURL use for create DB connection URL | |
func DbURL(dbConfig *DBConfig) string { | |
return fmt.Sprintf( | |
"%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", | |
dbConfig.User, | |
dbConfig.Password, | |
dbConfig.Host, | |
dbConfig.Port, | |
dbConfig.DBName, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment