-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
67 lines (60 loc) · 1.53 KB
/
database.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
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type DatabaseDialect string
const (
DialectMySQL DatabaseDialect = "mysql"
DialectPostgres DatabaseDialect = "postgres"
)
type DatabaseConfig struct {
Host string `env:"DATABASE_HOST"`
Port int `env:"DATABASE_PORT" envDefault:"3306"`
Username string `env:"DATABASE_USERNAME" envDefault:"root"`
Password string `env:"DATABASE_PASSWORD"`
Schema string `env:"DATABASE_SCHEMA"`
Debug bool `env:"DATABASE_DEBUG" envDefault:"false"`
LogLevel string `env:"DATABASE_LOG_LEVEL" envDefault:"info" enum:"silent,error,warn,info"`
Dialect string `env:"DATABASE_DIALECT"`
}
func (d DatabaseConfig) GetDialector() (gorm.Dialector, error) {
switch d.Dialect {
case string(DialectMySQL):
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
d.Username,
d.Password,
d.Host,
d.Port,
d.Schema,
)
return mysql.Open(dsn), nil
case string(DialectPostgres):
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Jakarta",
d.Host,
d.Username,
d.Password,
d.Schema,
d.Port,
)
return postgres.Open(dsn), nil
default:
return nil, fmt.Errorf("unsupported database dialect: %s", d.Dialect)
}
}
func (d DatabaseConfig) GetLogLevel() logger.LogLevel {
switch d.LogLevel {
case "error":
return logger.Error
case "warn":
return logger.Warn
case "info":
return logger.Info
default:
return logger.Silent
}
}