优化日志及config

This commit is contained in:
hoteas 2022-07-11 19:13:20 +08:00
parent f37b9aee70
commit 8f7380d796
6 changed files with 119 additions and 81 deletions

View File

@ -563,10 +563,11 @@ func Init(config string) *Application {
} }
if appIns.Config.GetInt("mode") > 0 { 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) appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake)
} else { } 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) appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(nil, codeMake)
} }
//接入动态代码层 //接入动态代码层

View File

@ -5,12 +5,11 @@ import (
) )
var Config = Map{ var Config = Map{
"name": "HoTimeDashBoard", "name": "HoTimeDashBoard",
"id": "2f92h3herh23rh2y8", //"id": "2f92h3herh23rh2y8",
"label": "HoTime管理平台", "label": "HoTime管理平台",
"menus": []Map{ "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": "测试表格", "table": "table", "icon": "el-icon-suitcase"},
//{"label": "系统管理", "name": "setting", "icon": "el-icon-setting", //{"label": "系统管理", "name": "setting", "icon": "el-icon-setting",
// "menus": []Map{ // "menus": []Map{
@ -40,6 +39,7 @@ var ColumnDataType = map[string]string{
"float": "number", "float": "number",
"double": "number", "double": "number",
"decimal": "number", "decimal": "number",
"integer": "number", //sqlite3
"char": "text", "char": "text",
"text": "text", "text": "text",
"blob": "text", "blob": "text",

View File

@ -27,6 +27,7 @@ type MakeCode struct {
func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) {
isMake := false isMake := false
hasConfigFile := false
idSlice := Slice{} idSlice := Slice{}
that.FileConfig = config that.FileConfig = config
if that.TableColumns == nil { if that.TableColumns == nil {
@ -50,8 +51,11 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) {
that.Config[k] = v //程序配置 that.Config[k] = v //程序配置
//Config[k] = v //系统配置 //Config[k] = v //系统配置
} }
hasConfigFile = true
} else { } else {
that.Error.SetError(errors.New("config配置文件不存在或者配置出错使用缺省默认配置")) that.Error.SetError(errors.New("config配置文件不存在或者配置出错使用缺省默认配置"))
} }
//加载规则文件 //加载规则文件
btesRule, errRule := ioutil.ReadFile(config.GetString("rule")) 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.Error.SetError(errors.New("rule配置文件不存在或者配置出错使用缺省默认配置"))
} }
that.IndexMenus = Map{} //that.IndexMenus = Map{}
menusConfig := that.Config.GetSlice("menus") //menusConfig := that.Config.GetSlice("menus")
//将配置写入到内存中仅作为判断用 ////将配置写入到内存中仅作为判断用
if menusConfig != nil { //if menusConfig != nil {
for kmenu, _ := range menusConfig { // for kmenu, _ := range menusConfig {
menu := menusConfig.GetMap(kmenu) // menu := menusConfig.GetMap(kmenu)
if menu != nil { // if menu != nil {
mname := menu.GetString("table") // mname := menu.GetString("table")
if mname == "" { //如果为空则不是表格 // if mname == "" { //如果为空则不是表格
mname = menu.GetString("name") // mname = menu.GetString("name")
} // }
that.IndexMenus[mname] = menu // that.IndexMenus[mname] = menu
childMenus := menu.GetSlice("menus") // childMenus := menu.GetSlice("menus")
if childMenus != nil { // if childMenus != nil {
for ckmenu, _ := range childMenus { // for ckmenu, _ := range childMenus {
cmenu := childMenus.GetMap(ckmenu) // cmenu := childMenus.GetMap(ckmenu)
if cmenu != nil { // if cmenu != nil {
cname := cmenu.GetString("table") // cname := cmenu.GetString("table")
if cmenu.GetString("table") == "" { // if cmenu.GetString("table") == "" {
continue // continue
} // }
that.IndexMenus[mname+"/"+cname] = cmenu // that.IndexMenus[mname+"/"+cname] = cmenu
} // }
//
} // }
//
} // }
//
} // }
//
} // }
} //}
that.TableConfig = that.Config.GetMap("tables") that.TableConfig = that.Config.GetMap("tables")
if that.TableConfig == nil { if that.TableConfig == nil {
@ -352,34 +356,35 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) {
//生成id判断数据库是否有改变以保证数据库和配置文件匹配唯一 //生成id判断数据库是否有改变以保证数据库和配置文件匹配唯一
id := Md5(ObjToStr(idSlice)) id := Md5(ObjToStr(idSlice))
if id == that.Config.GetString("id") { //if id == that.Config.GetString("id") {
if isMake { //有生成包文件 if isMake { //有生成包文件
fmt.Println("有新的业务代码生成,请重新运行") fmt.Println("有新的业务代码生成,请重新运行")
os.Exit(-1) os.Exit(-1)
}
return return
} }
//}
//数据生成完后进一步解析 //数据生成完后进一步解析
for fk, fv := range that.TableColumns { for fk, fv := range that.TableColumns {
//判断是否将表写入menu中 //判断是否将表写入menu中
isMenusGet := false //判断是否被目录收录 //isMenusGet := false //判断是否被目录收录
for indexKey, _ := range that.IndexMenus { //for indexKey, _ := range that.IndexMenus {
indexCode := strings.Index(indexKey, fk) // indexCode := strings.Index(indexKey, fk)
if indexCode == 0 || indexCode == 4 { // if indexCode == 0 || indexCode == 4 {
isMenusGet = false // isMenusGet = false
continue // continue
} // }
//如果相等或者表名在目录中已经设置(主要前一位是/并且他是最后一个字符串) // //如果相等或者表名在目录中已经设置(主要前一位是/并且他是最后一个字符串)
if indexKey == fk || (indexCode != -1 && indexKey[indexCode-1] == '/' && indexKey[indexCode:] == fk) { // if indexKey == fk || (indexCode != -1 && indexKey[indexCode-1] == '/' && indexKey[indexCode:] == fk) {
isMenusGet = true // isMenusGet = true
break // break
} // }
} //}
//目录没有收录 //目录没有收录
if !isMenusGet { //if !isMenusGet {
if !hasConfigFile {
tablePrefixCode := strings.Index(fk, "_") tablePrefixCode := strings.Index(fk, "_")
isNewPrefix := false //假定表名没有前缀 isNewPrefix := false //假定表名没有前缀
prefixName := "" prefixName := ""
@ -547,12 +552,12 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) {
} }
} }
fmt.Println(id, "---", that.Config.GetString("id")) //fmt.Println(id, "---", that.Config.GetString("id"))
that.Config["id"] = id //that.Config["id"] = id
if config.GetInt("mode") != 0 { if config.GetInt("mode") != 0 {
//init文件初始化 //init文件初始化
myInit = strings.Replace(myInit, "{{id}}", id, -1) //myInit = strings.Replace(myInit, "{{id}}", id, -1)
myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1) myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1)
_ = os.MkdirAll(config.GetString("name"), os.ModeDir) _ = os.MkdirAll(config.GetString("name"), os.ModeDir)
err = ioutil.WriteFile(config.GetString("name")+"/init.go", []byte(myInit), os.ModePerm) 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") //err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
_ = os.MkdirAll(filepath.Dir(config.GetString("config")), os.ModeDir) if !hasConfigFile {
err = ioutil.WriteFile(config.GetString("config"), []byte(that.Config.ToJsonString()), os.ModePerm) that.Config["id"] = id
if err != nil { _ = os.MkdirAll(filepath.Dir(config.GetString("config")), os.ModeDir)
that.Error.SetError(err) 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)
} }

