-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
54 lines (43 loc) · 1.03 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"path/filepath"
"io/ioutil"
"gopkg.in/yaml.v2"
"fmt"
"github.com/jinzhu/gorm"
)
const (
configPath = "./config/config.yaml"
)
var config configuration
type dbConfig struct {
Type string `yaml:"type"`
Host string `yaml:"host"`
Port string `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
DBName string `yaml:"dbname"`
}
type configuration struct {
Default dbConfig `yaml:"default"`
}
func init() {
var fileName string
var yamlFile []byte
var err error
if fileName, err = filepath.Abs(configPath); err != nil {
panic(err)
}
if yamlFile, err = ioutil.ReadFile(fileName); err != nil {
panic(err)
}
config = configuration{}
if err = yaml.Unmarshal(yamlFile, &config); err != nil {
panic(err)
}
}
func openConnection() (*gorm.DB, error) {
conf := config.Default
connectionString := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName)
return gorm.Open(conf.Type, connectionString)
}