forked from golang/hotime
优化日志及config
This commit is contained in:
parent
f37b9aee70
commit
8f7380d796
@ -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)
|
||||
}
|
||||
//接入动态代码层
|
||||
|
@ -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",
|
||||
|
133
code/makecode.go
133
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)
|
||||
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
}
|
@ -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)
|
||||
},
|
||||
}}})
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user