You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1004 B
Go

package initialize
import (
"fmt"
"github.com/redis/go-redis/v9"
"gopkg.in/yaml.v3"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"insure/config"
"os"
)
func InitConfig(filePath string) *config.Config {
content, err := os.ReadFile(filePath)
if err != nil {
fmt.Printf("err: %v\n", err)
panic(err)
}
var conf config.Config
if err := yaml.Unmarshal(content, &conf); err != nil {
fmt.Printf("err: %v\n", err)
panic(err)
}
return &conf
}
func InitDB(conf *config.Config) *gorm.DB {
var mysqlLogger logger.Interface
mysqlLogger = logger.Default.LogMode(logger.Info)
db, err := gorm.Open(mysql.Open(conf.Database.GetDsn()), &gorm.Config{
Logger: mysqlLogger,
})
if err != nil {
panic(err)
}
return db
}
func InitRedis(conf *config.Config) *redis.Client {
return redis.NewClient(&redis.Options{
Addr: conf.Redis.Host + ":" + conf.Redis.Port,
Password: conf.Redis.Password, // no password set
DB: conf.Redis.DB, // use default DB
})
}