forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
459 lines (403 loc) · 11.3 KB
/
load.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package global
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/data"
"github.com/yaoapp/xiang/table"
"github.com/yaoapp/xiang/xfs"
"github.com/yaoapp/xun/capsule"
)
// Script 脚本文件类型
type Script struct {
Name string
Type string
Content []byte
File string
}
// AppRoot 应用目录
type AppRoot struct {
APIs string
Flows string
Models string
Plugins string
Tables string
Charts string
Screens string
Data string
}
// AppInfo 应用信息
type AppInfo struct {
Name string `json:"name,omitempty"`
Short string `json:"short,omitempty"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
Icons map[string]string `json:"icons,omitempty"`
}
// App 应用信息
var App AppInfo
// Load 根据配置加载 API, FLow, Model, Plugin
func Load(cfg config.Config) {
AppInit(cfg)
DBConnect(cfg.Database)
LoadAppInfo(cfg.Root)
LoadEngine(cfg.Path)
LoadApp(AppRoot{
APIs: cfg.RootAPI,
Flows: cfg.RootFLow,
Models: cfg.RootModel,
Plugins: cfg.RootPlugin,
Tables: cfg.RootTable,
Charts: cfg.RootChart,
Screens: cfg.RootScreen,
Data: cfg.RootData,
})
// 加密密钥函数
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.Database.AESKey), "AES")
gou.LoadCrypt(`{}`, "PASSWORD")
// 设定已加载配置
Conf = cfg
}
// Reload 根据配置重新加载 API, FLow, Model, Plugin
func Reload(cfg config.Config) {
gou.APIs = map[string]*gou.API{}
gou.Models = map[string]*gou.Model{}
gou.Flows = map[string]*gou.Flow{}
gou.Plugins = map[string]*gou.Plugin{}
Load(cfg)
}
// DBConnect 建立数据库连接
func DBConnect(dbconfig config.DatabaseConfig) {
// 连接主库
for i, dsn := range dbconfig.Primary {
db := capsule.AddConn("primary", dbconfig.Driver, dsn)
if i == 0 {
db.SetAsGlobal()
}
}
// 连接从库
for _, dsn := range dbconfig.Secondary {
capsule.AddReadConn("secondary", dbconfig.Driver, dsn)
}
}
// AppInit 应用初始化
func AppInit(cfg config.Config) {
if _, err := os.Stat(cfg.RootUI); os.IsNotExist(err) {
err := os.MkdirAll(cfg.RootUI, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", cfg.RootUI, err)
}
}
if _, err := os.Stat(cfg.RootDB); os.IsNotExist(err) {
err := os.MkdirAll(cfg.RootDB, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", cfg.RootDB, err)
}
}
if _, err := os.Stat(cfg.RootTable); os.IsNotExist(err) {
err := os.MkdirAll(cfg.RootTable, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", cfg.RootTable, err)
}
}
}
// LoadAppInfo 读取应用信息
func LoadAppInfo(root string) {
info := defaultAppInfo()
fs := xfs.New(root)
if fs.MustExists("/app.json") {
err := jsoniter.Unmarshal(fs.MustReadFile("/app.json"), &info)
if err != nil {
exception.New("解析应用失败 %s", 500, err).Throw()
}
}
if fs.MustExists("/xiang/icons/icon.icns") {
info.Icons["icns"] = xfs.Encode(fs.MustReadFile("/xiang/icons/icon.icns"))
}
if fs.MustExists("/xiang/icons/icon.ico") {
info.Icons["ico"] = xfs.Encode(fs.MustReadFile("/xiang/icons/icon.ico"))
}
if fs.MustExists("/xiang/icons/icon.png") {
info.Icons["png"] = xfs.Encode(fs.MustReadFile("/xiang/icons/icon.png"))
}
App = info
}
// defaultAppInfo 读取默认应用信息
func defaultAppInfo() AppInfo {
info := AppInfo{}
err := jsoniter.Unmarshal(data.MustAsset("xiang/data/app.json"), &info)
if err != nil {
exception.New("解析默认应用失败 %s", 500, err).Throw()
}
info.Version = VERSION
info.Icons["icns"] = xfs.Encode(data.MustAsset("xiang/data/icons/icon.icns"))
info.Icons["ico"] = xfs.Encode(data.MustAsset("xiang/data/icons/icon.ico"))
info.Icons["png"] = xfs.Encode(data.MustAsset("xiang/data/icons/icon.png"))
return info
}
// LoadEngine 加载引擎的 API, Flow, Model 配置
func LoadEngine(from string) {
var scripts []Script
if strings.HasPrefix(from, "fs://") || !strings.Contains(from, "://") {
root := strings.TrimPrefix(from, "fs://")
scripts = getFilesFS(root, ".json")
} else if strings.HasPrefix(from, "bin://") {
root := strings.TrimPrefix(from, "bin://")
scripts = getFilesBin(root, ".json")
}
if scripts == nil {
exception.New("读取文件失败", 500, from).Throw()
}
if len(scripts) == 0 {
exception.New("读取文件失败, 未找到任何可执行脚本", 500, from).Throw()
}
// 加载 API, Flow, Models, Table, Chart, Screens
for _, script := range scripts {
switch script.Type {
case "models":
gou.LoadModel(string(script.Content), "xiang."+script.Name)
break
case "flows":
gou.LoadFlow(string(script.Content), "xiang."+script.Name)
break
case "apis":
gou.LoadAPI(string(script.Content), "xiang."+script.Name)
break
}
}
// 加载数据应用
for _, script := range scripts {
switch script.Type {
case "tables":
table.Load(string(script.Content), "xiang."+script.Name)
break
}
}
}
// LoadApp 加载应用的 API, Flow, Model 和 Plugin
func LoadApp(app AppRoot) {
// api string, flow string, model string, plugin string
// 创建应用目录
paths := []string{app.APIs, app.Flows, app.Models, app.Plugins, app.Charts, app.Screens, app.Data}
for _, p := range paths {
if !strings.HasPrefix(p, "fs://") && strings.Contains(p, "://") {
continue
}
root, err := filepath.Abs(strings.TrimPrefix(p, "fs://"))
if err != nil {
log.Panicf("创建目录失败(%s) %s", root, err)
}
if _, err := os.Stat(root); os.IsNotExist(err) {
err := os.MkdirAll(root, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", root, err)
}
}
}
// 加载API
if strings.HasPrefix(app.APIs, "fs://") || !strings.Contains(app.APIs, "://") {
root := strings.TrimPrefix(app.APIs, "fs://")
scripts := getAppFilesFS(root, ".json")
for _, script := range scripts {
// 验证API 加载逻辑
gou.LoadAPI(string(script.Content), script.Name)
}
}
// 加载Flow
if strings.HasPrefix(app.Flows, "fs://") || !strings.Contains(app.Flows, "://") {
root := strings.TrimPrefix(app.Flows, "fs://")
scripts := getAppFilesFS(root, ".json")
for _, script := range scripts {
gou.LoadFlow(string(script.Content), script.Name)
}
}
// 加载Model
if strings.HasPrefix(app.Models, "fs://") || !strings.Contains(app.Models, "://") {
root := strings.TrimPrefix(app.Models, "fs://")
scripts := getAppFilesFS(root, ".json")
for _, script := range scripts {
gou.LoadModel(string(script.Content), script.Name)
}
}
// 加载Plugin
if strings.HasPrefix(app.Plugins, "fs://") || !strings.Contains(app.Plugins, "://") {
root := strings.TrimPrefix(app.Plugins, "fs://")
scripts := getAppPlugins(root, ".so")
for _, script := range scripts {
gou.LoadPlugin(script.File, script.Name)
}
}
// 加载Table
if strings.HasPrefix(app.Tables, "fs://") || !strings.Contains(app.Tables, "://") {
root := strings.TrimPrefix(app.Tables, "fs://")
scripts := getAppFilesFS(root, ".json")
for _, script := range scripts {
// 验证API 加载逻辑
table.Load(string(script.Content), script.Name)
}
}
}
// / getAppPluins 遍历应用目录,读取文件列表
func getAppPlugins(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(file string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(file, typ) {
files = append(files, getAppPluginFile(root, file))
}
return nil
})
return files
}
// getAppPluginFile 读取文件
func getAppPluginFile(root string, file string) Script {
name := getAppPluginFileName(root, file)
return Script{
Name: name,
Type: "plugin",
File: file,
}
}
// getAppFile 读取文件
func getAppPluginFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
nametypes := strings.Split(namer[0], "/")
name := strings.Join(nametypes, ".")
return name
}
// getAppFilesFS 遍历应用目录,读取文件列表
func getAppFilesFS(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(filepath string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(filepath, typ) {
files = append(files, getAppFile(root, filepath))
}
return nil
})
return files
}
// getAppFile 读取文件
func getAppFile(root string, filepath string) Script {
name := getAppFileName(root, filepath)
file, err := os.Open(filepath)
if err != nil {
exception.Err(err, 500).Throw()
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
exception.Err(err, 500).Throw()
}
return Script{
Name: name,
Type: "app",
Content: content,
}
}
// getAppFile 读取文件
func getAppFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
nametypes := strings.Split(namer[0], "/")
name := strings.Join(nametypes, ".")
return name
}
// getAppFileBaseName 读取文件base
func getAppFileBaseName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
return filepath.Join(root, namer[0])
}
// getFilesFS 遍历目录,读取文件列表
func getFilesFS(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(path, typ) {
files = append(files, getFile(root, path))
}
return nil
})
return files
}
// getFile 读取文件
func getFile(root string, path string) Script {
filename := strings.TrimPrefix(path, root+"/")
name, typ := getTypeName(filename)
file, err := os.Open(path)
if err != nil {
exception.Err(err, 500).Throw()
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
exception.Err(err, 500).Throw()
}
return Script{
Name: name,
Type: typ,
Content: content,
}
}
// getFileName 读取文件
func getFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
name, _ := getTypeName(filename)
return name
}
// getFileBaseName 读取文件base
func getFileBaseName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
return filepath.Join(root, namer[0])
}
// getFilesBin 从 bindata 中读取文件列表
func getFilesBin(root string, typ string) []Script {
files := []Script{}
binfiles := data.AssetNames()
for _, path := range binfiles {
if strings.HasSuffix(path, typ) {
file := strings.TrimPrefix(path, root+"/")
name, typ := getTypeName(file)
content, err := data.Asset(path)
if err != nil {
exception.Err(err, 500).Throw()
}
files = append(files, Script{
Name: name,
Type: typ,
Content: content,
})
}
}
return files
}
func getTypeName(path string) (name string, typ string) {
namer := strings.Split(path, ".")
nametypes := strings.Split(namer[0], "/")
name = strings.Join(nametypes[1:], ".")
typ = nametypes[0]
return name, typ
}