-
Notifications
You must be signed in to change notification settings - Fork 1
/
configs.go
192 lines (164 loc) · 4.69 KB
/
configs.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"strings"
"os"
"fmt"
"encoding/json"
)
type TableConfig struct {
Name string
IsManyToMany bool
Label string
UniqueColumns []string
SkipColumns []string
}
type RelationConfig struct {
Label string
Table string
ReferenceTable string
Properties map[string]string
}
type GraphConfig struct {
Host string
Port string
Username string
Password string
}
type MysqlConfig struct {
Host string
Port string
DbName string
Username string
Password string
}
type DatabaseConfig struct {
Graph GraphConfig
Mysql MysqlConfig
}
type Configuration struct {
TablesConfig []*TableConfig
RelationsConfig []*RelationConfig
Limit int
SkipTables []string
Database *DatabaseConfig
Debug bool
}
type tempConfig struct {
TablesConfig []TableConfig
RelationsConfig []RelationConfig
QueryLimit int
SkipTables []string
Database DatabaseConfig
Debug bool
}
var SystemConfig *Configuration
func (configuration *Configuration) GetTableConfig(table *Table) *TableConfig {
for _, config := range configuration.TablesConfig {
if config.Name == table.name {
return config
}
}
panic("Config Not Found !")
}
func (configuration *Configuration) GetRelationConfig(foreignKey *ForeignKey) *RelationConfig {
for _, config := range configuration.RelationsConfig {
if config.Table == foreignKey.Table.name && config.ReferenceTable == foreignKey.ReferenceTable.name {
return config
}
}
return &RelationConfig{}
}
func InitConfiguration() {
SystemConfig = &Configuration{TablesConfig: []*TableConfig{}, RelationsConfig: []*RelationConfig{}}
loadConfig()
}
func (configuration *Configuration) InitDbConfig(tables map[string]*Table) {
for _, table := range tables {
config := configuration.findTableConfig(table.name)
config.setupConfig(table)
}
}
func loadConfig() {
adr, _ := os.Getwd()
file, err := os.Open(adr + "/conf.json")
if err != nil {
fmt.Println("Cannot load Config File ! \n" + err.Error())
} else {
decoder := json.NewDecoder(file)
temp := tempConfig{}
err := decoder.Decode(&temp)
if err != nil {
panic("Invalid Config file! => error:" + err.Error())
}
for _, tempTable := range temp.TablesConfig {
SystemConfig.TablesConfig = append(SystemConfig.TablesConfig, &TableConfig{
Name:tempTable.Name,
UniqueColumns:tempTable.UniqueColumns,
IsManyToMany:tempTable.IsManyToMany,
Label:tempTable.Label,
SkipColumns:tempTable.SkipColumns,
})
}
for _, tempRelation := range temp.RelationsConfig {
SystemConfig.RelationsConfig = append(SystemConfig.RelationsConfig, &RelationConfig{
Table:tempRelation.Table,
Label:tempRelation.Label,
ReferenceTable:tempRelation.ReferenceTable,
Properties:tempRelation.Properties,
})
}
if temp.QueryLimit == 0 {
SystemConfig.Limit = 20
} else {
SystemConfig.Limit = temp.QueryLimit
}
SystemConfig.SkipTables = temp.SkipTables
SystemConfig.Database = loadEnvDbConfig(&temp.Database)
SystemConfig.Debug = temp.Debug
}
}
func loadEnvDbConfig(dbConfig *DatabaseConfig) *DatabaseConfig {
dbConfig.Mysql.Host = getEnvDefault("_MYSQL_HOST", dbConfig.Mysql.Host)
dbConfig.Mysql.Port = getEnvDefault("_MYSQL_PORT", dbConfig.Mysql.Port)
dbConfig.Mysql.DbName = getEnvDefault("_MYSQL_DB", dbConfig.Mysql.DbName)
dbConfig.Mysql.Username = getEnvDefault("_MYSQL_USER", dbConfig.Mysql.Username)
dbConfig.Mysql.Password = getEnvDefault("_MYSQL_PASS", dbConfig.Mysql.Password)
dbConfig.Graph.Host = getEnvDefault("_NEO_HOST", dbConfig.Graph.Host)
dbConfig.Graph.Port = getEnvDefault("_NEO_PORT", dbConfig.Graph.Port)
dbConfig.Graph.Username = getEnvDefault("_NEO_USER", dbConfig.Graph.Username)
dbConfig.Graph.Password = getEnvDefault("_NEO_PASS", dbConfig.Graph.Password)
return dbConfig
}
func getEnvDefault(key string, default_data string) string {
if env := os.Getenv(key); env != "" {
return env
}
return default_data
}
func (configuration *Configuration) findTableConfig(table string) *TableConfig {
for _, config := range configuration.TablesConfig {
if config.Name == table {
return config
}
}
config := &TableConfig{}
configuration.TablesConfig = append(configuration.TablesConfig, config)
return config
}
func (tableConfig *TableConfig) setupConfig(table *Table) {
if tableConfig.Name == "" {
tableConfig.Name = table.name
}
if tableConfig.Label == "" {
tableConfig.Label = strings.ToUpper(table.name)
}
if len(tableConfig.UniqueColumns) == 0 {
unique := []string{}
for _, column := range table.columns {
if column.IsPrimary() || column.IsUnique() {
unique = append(unique, column.Field)
}
}
tableConfig.UniqueColumns = unique
}
}