forked from golang/hotime
框架优化
This commit is contained in:
parent
17905c2d21
commit
cb04dc6c34
@ -256,8 +256,8 @@ func (that *Application) SetConfig(configPath ...string) {
|
||||
|
||||
}
|
||||
|
||||
// SetConnectListener 连接判断,返回true继续传输至控制层,false则停止传输
|
||||
func (that *Application) SetConnectListener(lis func(that *Context) bool) {
|
||||
// SetConnectListener 连接判断,返回false继续传输至控制层,true则停止传输
|
||||
func (that *Application) SetConnectListener(lis func(that *Context) (isFinished bool)) {
|
||||
that.connectListener = append(that.connectListener, lis)
|
||||
}
|
||||
|
||||
@ -358,16 +358,15 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
//访问拦截true继续false暂停
|
||||
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-- {
|
||||
|
||||
if that.connectListener[i](&context) {
|
||||
|
||||
context.View()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//接口服务
|
||||
//验证接口严格模式
|
||||
@ -532,7 +531,7 @@ func Init(config string) Application {
|
||||
|
||||
SetDB(&appIns)
|
||||
appIns.SetCache()
|
||||
codeConfig := appIns.Config.GetMap("codeConfig")
|
||||
codeConfig := appIns.Config.GetSlice("codeConfig")
|
||||
|
||||
if codeConfig != nil {
|
||||
|
||||
@ -541,10 +540,15 @@ func Init(config string) Application {
|
||||
if codeMake == nil {
|
||||
continue
|
||||
}
|
||||
codeMake["table"] = k
|
||||
//codeMake["table"] = k
|
||||
if appIns.MakeCodeRouter == nil {
|
||||
appIns.MakeCodeRouter = map[string]*code.MakeCode{}
|
||||
}
|
||||
|
||||
if codeMake.GetString("name") == "" {
|
||||
codeMake["name"] = codeMake.GetString("table")
|
||||
}
|
||||
|
||||
if appIns.Config.GetInt("mode") > 0 {
|
||||
appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{}
|
||||
appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake)
|
||||
@ -557,10 +561,6 @@ func Init(config string) Application {
|
||||
appIns.Router = Router{}
|
||||
}
|
||||
|
||||
if codeMake.GetString("name") == "" {
|
||||
codeMake["name"] = k
|
||||
}
|
||||
|
||||
appIns.Router[codeMake.GetString("name")] = TptProject
|
||||
for k1, _ := range appIns.MakeCodeRouter[codeMake.GetString("name")].TableColumns {
|
||||
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) {
|
||||
appIns.SetConnectListener(func(context *Context) bool {
|
||||
appIns.SetConnectListener(func(context *Context) (isFinished bool) {
|
||||
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 context.RouterString[1] == "hotime" && context.RouterString[2] == "login" {
|
||||
return true
|
||||
return isFinished
|
||||
}
|
||||
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, "你还没有登录")
|
||||
return false
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
//文件上传接口
|
||||
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, "你还没有登录")
|
||||
return false
|
||||
return true
|
||||
}
|
||||
//读取网络文件
|
||||
fi, fheader, err := context.Req.FormFile("file")
|
||||
if err != nil {
|
||||
context.Display(3, err)
|
||||
return false
|
||||
return true
|
||||
|
||||
}
|
||||
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)
|
||||
if e != nil {
|
||||
context.Display(3, e)
|
||||
return false
|
||||
return true
|
||||
}
|
||||
filePath = path + Md5(ObjToStr(RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):]
|
||||
newFile, e := os.Create(context.Config.GetString("tpt") + "/" + filePath)
|
||||
|
||||
if e != nil {
|
||||
context.Display(3, e)
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
_, e = io.Copy(newFile, fi)
|
||||
|
||||
if e != nil {
|
||||
context.Display(3, e)
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
context.Display(0, filePath)
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
if len(context.RouterString) < 2 || len(context.RouterString) > 3 ||
|
||||
!(context.Router[context.RouterString[0]] != nil &&
|
||||
context.Router[context.RouterString[0]][context.RouterString[1]] != nil) {
|
||||
return true
|
||||
return isFinished
|
||||
}
|
||||
//排除无效操作
|
||||
if len(context.RouterString) == 2 &&
|
||||
context.Req.Method != "GET" &&
|
||||
context.Req.Method != "POST" {
|
||||
return true
|
||||
return isFinished
|
||||
}
|
||||
//列表检索
|
||||
if len(context.RouterString) == 2 &&
|
||||
context.Req.Method == "GET" {
|
||||
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)
|
||||
}
|
||||
@ -723,14 +724,14 @@ func setMakeCodeLintener(name string, appIns *Application) {
|
||||
}
|
||||
if len(context.RouterString) == 3 &&
|
||||
context.Req.Method == "POST" {
|
||||
return true
|
||||
return isFinished
|
||||
}
|
||||
//查询单条
|
||||
if len(context.RouterString) == 3 &&
|
||||
context.Req.Method == "GET" {
|
||||
|
||||
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)
|
||||
@ -755,7 +756,8 @@ func setMakeCodeLintener(name string, appIns *Application) {
|
||||
|
||||
context.Router[context.RouterString[0]][context.RouterString[1]]["remove"](context)
|
||||
}
|
||||
|
||||
context.View()
|
||||
return false
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
9
code.go
9
code.go
@ -11,7 +11,8 @@ var TptProject = Proj{
|
||||
"hotimeCommon": Ctr{
|
||||
"info": func(that *Context) {
|
||||
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)
|
||||
where := Map{"id": that.RouterString[2]}
|
||||
|
||||
@ -128,7 +129,9 @@ var TptProject = Proj{
|
||||
|
||||
"search": func(that *Context) {
|
||||
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)
|
||||
|
||||
@ -220,7 +223,7 @@ var TptProject = Proj{
|
||||
return
|
||||
}
|
||||
for k, v := range re {
|
||||
column := that.MakeCodeRouter[hotimeName].TableColumns[hotimeName][k]
|
||||
column := that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")][k]
|
||||
if column == nil {
|
||||
continue
|
||||
}
|
||||
|
@ -61,6 +61,8 @@ type ColumnShow struct {
|
||||
|
||||
var ColumnNameType = []ColumnShow{
|
||||
//通用
|
||||
{"idcard", false, true, true, false, "", false},
|
||||
{"id", true, false, true, false, "", true},
|
||||
{"parent_id", true, true, true, false, "", true},
|
||||
//"sn"{true,true,true,""},
|
||||
{"status", true, true, true, false, "select", false},
|
||||
|
@ -43,32 +43,32 @@ func (that *HoTimeDB) GetType() string {
|
||||
}
|
||||
|
||||
// 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}
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
that.LastErr.SetError(err)
|
||||
return false
|
||||
return isSuccess
|
||||
}
|
||||
|
||||
db.Tx = tx
|
||||
|
||||
result := action(db)
|
||||
isSuccess = action(db)
|
||||
|
||||
if !result {
|
||||
if !isSuccess {
|
||||
err = db.Tx.Rollback()
|
||||
if err != nil {
|
||||
that.LastErr.SetError(err)
|
||||
return false
|
||||
return isSuccess
|
||||
}
|
||||
return result
|
||||
return isSuccess
|
||||
}
|
||||
err = db.Tx.Commit()
|
||||
if err != nil {
|
||||
that.LastErr.SetError(err)
|
||||
return false
|
||||
}
|
||||
return result
|
||||
return true
|
||||
}
|
||||
|
||||
func (that *HoTimeDB) InitDb(err ...*Error) *Error {
|
||||
|
@ -11,18 +11,27 @@
|
||||
"timeout": 7200
|
||||
}
|
||||
},
|
||||
"codeConfig": {
|
||||
"admin": {
|
||||
"config": "config/app.json",
|
||||
"name": "admin",
|
||||
"rule": "config/rule.json"
|
||||
}
|
||||
"codeConfig": [
|
||||
{
|
||||
"config": "config/admin.json",
|
||||
"mode": 0,
|
||||
"name": "",
|
||||
"rule": "config/adminRule.json",
|
||||
"table": "admin"
|
||||
},
|
||||
"crossDomain": "auto",
|
||||
{
|
||||
"config": "config/app.json",
|
||||
"mode": 0,
|
||||
"name": "app",
|
||||
"rule": "config/appRule.json",
|
||||
"table": "admin"
|
||||
}
|
||||
],
|
||||
"crossDomain": "",
|
||||
"db": {
|
||||
"mysql": {
|
||||
"host": "192.168.6.253",
|
||||
"name": "iedc_dev",
|
||||
"name": "rfcb",
|
||||
"password": "dasda8454456",
|
||||
"port": "3306",
|
||||
"prefix": "",
|
||||
|
@ -20,16 +20,16 @@
|
||||
},
|
||||
"注释": "可配置memory,db,redis,默认启用memory,默认优先级为memory\u003eredis\u003edb,memory与数据库缓存设置项一致,缓存数据填充会自动反方向反哺,加入memory缓存过期将自动从redis更新,但memory永远不会更新redis,如果是集群建议不要开启memory,配置即启用"
|
||||
},
|
||||
"codeConfig": {
|
||||
"admin": {
|
||||
"codeConfig": [
|
||||
"注释:配置即启用,非必须,默认无",
|
||||
{
|
||||
"config": "默认config/app.json,必须,接口描述配置文件",
|
||||
"mode": "默认0,非必须,0为内嵌代码模式,1为生成代码模式",
|
||||
"name": "默认无,非必须,有则生成代码到此目录,无则采用缺省模式使用表名,如设置为:admin,将在admin目录生成包名为admin的代码",
|
||||
"rule": "默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成",
|
||||
"注释": "默认admin,非必须,有则根据数据库内当前表名做为用户生成数据"
|
||||
},
|
||||
"注释": "配置即启用,非必须,默认无"
|
||||
},
|
||||
"table": "默认admin,必须,根据数据库内当前表名做为用户生成数据"
|
||||
}
|
||||
],
|
||||
"crossDomain": "默认空 非必须,空字符串为不开启,如果需要跨域设置,auto为智能开启所有网站允许跨域,http://www.baidu.com为指定域允许跨域",
|
||||
"db": {
|
||||
"mysql": {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
//. "code.hoteas.com/golang/hotime/common"
|
||||
"fmt"
|
||||
"time"
|
||||
@ -11,8 +11,21 @@ func main() {
|
||||
date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00")
|
||||
fmt.Println(date, date.Unix())
|
||||
//fmt.Println("0123456"[1:7])
|
||||
appIns := hotime.Init("config/config.json")
|
||||
appIns := Init("config/config.json")
|
||||
//a:=Map{}
|
||||
//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
15
var.go
@ -14,9 +14,9 @@ var Config = Map{
|
||||
//"codeConfig": Map{
|
||||
// "admin": "config/app.json",
|
||||
//},
|
||||
"codeConfig": Map{
|
||||
"admin": Map{
|
||||
//"table":"admin",//默认admin,必须,有则根据数据库内当前表名做为用户生成数据
|
||||
"codeConfig": Slice{
|
||||
Map{
|
||||
"table": "admin", //默认admin,必须,有则根据数据库内当前表名做为用户生成数据
|
||||
"name": "", //默认admin,非必须,有则生成代码到此目录,无则采用缺省模式使用表名
|
||||
"config": "config/app.json", //默认config/app.json,必须,接口描述配置文件
|
||||
"rule": "config/rule.json", //默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成
|
||||
@ -61,10 +61,11 @@ var ConfigNote = Map{
|
||||
//
|
||||
// "packageName": "默认无,必须,包名称以及应用名,生成代码的配置文件地址,比如config/app.json,数据库有更新时自动更新配置文件以及对应的生成文件",
|
||||
//},
|
||||
"codeConfig": Map{
|
||||
"注释": "配置即启用,非必须,默认无",
|
||||
"admin": Map{ //默认无,必须,接口类别名称
|
||||
"注释": "默认admin,非必须,有则根据数据库内当前表名做为用户生成数据", //
|
||||
"codeConfig": Slice{
|
||||
"注释:配置即启用,非必须,默认无",
|
||||
Map{ //默认无,必须,接口类别名称
|
||||
//"注释": "", //
|
||||
"table": "默认admin,必须,根据数据库内当前表名做为用户生成数据",
|
||||
"name": "默认无,非必须,有则生成代码到此目录,无则采用缺省模式使用表名,如设置为:admin,将在admin目录生成包名为admin的代码",
|
||||
"config": "默认config/app.json,必须,接口描述配置文件", //
|
||||
"rule": "默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成",
|
||||
|
Loading…
Reference in New Issue
Block a user