struct
This commit is contained in:
45
config/config.go
Normal file
45
config/config.go
Normal file
@ -0,0 +1,45 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"gorm.io/driver/postgres"
|
||||
)
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
type AppConfig struct {
|
||||
DB struct {
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Name string `yaml:"name"`
|
||||
} `yaml:"database"`
|
||||
}
|
||||
|
||||
func InitConfig() {
|
||||
viper.AddConfigPath("./")
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
panic("Failed to read config: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func ConnectDB() {
|
||||
dsn := "host=" + viper.GetString("database.host") +
|
||||
" user=" + viper.GetString("database.user") +
|
||||
" password=" + viper.GetString("database.password") +
|
||||
" dbname=" + viper.GetString("database.name") +
|
||||
" port=" + viper.GetString("database.port") +
|
||||
" sslmode=disable"
|
||||
|
||||
var err error
|
||||
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("Failed to connect database: " + err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user