/
config.go
166 lines (148 loc) · 4.13 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package config
import (
"database/sql"
"fmt"
"log"
//Import the PostgreSQL driver
_ "github.com/lib/pq"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
"os"
"sync"
"time"
)
var once sync.Once
var goapisqlCache *cache.Cache
const (
KEY_DB_USERS = "db_users"
KEY_DB_URI_TEMPLATE = "db-uri-template"
KEY_PASSWORD = "password"
KEY_JWT_SECRET = "jwt-secret"
PREFIX_TOKEN = "Bearer "
DB_DRIVER_NAME = "postgres"
DB_ADMIN_ROLE = "db-admin-role"
)
//Init config file from the config paths
func InitConfigFile(path string) {
viper.SetConfigFile(path)
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %s", err))
}
}
//GetCache retrieves the *cache.Cache for your Application
func GetCache() *cache.Cache {
once.Do(func() {
goapisqlCache = cache.New(5*time.Minute, 10*time.Minute)
})
isFlushCache := os.Getenv("GOAPISQL_IS_CACHE_FLUSH")
if isFlushCache == "yes" {
goapisqlCache.Flush()
err := os.Setenv("GOAPISQL_IS_CACHE_FLUSH", "no")
if err != nil {
log.Println(err)
}
}
return goapisqlCache
}
//GetEnv retrieves the name of current environment
//default "prod"
func GetEnv() string {
envVal := os.Getenv("GOAPISQL_ENV")
if envVal == "" {
envVal = "prod"
}
return envVal
}
//GetNoCachedString returns without cache the value associated with the key as a string.
func GetNoCachedString(key string) string {
return viper.GetString(GetEnv() + "." + key)
}
//GetString retrieves the string value of the config variable named by the key.
//If string value have in cache then retrieves from cache
func GetString(key string) string {
var res string
cacheApp := GetCache()
resFromCache, ok := cacheApp.Get(key)
if ok {
res = resFromCache.(string)
} else {
res = GetNoCachedString(key)
cacheApp.Set(key, res, cache.NoExpiration)
}
return res
}
//GetNoCachedDbUsers retrieves map with iformation about DB user.
//example: map{postgres: {name: postgres, password: postgres_user_password}}
//Cache doesn't use
func GetNoCachedDbUsers() map[string]map[string]string {
resSlice := map[string]map[string]string{}
var userName string
resInterface := viper.Get(GetEnv() + "." + KEY_DB_USERS).([]interface{})
for _, item := range resInterface {
itemTyped := item.(map[string]interface{})
resMap := map[string]string{}
for key, value := range itemTyped {
valueTyped := value.(string)
if key == "name" {
userName = valueTyped
}
resMap[key] = valueTyped
}
resSlice[userName] = resMap
}
return resSlice
}
//GetDbUsers retrieves map with iformation about DB user.
//example: map{postgres: {name: postgres, password: postgres_user_password}}
//Cache use
func GetDbUsers() map[string]map[string]string {
var res map[string]map[string]string
cacheApp := GetCache()
resFromCache, ok := cacheApp.Get(KEY_DB_USERS)
if ok {
res = resFromCache.(map[string]map[string]string)
} else {
res = GetNoCachedDbUsers()
cacheApp.Set(KEY_DB_USERS, res, cache.NoExpiration)
}
return res
}
//GetNoCachedDbConnection retrieves *sql.DB
//Cache doesn't use
func GetNoCachedDbConnection(name string) *sql.DB {
dbUsers := GetNoCachedDbUsers()
connStr := fmt.Sprintf(GetNoCachedString(KEY_DB_URI_TEMPLATE), name, dbUsers[name][KEY_PASSWORD])
db, err := sql.Open(DB_DRIVER_NAME, connStr)
if err != nil {
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
db.SetMaxIdleConns(2)
//defer db.Close()
// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
return db
}
//GetDbConnection retrieves *sql.DB
//Cache use
func GetDbConnection(name string) *sql.DB {
var res *sql.DB
var keyDbConnection = "db_connection_" + name
cacheApp := GetCache()
resFromCache, ok := cacheApp.Get(keyDbConnection)
if ok {
res = resFromCache.(*sql.DB)
} else {
res = GetNoCachedDbConnection(name)
cacheApp.Set(keyDbConnection, res, cache.NoExpiration)
}
return res
}
//GetDbAdminConnection retrieves *sql.DB
//Cache use
func GetDbAdminConnection() *sql.DB {
return GetDbConnection(GetString(DB_ADMIN_ROLE))
}