View File

@ -16,8 +16,12 @@
} }
], ],
"db": { "db": {
"sqlite": { "mysql": {
"path": "config/data.db" "host": "127.0.0.1",
"name": "gov_crawler",
"password": "root",
"port": "3306",
"user": "root"
} }
}, },
"defFile": [ "defFile": [
@ -32,7 +36,7 @@
"5": "数据结果异常" "5": "数据结果异常"
}, },
"mode": 2, "mode": 2,
"port": "80", "port": "8081",
"sessionName": "HOTIME", "sessionName": "HOTIME",
"tpt": "tpt" "tpt": "tpt"
} }

View File

@ -4,6 +4,7 @@ import (
. "code.hoteas.com/golang/hotime" . "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common" . "code.hoteas.com/golang/hotime/common"
"fmt" "fmt"
"golang.org/x/net/websocket"
) )
func main() { func main() {
@ -13,15 +14,35 @@ func main() {
"user": Ctr{ "user": Ctr{
"test": func(that *Context) { "test": func(that *Context) {
fmt.Println("dasdasd") fmt.Println("dasdasd")
id := that.Db.Insert("user", Map{"name": "test"}) //id := that.Db.Insert("user", Map{"name": "test"})
ok := that.Db.Update("user", Map{"name": "test1"}, Map{"name": "test"}) //ok := that.Db.Update("user", Map{"name": "test1"}, Map{"name": "test"})
ps := that.Db.Select("user", "*") ps := that.Db.Select("user", "*")
p := that.Db.Get("user", "*") p := that.Db.Get("user", "*")
row := that.Db.Delete("user", Map{"id": id}) //row := that.Db.Delete("user", Map{"id": id})
that.Display(0, Slice{id, ok, ps, p, row}) //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)
},
}}})
} }

View File

@ -91,9 +91,12 @@ func findCaller(skip int) string {
if file == "db/hotimedb.go" { if file == "db/hotimedb.go" {
file, line = getCaller(skip + i + j) file, line = getCaller(skip + i + j)
} }
//if file == "code/makecode.go" { if file == "code/makecode.go" {
// file, line = getCaller(skip + i + j) file, line = getCaller(skip + i + j)
//} }
if strings.Contains(file, "application.go") {
file, line = getCaller(skip + i + j)
}
if j == 5 { if j == 5 {
break break
} }