github.com/condensat/bank-core@v0.1.0/database/helper.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package database 6 7 import ( 8 "fmt" 9 "io/ioutil" 10 "strings" 11 12 driver "github.com/go-sql-driver/mysql" 13 14 "github.com/jinzhu/gorm" 15 log "github.com/sirupsen/logrus" 16 ) 17 18 func connectMyql(host string, port int, user, pass, dbname string) *gorm.DB { 19 cfg := driver.Config{ 20 User: user, 21 Passwd: pass, 22 Net: "tcp", 23 Addr: fmt.Sprintf("%s:%d", host, port), 24 DBName: dbname, 25 AllowNativePasswords: true, 26 MultiStatements: true, 27 ParseTime: true, 28 } 29 30 db, err := gorm.Open("mysql", cfg.FormatDSN()) 31 if err != nil { 32 log. 33 WithError(err). 34 Panicln("Failed to open connection to database") 35 } 36 37 db.SingularTable(true) 38 39 return db 40 } 41 42 func secretOrPassword(secret string) string { 43 content, err := ioutil.ReadFile(secret) 44 if err != nil { 45 return secret 46 } 47 48 return strings.TrimRightFunc(string(content), func(c rune) bool { 49 return c == '\r' || c == '\n' 50 }) 51 }