-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
207 lines (170 loc) · 4.19 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"flag"
"fmt"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/unrolled/render"
"log"
"metadb/demodata"
"net/http"
)
var format = render.New()
type DBFieldType int
const (
_ DBFieldType = iota
UnknownField
NumberField
StringField
DateField
BoolField
ReferenceField
PickListField
)
var fieldTypeNames = map[DBFieldType]string{
NumberField: "number",
StringField: "text",
DateField: "date",
BoolField: "bool",
ReferenceField: "reference",
PickListField: "picklist",
}
var backFieldTypeNames = map[string]DBFieldType{}
func init() {
for key, value := range fieldTypeNames {
backFieldTypeNames[value] = key
}
}
type Pick struct {
ID string `json:"id"`
Value string `json:"value"`
}
type PickList struct {
ID string
Options []Pick
}
type DBField struct {
ID string `json:"id"`
Name string `json:"name"`
Filter bool `yaml:",omitempty" json:"filter"`
Edit bool `yaml:",omitempty" json:"edit"`
TypeName string `yaml:"type" json:"type"`
Ref string `json:"ref"`
Type DBFieldType `yaml:"-" json:"-"`
IsKey bool `yaml:"key,omitempty" json:"key"`
IsLabel bool `yaml:"label,omitempty" json:"show"`
}
type Relation struct {
To string `json:"to"`
}
func (d *DBField) GetTypeName() string {
return fieldTypeNames[d.Type]
}
type DBReference struct {
ID int `json:"id"`
Target string `json:"target"`
Source string `json:"source"`
Name string `json:"name"`
Field *DBField `json:"-"`
}
type DBObject struct {
ID string `json:"id"`
Name string `json:"name"`
Fields []DBField `json:"data"`
Key string `yaml:"-" json:"-"`
Label string `yaml:"-" json:"-"`
References []DBReference `yaml:"-" json:"refs,omitempty"`
}
type DBInfo struct {
Models []DBObject
Picklists []PickList
}
type RelationDetectionStrategy interface {
IsRelation(name string, pull map[string]*DBObject) bool
}
type MatchByName struct{}
func (m MatchByName) IsRelation(name string, pull map[string]*DBObject) bool {
return false
}
type MySQLField struct {
Field string `db:"Field"`
Type string `db:"Type"`
Default *string `db:"Default"`
Key string `db:"Key"`
Null string `db:"Null"`
Extra string `db:"Extra"`
}
var saveScheme = flag.String("save", "", "import scheme from DB and save to the file")
var loadScheme = flag.String("scheme", "", "path to file with scheme config")
var initDemo = flag.Bool("demodata", false, "fill DB with demo data")
var pull map[string]*DBObject
var picks map[string]*[]Pick
func main() {
//config
flag.Parse()
Config.LoadFromFile("./config.yml")
db, err := sqlx.Connect("mysql", Config.DataDBSourceName())
if err != nil {
log.Fatal(err)
}
appDB, err := sqlx.Connect("mysql", Config.AppDBSourceName())
if err != nil {
log.Fatal(err)
}
err = InitDB(appDB)
if err != nil {
log.Fatal(err)
}
if *initDemo {
err := demodata.InitDB(db)
if err != nil {
log.Fatal(err)
}
err = demodata.InitReports(appDB)
if err != nil {
log.Fatal(err)
}
if *loadScheme == "" && *saveScheme == ""{
return
}
}
if *saveScheme != "" {
pull := readFromDB(db)
writeToFile(*saveScheme, pull)
return
}
pull, picks = readFromFile(*loadScheme)
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Compress(5))
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 300,
})
r.Use(cors.Handler)
metaAPI(r, appDB)
dataAPI(r, db)
queryAPI(r, appDB)
moduleAPI(r, appDB, db)
log.Printf("Start server at %s", Config.Server.Port)
http.ListenAndServe(Config.Server.Port, r)
}
func getFieldInfo(table, field string) (*DBField, error) {
t, ok := pull[table]
if !ok {
return nil, fmt.Errorf("table %s is unknown", table)
}
for i := range t.Fields {
if t.Fields[i].ID == field {
return &t.Fields[i], nil
}
}
return nil, fmt.Errorf("table %s doesn't have %s field", table, field)
}