-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
155 lines (140 loc) · 3.45 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
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"os"
"regexp"
"strings"
"time"
)
const (
LowerCamel = "lowerCamel"
UpperCamel = "upperCamel"
Underscore = "underscore"
)
type ConfigOptions struct {
Server ServerOptions `yaml:"server"`
Log LogOptions `yaml:"log"`
DBs []DBOptions `yaml:"dbs"`
}
type ServerOptions struct {
Addr string `yaml:"addr"`
Port int `yaml:"port"`
Auth bool `yaml:"auth"`
Accounts []AccountOptions `yaml:"accounts"`
TLS bool `yaml:"tls"`
CertFile string `yaml:"cert-file"`
KeyFile string `yaml:"key-file"`
Debug bool `yaml:"debug"`
}
type AccountOptions struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type LogOptions struct {
Path string `yaml:"path"`
Size int `yaml:"size"`
Age int `yaml:"age"`
Backups int `yaml:"backups"`
}
type DBOptions struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
DSN string `yaml:"dsn"`
Format string `yaml:"format"`
Groups []GroupOptions `yaml:"groups"`
}
type GroupOptions struct {
Name string `yaml:"name"`
Format string `yaml:"format"`
APIs []APIOptions `yaml:"apis"`
}
type APIOptions struct {
Name string `yaml:"name"`
Sql string `yaml:"sql"`
Format string `yaml:"format"`
Hide []string
Show []string
Params []string
Store *Store
}
var config ConfigOptions
func Config() ConfigOptions {
return config
}
func InitConfig() {
//加载客户端配置
configBytes, err := os.ReadFile("./config.yaml")
if err != nil {
fmt.Println("config error:", err)
time.Sleep(3 * time.Second)
panic(err)
}
err = yaml.Unmarshal(configBytes, &config)
if err != nil {
fmt.Println("config error:", err)
time.Sleep(3 * time.Second)
panic(err)
}
for _, db := range Config().DBs {
for i := 0; i < len(db.Groups); i++ {
for j := 0; j < len(db.Groups[i].APIs); j++ {
db.Groups[i].APIs[j].Params = matchParams(db.Groups[i].APIs[j].Sql)
if len(db.Groups[i].APIs[j].Format) > 0 {
continue
}
if len(db.Groups[i].Format) > 0 {
db.Groups[i].APIs[j].Format = db.Groups[i].Format
continue
}
db.Groups[i].APIs[j].Format = db.Format
}
}
}
if Config().Server.Debug {
for _, db := range Config().DBs {
fmt.Println("[DEBUG]", "> DB:", db.Name)
for _, group := range db.Groups {
fmt.Println("[DEBUG]", ">>> GROUP:", group.Name)
for _, api := range group.APIs {
fmt.Println("[DEBUG]", ">>>>> API:", api.Name)
fmt.Println("[DEBUG]", ">>>>>>> URI:", fmt.Sprintf("/%s/%s/%s", db.Name, group.Name, api.Name))
fmt.Println("[DEBUG]", ">>>>>>> SQL:", api.Sql)
fmt.Println("[DEBUG]", ">>>>>>> PARAMS:", api.Params)
}
}
}
}
}
func matchParams(src string) []string {
reg := regexp.MustCompile("\\{(.*?)}")
arr := reg.FindAllString(src, -1)
var r []string
m := make(map[string]bool)
for _, v := range arr {
if strings.Contains(v, "{#") || strings.Contains(v, "{/") {
continue
}
v = strings.ReplaceAll(v, "{", "")
v = strings.ReplaceAll(v, "}", "")
if m[v] {
continue
}
r = append(r, v)
m[v] = true
}
for _, v := range arr {
if !strings.Contains(v, "{#") && !strings.Contains(v, "{/") {
continue
}
v = strings.ReplaceAll(v, "{#", "")
v = strings.ReplaceAll(v, "{/", "")
v = strings.ReplaceAll(v, "}", "")
if m[v] {
continue
}
r = append(r, v)
m[v] = true
}
return r
}