框架优化

This commit is contained in:
hoteas 2022-03-13 17:02:19 +08:00
parent 17905c2d21
commit cb04dc6c34
8 changed files with 96 additions and 66 deletions

View File

@ -256,8 +256,8 @@ func (that *Application) SetConfig(configPath ...string) {
} }
// SetConnectListener 连接判断,返回true继续传输至控制层false则停止传输 // SetConnectListener 连接判断,返回false继续传输至控制层true则停止传输
func (that *Application) SetConnectListener(lis func(that *Context) bool) { func (that *Application) SetConnectListener(lis func(that *Context) (isFinished bool)) {
that.connectListener = append(that.connectListener, lis) that.connectListener = append(that.connectListener, lis)
} }
@ -358,14 +358,13 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
//访问拦截true继续false暂停 //访问拦截true继续false暂停
connectListenerLen := len(that.connectListener) connectListenerLen := len(that.connectListener)
if connectListenerLen != 0 {
for i := 0; i < connectListenerLen; i++ {
if !that.connectListener[i](&context) { for i := connectListenerLen - 1; i > 0; i-- {
context.View() if that.connectListener[i](&context) {
return
} context.View()
return
} }
} }
@ -532,7 +531,7 @@ func Init(config string) Application {
SetDB(&appIns) SetDB(&appIns)
appIns.SetCache() appIns.SetCache()
codeConfig := appIns.Config.GetMap("codeConfig") codeConfig := appIns.Config.GetSlice("codeConfig")
if codeConfig != nil { if codeConfig != nil {
@ -541,10 +540,15 @@ func Init(config string) Application {
if codeMake == nil { if codeMake == nil {
continue continue
} }
codeMake["table"] = k //codeMake["table"] = k
if appIns.MakeCodeRouter == nil { if appIns.MakeCodeRouter == nil {
appIns.MakeCodeRouter = map[string]*code.MakeCode{} appIns.MakeCodeRouter = map[string]*code.MakeCode{}
} }
if codeMake.GetString("name") == "" {
codeMake["name"] = codeMake.GetString("table")
}
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{}
appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake) appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake)
@ -557,10 +561,6 @@ func Init(config string) Application {
appIns.Router = Router{} appIns.Router = Router{}
} }
if codeMake.GetString("name") == "" {
codeMake["name"] = k
}
appIns.Router[codeMake.GetString("name")] = TptProject appIns.Router[codeMake.GetString("name")] = TptProject
for k1, _ := range appIns.MakeCodeRouter[codeMake.GetString("name")].TableColumns { for k1, _ := range appIns.MakeCodeRouter[codeMake.GetString("name")].TableColumns {
appIns.Router[codeMake.GetString("name")][k1] = appIns.Router[codeMake.GetString("name")]["hotimeCommon"] appIns.Router[codeMake.GetString("name")][k1] = appIns.Router[codeMake.GetString("name")]["hotimeCommon"]
@ -633,35 +633,36 @@ func SetSqliteDB(appIns *Application, config Map) {
} }
func setMakeCodeLintener(name string, appIns *Application) { func setMakeCodeLintener(name string, appIns *Application) {
appIns.SetConnectListener(func(context *Context) bool { appIns.SetConnectListener(func(context *Context) (isFinished bool) {
if len(context.RouterString) < 2 || appIns.MakeCodeRouter[context.RouterString[0]] == nil { if len(context.RouterString) < 2 || appIns.MakeCodeRouter[context.RouterString[0]] == nil {
return true return isFinished
} }
codeIns := appIns.MakeCodeRouter[name]
if len(context.RouterString) > 1 && context.RouterString[0] == name { if len(context.RouterString) > 1 && context.RouterString[0] == name {
if context.RouterString[1] == "hotime" && context.RouterString[2] == "login" { if context.RouterString[1] == "hotime" && context.RouterString[2] == "login" {
return true return isFinished
} }
if context.RouterString[1] == "hotime" && context.RouterString[2] == "logout" { if context.RouterString[1] == "hotime" && context.RouterString[2] == "logout" {
return true return isFinished
} }
if context.Session(name+"_id").Data == nil { if context.Session(codeIns.FileConfig.GetString("table")+"_id").Data == nil {
context.Display(2, "你还没有登录") context.Display(2, "你还没有登录")
return false return true
} }
} }
//文件上传接口 //文件上传接口
if len(context.RouterString) == 1 && context.RouterString[0] == "file" && context.Req.Method == "POST" { if len(context.RouterString) == 1 && context.RouterString[0] == "file" && context.Req.Method == "POST" {
if context.Session(name+"_id").Data == nil { if context.Session(codeIns.FileConfig.GetString("table")+"_id").Data == nil {
context.Display(2, "你还没有登录") context.Display(2, "你还没有登录")
return false return true
} }
//读取网络文件 //读取网络文件
fi, fheader, err := context.Req.FormFile("file") fi, fheader, err := context.Req.FormFile("file")
if err != nil { if err != nil {
context.Display(3, err) context.Display(3, err)
return false return true
} }
filePath := context.Config.GetString("filePath") filePath := context.Config.GetString("filePath")
@ -673,43 +674,43 @@ func setMakeCodeLintener(name string, appIns *Application) {
e := os.MkdirAll(context.Config.GetString("tpt")+"/"+path, os.ModeDir) e := os.MkdirAll(context.Config.GetString("tpt")+"/"+path, os.ModeDir)
if e != nil { if e != nil {
context.Display(3, e) context.Display(3, e)
return false return true
} }
filePath = path + Md5(ObjToStr(RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):] filePath = path + Md5(ObjToStr(RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):]
newFile, e := os.Create(context.Config.GetString("tpt") + "/" + filePath) newFile, e := os.Create(context.Config.GetString("tpt") + "/" + filePath)
if e != nil { if e != nil {
context.Display(3, e) context.Display(3, e)
return false return true
} }
_, e = io.Copy(newFile, fi) _, e = io.Copy(newFile, fi)
if e != nil { if e != nil {
context.Display(3, e) context.Display(3, e)
return false return true
} }
context.Display(0, filePath) context.Display(0, filePath)
return false return true
} }
if len(context.RouterString) < 2 || len(context.RouterString) > 3 || if len(context.RouterString) < 2 || len(context.RouterString) > 3 ||
!(context.Router[context.RouterString[0]] != nil && !(context.Router[context.RouterString[0]] != nil &&
context.Router[context.RouterString[0]][context.RouterString[1]] != nil) { context.Router[context.RouterString[0]][context.RouterString[1]] != nil) {
return true return isFinished
} }
//排除无效操作 //排除无效操作
if len(context.RouterString) == 2 && if len(context.RouterString) == 2 &&
context.Req.Method != "GET" && context.Req.Method != "GET" &&
context.Req.Method != "POST" { context.Req.Method != "POST" {
return true return isFinished
} }
//列表检索 //列表检索
if len(context.RouterString) == 2 && if len(context.RouterString) == 2 &&
context.Req.Method == "GET" { context.Req.Method == "GET" {
if context.Router[context.RouterString[0]][context.RouterString[1]]["search"] == nil { if context.Router[context.RouterString[0]][context.RouterString[1]]["search"] == nil {
return true return isFinished
} }
context.Router[context.RouterString[0]][context.RouterString[1]]["search"](context) context.Router[context.RouterString[0]][context.RouterString[1]]["search"](context)
} }
@ -723,14 +724,14 @@ func setMakeCodeLintener(name string, appIns *Application) {
} }
if len(context.RouterString) == 3 && if len(context.RouterString) == 3 &&
context.Req.Method == "POST" { context.Req.Method == "POST" {
return true return isFinished
} }
//查询单条 //查询单条
if len(context.RouterString) == 3 && if len(context.RouterString) == 3 &&
context.Req.Method == "GET" { context.Req.Method == "GET" {
if context.Router[context.RouterString[0]][context.RouterString[1]]["info"] == nil { if context.Router[context.RouterString[0]][context.RouterString[1]]["info"] == nil {
return true return isFinished
} }
context.Router[context.RouterString[0]][context.RouterString[1]]["info"](context) context.Router[context.RouterString[0]][context.RouterString[1]]["info"](context)
@ -755,7 +756,8 @@ func setMakeCodeLintener(name string, appIns *Application) {
context.Router[context.RouterString[0]][context.RouterString[1]]["remove"](context) context.Router[context.RouterString[0]][context.RouterString[1]]["remove"](context)
} }
context.View() context.View()
return false return true
}) })
} }

View File

@ -11,7 +11,8 @@ var TptProject = Proj{
"hotimeCommon": Ctr{ "hotimeCommon": Ctr{
"info": func(that *Context) { "info": func(that *Context) {
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()}) fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
str, inData := that.MakeCodeRouter[hotimeName].Info(that.RouterString[1], data, that.Db) str, inData := that.MakeCodeRouter[hotimeName].Info(that.RouterString[1], data, that.Db)
where := Map{"id": that.RouterString[2]} where := Map{"id": that.RouterString[2]}
@ -128,7 +129,9 @@ var TptProject = Proj{
"search": func(that *Context) { "search": func(that *Context) {
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()}) fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
columnStr, leftJoin, where := that.MakeCodeRouter[hotimeName].Search(that.RouterString[1], data, that.Req, that.Db) columnStr, leftJoin, where := that.MakeCodeRouter[hotimeName].Search(that.RouterString[1], data, that.Req, that.Db)
@ -220,7 +223,7 @@ var TptProject = Proj{
return return
} }
for k, v := range re { for k, v := range re {
column := that.MakeCodeRouter[hotimeName].TableColumns[hotimeName][k] column := that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")][k]
if column == nil { if column == nil {
continue continue
} }

View File

@ -61,6 +61,8 @@ type ColumnShow struct {
var ColumnNameType = []ColumnShow{ var ColumnNameType = []ColumnShow{
//通用 //通用
{"idcard", false, true, true, false, "", false},
{"id", true, false, true, false, "", true},
{"parent_id", true, true, true, false, "", true}, {"parent_id", true, true, true, false, "", true},
//"sn"{true,true,true,""}, //"sn"{true,true,true,""},
{"status", true, true, true, false, "select", false}, {"status", true, true, true, false, "select", false},

View File

@ -43,32 +43,32 @@ func (that *HoTimeDB) GetType() string {
} }
// Action 事务如果action返回true则执行成功false则回滚 // Action 事务如果action返回true则执行成功false则回滚
func (that *HoTimeDB) Action(action func(db HoTimeDB) bool) bool { func (that *HoTimeDB) Action(action func(db HoTimeDB) (isSuccess bool)) (isSuccess bool) {
db := HoTimeDB{DB: that.DB, HoTimeCache: that.HoTimeCache, Prefix: that.Prefix} db := HoTimeDB{DB: that.DB, HoTimeCache: that.HoTimeCache, Prefix: that.Prefix}
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
that.LastErr.SetError(err) that.LastErr.SetError(err)
return false return isSuccess
} }
db.Tx = tx db.Tx = tx
result := action(db) isSuccess = action(db)
if !result { if !isSuccess {
err = db.Tx.Rollback() err = db.Tx.Rollback()
if err != nil { if err != nil {
that.LastErr.SetError(err) that.LastErr.SetError(err)
return false return isSuccess
} }
return result return isSuccess
} }
err = db.Tx.Commit() err = db.Tx.Commit()
if err != nil { if err != nil {
that.LastErr.SetError(err) that.LastErr.SetError(err)
return false return false
} }
return result return true
} }
func (that *HoTimeDB) InitDb(err ...*Error) *Error { func (that *HoTimeDB) InitDb(err ...*Error) *Error {

View File

@ -11,18 +11,27 @@
"timeout": 7200 "timeout": 7200
} }
}, },
"codeConfig": { "codeConfig": [
"admin": { {
"config": "config/admin.json",
"mode": 0,
"name": "",
"rule": "config/adminRule.json",
"table": "admin"
},
{
"config": "config/app.json", "config": "config/app.json",
"name": "admin", "mode": 0,
"rule": "config/rule.json" "name": "app",
"rule": "config/appRule.json",
"table": "admin"
} }
}, ],
"crossDomain": "auto", "crossDomain": "",
"db": { "db": {
"mysql": { "mysql": {
"host": "192.168.6.253", "host": "192.168.6.253",
"name": "iedc_dev", "name": "rfcb",
"password": "dasda8454456", "password": "dasda8454456",
"port": "3306", "port": "3306",
"prefix": "", "prefix": "",

View File

@ -20,16 +20,16 @@
}, },
"注释": "可配置memorydbredis默认启用memory默认优先级为memory\u003eredis\u003edb,memory与数据库缓存设置项一致缓存数据填充会自动反方向反哺加入memory缓存过期将自动从redis更新但memory永远不会更新redis如果是集群建议不要开启memory配置即启用" "注释": "可配置memorydbredis默认启用memory默认优先级为memory\u003eredis\u003edb,memory与数据库缓存设置项一致缓存数据填充会自动反方向反哺加入memory缓存过期将自动从redis更新但memory永远不会更新redis如果是集群建议不要开启memory配置即启用"
}, },
"codeConfig": { "codeConfig": [
"admin": { "注释:配置即启用,非必须,默认无",
{
"config": "默认config/app.json必须接口描述配置文件", "config": "默认config/app.json必须接口描述配置文件",
"mode": "默认0非必须0为内嵌代码模式1为生成代码模式", "mode": "默认0非必须0为内嵌代码模式1为生成代码模式",
"name": "默认无非必须有则生成代码到此目录无则采用缺省模式使用表名如设置为admin将在admin目录生成包名为admin的代码", "name": "默认无非必须有则生成代码到此目录无则采用缺省模式使用表名如设置为admin将在admin目录生成包名为admin的代码",
"rule": "默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成", "rule": "默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成",
"注释": "默认admin非必须有则根据数据库内当前表名做为用户生成数据" "table": "默认admin必须根据数据库内当前表名做为用户生成数据"
}, }
"注释": "配置即启用,非必须,默认无" ],
},
"crossDomain": "默认空 非必须空字符串为不开启如果需要跨域设置auto为智能开启所有网站允许跨域http://www.baidu.com为指定域允许跨域", "crossDomain": "默认空 非必须空字符串为不开启如果需要跨域设置auto为智能开启所有网站允许跨域http://www.baidu.com为指定域允许跨域",
"db": { "db": {
"mysql": { "mysql": {

View File

@ -1,7 +1,7 @@
package main package main
import ( 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"
"time" "time"
@ -11,8 +11,21 @@ func main() {
date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00") date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00")
fmt.Println(date, date.Unix()) fmt.Println(date, date.Unix())
//fmt.Println("0123456"[1:7]) //fmt.Println("0123456"[1:7])
appIns := hotime.Init("config/config.json") appIns := Init("config/config.json")
//a:=Map{} //a:=Map{}
//a.GetBool() //a.GetBool()
appIns.Run(hotime.Router{}) appIns.SetConnectListener(func(that *Context) (isFinished bool) {
if len(that.RouterString) == 3 {
if that.HandlerStr == "/app/hotime/test" {
that.Session("admin_id", 1)
that.Display(0, "开始测试")
return true
}
}
return isFinished
})
//appIns.Db.Action(func(db db.HoTimeDB) (isSuccess bool) {
// return isSuccess
//})
appIns.Run(Router{})
} }

15
var.go
View File

@ -14,9 +14,9 @@ var Config = Map{
//"codeConfig": Map{ //"codeConfig": Map{
// "admin": "config/app.json", // "admin": "config/app.json",
//}, //},
"codeConfig": Map{ "codeConfig": Slice{
"admin": Map{ Map{
//"table":"admin",//默认admin必须有则根据数据库内当前表名做为用户生成数据 "table": "admin", //默认admin必须有则根据数据库内当前表名做为用户生成数据
"name": "", //默认admin非必须有则生成代码到此目录无则采用缺省模式使用表名 "name": "", //默认admin非必须有则生成代码到此目录无则采用缺省模式使用表名
"config": "config/app.json", //默认config/app.json必须接口描述配置文件 "config": "config/app.json", //默认config/app.json必须接口描述配置文件
"rule": "config/rule.json", //默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成 "rule": "config/rule.json", //默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成
@ -61,10 +61,11 @@ var ConfigNote = Map{
// //
// "packageName": "默认无必须包名称以及应用名生成代码的配置文件地址比如config/app.json数据库有更新时自动更新配置文件以及对应的生成文件", // "packageName": "默认无必须包名称以及应用名生成代码的配置文件地址比如config/app.json数据库有更新时自动更新配置文件以及对应的生成文件",
//}, //},
"codeConfig": Map{ "codeConfig": Slice{
"注释": "配置即启用,非必须,默认无", "注释:配置即启用,非必须,默认无",
"admin": Map{ //默认无,必须,接口类别名称 Map{ //默认无,必须,接口类别名称
"注释": "默认admin非必须有则根据数据库内当前表名做为用户生成数据", // //"注释": "", //
"table": "默认admin必须根据数据库内当前表名做为用户生成数据",
"name": "默认无非必须有则生成代码到此目录无则采用缺省模式使用表名如设置为admin将在admin目录生成包名为admin的代码", "name": "默认无非必须有则生成代码到此目录无则采用缺省模式使用表名如设置为admin将在admin目录生成包名为admin的代码",
"config": "默认config/app.json必须接口描述配置文件", // "config": "默认config/app.json必须接口描述配置文件", //
"rule": "默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成", "rule": "默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成",