Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Created June 24, 2021 11:37
Show Gist options
  • Save dipeshhkc/47fef5a2a912125c09e153f92bc3f7c2 to your computer and use it in GitHub Desktop.
Save dipeshhkc/47fef5a2a912125c09e153f92bc3f7c2 to your computer and use it in GitHub Desktop.
//DBConnection -> return db instance
func DBConnection() (*gorm.DB, error) {
USER := "root"
PASS := "root"
HOST := "localhost"
PORT := "3306"
DBNAME := "casbin-golang"
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Info, // Log level
Colorful: true, // Disable color
},
)
url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", USER, PASS, HOST, PORT, DBNAME)
return gorm.Open(mysql.Open(url), &gorm.Config{Logger: newLogger})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment