From 8f7380d79615dfe29b6c8062b0b645d3b39b6198 Mon Sep 17 00:00:00 2001 From: hoteas <等> Date: Mon, 11 Jul 2022 19:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E5=8F=8Aco?= =?UTF-8?q?nfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application.go | 5 +- code/config.go | 8 +-- code/makecode.go | 133 ++++++++++++++++++++----------------- example/config/config.json | 10 ++- example/main.go | 35 ++++++++-- log/logrus.go | 9 ++- 6 files changed, 119 insertions(+), 81 deletions(-) diff --git a/application.go b/application.go index cf5488b..a9541f3 100644 --- a/application.go +++ b/application.go @@ -563,10 +563,11 @@ func Init(config string) *Application { } if appIns.Config.GetInt("mode") > 0 { - appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{} + appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{Error: appIns.Error} + appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake) } else { - appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{} + appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{Error: appIns.Error} appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(nil, codeMake) } //接入动态代码层 diff --git a/code/config.go b/code/config.go index 96d6a40..5060194 100644 --- a/code/config.go +++ b/code/config.go @@ -5,12 +5,11 @@ import ( ) var Config = Map{ - "name": "HoTimeDashBoard", - "id": "2f92h3herh23rh2y8", + "name": "HoTimeDashBoard", + //"id": "2f92h3herh23rh2y8", "label": "HoTime管理平台", - "menus": []Map{ - {"label": "平台首页", "name": "HelloWorld", "icon": "el-icon-s-home"}, + //{"label": "平台首页", "name": "HelloWorld", "icon": "el-icon-s-home"}, //{"label": "测试表格", "table": "table", "icon": "el-icon-suitcase"}, //{"label": "系统管理", "name": "setting", "icon": "el-icon-setting", // "menus": []Map{ @@ -40,6 +39,7 @@ var ColumnDataType = map[string]string{ "float": "number", "double": "number", "decimal": "number", + "integer": "number", //sqlite3 "char": "text", "text": "text", "blob": "text", diff --git a/code/makecode.go b/code/makecode.go index fdda12b..0d2c8de 100644 --- a/code/makecode.go +++ b/code/makecode.go @@ -27,6 +27,7 @@ type MakeCode struct { func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { isMake := false + hasConfigFile := false idSlice := Slice{} that.FileConfig = config if that.TableColumns == nil { @@ -50,8 +51,11 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { that.Config[k] = v //程序配置 //Config[k] = v //系统配置 } + hasConfigFile = true } else { + that.Error.SetError(errors.New("config配置文件不存在,或者配置出错,使用缺省默认配置")) + } //加载规则文件 btesRule, errRule := ioutil.ReadFile(config.GetString("rule")) @@ -79,38 +83,38 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { that.Error.SetError(errors.New("rule配置文件不存在,或者配置出错,使用缺省默认配置")) } - that.IndexMenus = Map{} - menusConfig := that.Config.GetSlice("menus") - //将配置写入到内存中仅作为判断用 - if menusConfig != nil { - for kmenu, _ := range menusConfig { - menu := menusConfig.GetMap(kmenu) - if menu != nil { - mname := menu.GetString("table") - if mname == "" { //如果为空则不是表格 - mname = menu.GetString("name") - } - that.IndexMenus[mname] = menu - childMenus := menu.GetSlice("menus") - if childMenus != nil { - for ckmenu, _ := range childMenus { - cmenu := childMenus.GetMap(ckmenu) - if cmenu != nil { - cname := cmenu.GetString("table") - if cmenu.GetString("table") == "" { - continue - } - that.IndexMenus[mname+"/"+cname] = cmenu - } - - } - - } - - } - - } - } + //that.IndexMenus = Map{} + //menusConfig := that.Config.GetSlice("menus") + ////将配置写入到内存中仅作为判断用 + //if menusConfig != nil { + // for kmenu, _ := range menusConfig { + // menu := menusConfig.GetMap(kmenu) + // if menu != nil { + // mname := menu.GetString("table") + // if mname == "" { //如果为空则不是表格 + // mname = menu.GetString("name") + // } + // that.IndexMenus[mname] = menu + // childMenus := menu.GetSlice("menus") + // if childMenus != nil { + // for ckmenu, _ := range childMenus { + // cmenu := childMenus.GetMap(ckmenu) + // if cmenu != nil { + // cname := cmenu.GetString("table") + // if cmenu.GetString("table") == "" { + // continue + // } + // that.IndexMenus[mname+"/"+cname] = cmenu + // } + // + // } + // + // } + // + // } + // + // } + //} that.TableConfig = that.Config.GetMap("tables") if that.TableConfig == nil { @@ -352,34 +356,35 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { //生成id,判断数据库是否有改变,以保证数据库和配置文件匹配唯一 id := Md5(ObjToStr(idSlice)) - if id == that.Config.GetString("id") { - if isMake { //有生成包文件 - fmt.Println("有新的业务代码生成,请重新运行") - os.Exit(-1) - } + //if id == that.Config.GetString("id") { + if isMake { //有生成包文件 + fmt.Println("有新的业务代码生成,请重新运行") + os.Exit(-1) return } + + //} //数据生成完后进一步解析 for fk, fv := range that.TableColumns { //判断是否将表写入menu中 - isMenusGet := false //判断是否被目录收录 - for indexKey, _ := range that.IndexMenus { - indexCode := strings.Index(indexKey, fk) - if indexCode == 0 || indexCode == 4 { - isMenusGet = false - continue - } - //如果相等或者表名在目录中已经设置(主要前一位是/并且他是最后一个字符串) - if indexKey == fk || (indexCode != -1 && indexKey[indexCode-1] == '/' && indexKey[indexCode:] == fk) { - isMenusGet = true - break - } - } + //isMenusGet := false //判断是否被目录收录 + //for indexKey, _ := range that.IndexMenus { + // indexCode := strings.Index(indexKey, fk) + // if indexCode == 0 || indexCode == 4 { + // isMenusGet = false + // continue + // } + // //如果相等或者表名在目录中已经设置(主要前一位是/并且他是最后一个字符串) + // if indexKey == fk || (indexCode != -1 && indexKey[indexCode-1] == '/' && indexKey[indexCode:] == fk) { + // isMenusGet = true + // break + // } + //} //目录没有收录 - if !isMenusGet { - + //if !isMenusGet { + if !hasConfigFile { tablePrefixCode := strings.Index(fk, "_") isNewPrefix := false //假定表名没有前缀 prefixName := "" @@ -547,12 +552,12 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { } } - fmt.Println(id, "---", that.Config.GetString("id")) - that.Config["id"] = id + //fmt.Println(id, "---", that.Config.GetString("id")) + //that.Config["id"] = id if config.GetInt("mode") != 0 { //init文件初始化 - myInit = strings.Replace(myInit, "{{id}}", id, -1) + //myInit = strings.Replace(myInit, "{{id}}", id, -1) myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1) _ = os.MkdirAll(config.GetString("name"), os.ModeDir) err = ioutil.WriteFile(config.GetString("name")+"/init.go", []byte(myInit), os.ModePerm) @@ -563,14 +568,18 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { //写入配置文件 //err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t") - _ = os.MkdirAll(filepath.Dir(config.GetString("config")), os.ModeDir) - err = ioutil.WriteFile(config.GetString("config"), []byte(that.Config.ToJsonString()), os.ModePerm) - if err != nil { - that.Error.SetError(err) - } + if !hasConfigFile { + that.Config["id"] = id + _ = os.MkdirAll(filepath.Dir(config.GetString("config")), os.ModeDir) + err = ioutil.WriteFile(config.GetString("config"), []byte(that.Config.ToJsonString()), os.ModePerm) + if err != nil { + that.Error.SetError(err) + } + that.Logger.Warn("新建") - fmt.Println("有新的代码生成,请重新运行") - os.Exit(-1) + } + //fmt.Println("有新的代码生成,请重新运行") + //os.Exit(-1) } diff --git a/example/config/config.json b/example/config/config.json index ea49547..08806d5 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -16,8 +16,12 @@ } ], "db": { - "sqlite": { - "path": "config/data.db" + "mysql": { + "host": "127.0.0.1", + "name": "gov_crawler", + "password": "root", + "port": "3306", + "user": "root" } }, "defFile": [ @@ -32,7 +36,7 @@ "5": "数据结果异常" }, "mode": 2, - "port": "80", + "port": "8081", "sessionName": "HOTIME", "tpt": "tpt" } \ No newline at end of file diff --git a/example/main.go b/example/main.go index 243dcc4..6f8c130 100644 --- a/example/main.go +++ b/example/main.go @@ -4,6 +4,7 @@ import ( . "code.hoteas.com/golang/hotime" . "code.hoteas.com/golang/hotime/common" "fmt" + "golang.org/x/net/websocket" ) func main() { @@ -13,15 +14,35 @@ func main() { "user": Ctr{ "test": func(that *Context) { fmt.Println("dasdasd") - id := that.Db.Insert("user", Map{"name": "test"}) - ok := that.Db.Update("user", Map{"name": "test1"}, Map{"name": "test"}) + //id := that.Db.Insert("user", Map{"name": "test"}) + //ok := that.Db.Update("user", Map{"name": "test1"}, Map{"name": "test"}) ps := that.Db.Select("user", "*") p := that.Db.Get("user", "*") - row := that.Db.Delete("user", Map{"id": id}) - that.Display(0, Slice{id, ok, ps, p, row}) + //row := that.Db.Delete("user", Map{"id": id}) + //that.Display(0, Slice{id, ok, ps, p, row}) + that.Display(0, Slice{ps, p}) }, - }, - }, - }) + "websocket": func(that *Context) { + hdler := websocket.Handler(func(ws *websocket.Conn) { + for true { + msg := make([]byte, 5120) + n, err := ws.Read(msg) + + if err != nil { + return + } + fmt.Printf("Receive: %s\n", msg[:n]) + + send_msg := "[" + string(msg[:n]) + "]" + m, err := ws.Write([]byte(send_msg)) + if err != nil { + return + } + fmt.Printf("Send: %s\n", msg[:m]) + } + }) + hdler.ServeHTTP(that.Resp, that.Req) + }, + }}}) } diff --git a/log/logrus.go b/log/logrus.go index 7011118..774f067 100644 --- a/log/logrus.go +++ b/log/logrus.go @@ -91,9 +91,12 @@ func findCaller(skip int) string { if file == "db/hotimedb.go" { file, line = getCaller(skip + i + j) } - //if file == "code/makecode.go" { - // file, line = getCaller(skip + i + j) - //} + if file == "code/makecode.go" { + file, line = getCaller(skip + i + j) + } + if strings.Contains(file, "application.go") { + file, line = getCaller(skip + i + j) + } if j == 5 { break }