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.

60 lines
1.2 KiB
Go

11 months ago
package initialize
import (
"fmt"
8 months ago
"github.com/redis/go-redis/v9"
"gold-shop/config"
11 months ago
"gopkg.in/yaml.v3"
"gorm.io/driver/mysql"
"gorm.io/gorm"
8 months ago
"gorm.io/gorm/logger"
11 months ago
"os"
7 months ago
"path/filepath"
11 months ago
)
7 months ago
const FilePath = "config.yaml"
11 months ago
7 months ago
func InitConfig(filePath string) *config.Config {
7 months ago
content, err := os.ReadFile(filePath)
11 months ago
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
}
7 months ago
func getCurrentDirectory() (string, error) {
path, err := filepath.Abs(os.Args[0])
if err != nil {
return "", err
}
dir := filepath.Dir(path)
return dir, nil
}
11 months ago
func InitDB(conf *config.Config) *gorm.DB {
8 months ago
var mysqlLogger logger.Interface
mysqlLogger = logger.Default.LogMode(logger.Info)
db, err := gorm.Open(mysql.Open(conf.Database.GetDsn()), &gorm.Config{
Logger: mysqlLogger,
})
11 months ago
if err != nil {
panic(err)
}
return db
}
8 months ago
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
})
}