From f37b9aee709ef91e4f558b3918c091aa34deafa8 Mon Sep 17 00:00:00 2001 From: hoteas <等> Date: Mon, 11 Jul 2022 11:07:17 +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 | 4 + db/hotimedb.go | 21 +- example/config/config.json | 38 ++++ example/config/configNote.json | 78 +++++++ example/config/data.db | Bin 0 -> 16384 bytes example/config/rule.json | 362 +++++++++++++++++++++++++++++++++ example/main.go | 27 +++ log/logrus.go | 19 +- 8 files changed, 541 insertions(+), 8 deletions(-) create mode 100644 example/config/config.json create mode 100644 example/config/configNote.json create mode 100644 example/config/data.db create mode 100644 example/config/rule.json create mode 100644 example/main.go diff --git a/application.go b/application.go index 5f2eeb1..cf5488b 100644 --- a/application.go +++ b/application.go @@ -615,6 +615,8 @@ func SetMysqlDB(appIns *Application, config Map) { appIns.Db.Type = "mysql" appIns.Db.DBName = config.GetString("name") appIns.Db.Prefix = config.GetString("prefix") + appIns.Db.Log = appIns.Log + appIns.Db.Mode = appIns.Config.GetCeilInt("mode") appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { //master数据库配置 query := config.GetString("user") + ":" + config.GetString("password") + @@ -644,6 +646,8 @@ func SetSqliteDB(appIns *Application, config Map) { appIns.Db.Type = "sqlite" appIns.Db.Prefix = config.GetString("prefix") + appIns.Db.Mode = appIns.Config.GetCeilInt("mode") + appIns.Db.Log = appIns.Log appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { db, e := sql.Open("sqlite3", config.GetString("path")) if e != nil && len(err) != 0 { diff --git a/db/hotimedb.go b/db/hotimedb.go index 46c76bd..758fe26 100644 --- a/db/hotimedb.go +++ b/db/hotimedb.go @@ -8,6 +8,7 @@ import ( "errors" _ "github.com/go-sql-driver/mysql" _ "github.com/mattn/go-sqlite3" + "github.com/sirupsen/logrus" "os" "reflect" "sort" @@ -19,6 +20,7 @@ type HoTimeDB struct { ContextBase DBName string *cache.HoTimeCache + Log *logrus.Logger Type string Prefix string LastQuery string @@ -28,6 +30,7 @@ type HoTimeDB struct { limit Slice *sql.Tx //事务对象 SlaveDB *sql.DB + Mode int //mode为0生产模式,1、为测试模式、2为开发模式 } // SetConnect 设置数据库配置连接 @@ -306,7 +309,11 @@ func (that *HoTimeDB) md5(query string, args ...interface{}) string { } func (that *HoTimeDB) Query(query string, args ...interface{}) []Map { - + defer func() { + if that.Mode == 2 { + that.Log.Info("SQL:"+that.LastQuery, " DATA:", that.LastData, " ERROR:", that.LastErr.GetError()) + } + }() //fmt.Println(query) var err error var resl *sql.Rows @@ -348,7 +355,11 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map { } func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) { - + defer func() { + if that.Mode == 2 { + that.Log.Info("SQL: "+that.LastQuery, " DATA: ", that.LastData, " ERROR: ", that.LastErr.GetError()) + } + }() that.LastQuery = query that.LastData = args var e error @@ -480,7 +491,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { temp, resWhere := that.where(where) - query += temp + query += temp + ";" qs = append(qs, resWhere...) md5 := that.md5(query, qs...) @@ -982,7 +993,7 @@ func (that *HoTimeDB) Update(table string, data Map, where Map) int64 { temp, resWhere := that.where(where) //fmt.Println(resWhere) - query += temp + query += temp + ";" qs = append(qs, resWhere...) res, err := that.Exec(query, qs...) @@ -1007,7 +1018,7 @@ func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 { query := "DELETE FROM " + that.Prefix + table + " " temp, resWhere := that.where(data) - query += temp + query += temp + ";" res, err := that.Exec(query, resWhere...) rows := int64(0) diff --git a/example/config/config.json b/example/config/config.json new file mode 100644 index 0000000..ea49547 --- /dev/null +++ b/example/config/config.json @@ -0,0 +1,38 @@ +{ + "cache": { + "memory": { + "db": true, + "session": true, + "timeout": 7200 + } + }, + "codeConfig": [ + { + "config": "config/app.json", + "mode": 0, + "name": "", + "rule": "config/rule.json", + "table": "admin" + } + ], + "db": { + "sqlite": { + "path": "config/data.db" + } + }, + "defFile": [ + "index.html", + "index.htm" + ], + "error": { + "1": "内部系统异常", + "2": "访问权限异常", + "3": "请求参数异常", + "4": "数据处理异常", + "5": "数据结果异常" + }, + "mode": 2, + "port": "80", + "sessionName": "HOTIME", + "tpt": "tpt" +} \ No newline at end of file diff --git a/example/config/configNote.json b/example/config/configNote.json new file mode 100644 index 0000000..aa87daf --- /dev/null +++ b/example/config/configNote.json @@ -0,0 +1,78 @@ +{ + "cache": { + "db": { + "db": "默认false,非必须,缓存数据库,启用后能减少数据库的读写压力", + "session": "默认true,非必须,缓存web session,同时缓存session保持的用户缓存", + "timeout": "默认60 * 60 * 24 * 30,非必须,过期时间,超时自动删除" + }, + "memory": { + "db": "默认true,非必须,缓存数据库,启用后能减少数据库的读写压力", + "session": "默认true,非必须,缓存web session,同时缓存session保持的用户缓存", + "timeout": "默认60 * 60 * 2,非必须,过期时间,超时自动删除" + }, + "redis": { + "db": "默认true,非必须,缓存数据库,启用后能减少数据库的读写压力", + "host": "默认服务ip:127.0.0.1,必须,如果需要使用redis服务时配置,", + "password": "默认密码空,必须,如果需要使用redis服务时配置,默认密码空", + "port": "默认服务端口:6379,必须,如果需要使用redis服务时配置,", + "session": "默认true,非必须,缓存web session,同时缓存session保持的用户缓存", + "timeout": "默认60 * 60 * 24 * 15,非必须,过期时间,超时自动删除" + }, + "注释": "可配置memory,db,redis,默认启用memory,默认优先级为memory\u003eredis\u003edb,memory与数据库缓存设置项一致,缓存数据填充会自动反方向反哺,加入memory缓存过期将自动从redis更新,但memory永远不会更新redis,如果是集群建议不要开启memory,配置即启用" + }, + "codeConfig": [ + "注释:配置即启用,非必须,默认无", + { + "config": "默认config/app.json,必须,接口描述配置文件", + "mode": "默认0,非必须,0为内嵌代码模式,1为生成代码模式,在开发模式下会显示更多的数据用于开发测试,并能够辅助研发,自动生成配置文件、代码等功能,", + "name": "默认无,非必须,有则生成代码到此目录,无则采用缺省模式使用表名,如设置为:admin,将在admin目录生成包名为admin的代码", + "rule": "默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成", + "table": "默认admin,必须,根据数据库内当前表名做为用户生成数据" + } + ], + "crossDomain": "默认空 非必须,空字符串为不开启,如果需要跨域设置,auto为智能开启所有网站允许跨域,http://www.baidu.com为指定域允许跨域", + "db": { + "mysql": { + "host": "默认127.0.0.1,必须,数据库ip地址", + "name": "默认test,必须,数据库名称", + "password": "默认root,必须,数据库密码", + "port": "默认3306,必须,数据库端口", + "prefix": "默认空,非必须,数据表前缀", + "slave": { + "host": "默认127.0.0.1,必须,数据库ip地址", + "name": "默认test,必须,数据库名称", + "password": "默认root,必须,数据库密码", + "port": "默认3306,必须,数据库端口", + "user": "默认root,必须,数据库用户名", + "注释": "从数据库配置,mysql里配置slave项即启用主从读写,减少数据库压力" + }, + "user": "默认root,必须,数据库用户名", + "注释": "除prefix及主从数据库slave项,其他全部必须" + }, + "sqlite": { + "path": "默认config/data.db,必须,数据库位置" + }, + "注释": "配置即启用,非必须,默认使用sqlite数据库" + }, + "defFile": "默认访问index.html或者index.htm文件,必须,默认访问文件类型", + "error": { + "1": "内部系统异常,在环境配置,文件访问权限等基础运行环境条件不足造成严重错误时使用", + "2": "访问权限异常,没有登录或者登录异常等时候使用", + "3": "请求参数异常,request参数不满足要求,比如参数不足,参数类型错误,参数不满足要求等时候使用", + "4": "数据处理异常,数据库操作或者三方请求返回的结果非正常结果,比如数据库突然中断等时候使用", + "5": "数据结果异常,一般用于无法给出response要求的格式要求下使用,比如response需要的是string格式但你只能提供int数据时", + "注释": "web服务内置错误提示,自定义异常建议10开始" + }, + "logFile": "无默认,非必须,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置", + "logLevel": "默认0,必须,0关闭,1打印,日志等级", + "mode": "默认0,非必须,0生产模式,1,测试模式,2开发模式,web无缓存,数据库不启用缓存", + "modeRouterStrict": "默认false,必须,路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配", + "port": "默认80,必须,web服务开启Http端口,0为不启用http服务,默认80", + "sessionName": "默认HOTIME,必须,设置session的cookie名", + "tlsCert": "默认空,非必须,https证书", + "tlsKey": "默认空,非必须,https密钥", + "tlsPort": "默认空,非必须,web服务https端口,0为不启用https服务", + "tpt": "默认tpt,必须,web静态文件目录,默认为程序目录下tpt目录", + "webConnectLogFile": "无默认,非必须,webConnectLogShow开启之后才能使用,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置", + "webConnectLogShow": "默认true,非必须,访问日志如果需要web访问链接、访问ip、访问时间打印,false为关闭true开启此功能" +} \ No newline at end of file diff --git a/example/config/data.db b/example/config/data.db new file mode 100644 index 0000000000000000000000000000000000000000..3935bce0dd73a866294ca4ef51f3f6417ee9238c GIT binary patch literal 16384 zcmeI&!A`<37zgl{5o9wuFUzj=wgqA^zJRDZkPspYiN*t&te6GX!PZ4M@`yf+9z5Bi z81dkRiy{AJP5X6ixBhNlw%cwymYj{!ED0q$BS(Z%a?Thb#8b7b>bjIv=VwgUH~wfn za`MnPP!o*97h>o3v1(8t009U<00Izz00bZa0SG_<0^198jEd_!G>JvT=S9x5K^zT= z@2Grv6Eu4P>vvk+e!yCttKg1pPP5EZtcI8uNB(uG;?`>PUWShop3f#KTOQu{_I+BNhwEpHyU;!&Kxl z3)R5qD^JI2(41y*5@riF<_i`UGL6MB;|UkCZkhU_O!B$z=Q328zFr*hQCLhQYgoR{ z%~jG*?APt-u>%Ak009U<00Izz00bZa0SG_<0{=^3->piX%Tt~IQ~Qmm3k3oYfB*y_ z009U<00Izz00bZaft?blQp2J87X#bU=hEN*-*@VOI1B_J009U<00Izz00bZa0SG_< K0&9VaZGQj&_I3~e literal 0 HcmV?d00001 diff --git a/example/config/rule.json b/example/config/rule.json new file mode 100644 index 0000000..323ba9f --- /dev/null +++ b/example/config/rule.json @@ -0,0 +1,362 @@ +[ + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "idcard", + "strict": false, + "type": "" + }, + { + "edit": false, + "info": true, + "list": true, + "must": false, + "name": "id", + "strict": true, + "type": "" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "parent_id", + "strict": true, + "type": "" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "amount", + "strict": true, + "type": "money" + }, + { + "edit": false, + "info": false, + "list": false, + "must": false, + "name": "content", + "strict": false, + "type": "textArea" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "info", + "strict": false, + "type": "textArea" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "status", + "strict": false, + "type": "select" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "state", + "strict": false, + "type": "select" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "sex", + "strict": false, + "type": "select" + }, + { + "edit": false, + "info": false, + "list": false, + "must": false, + "name": "delete", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "lat", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "lng", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "latitude", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "longitude", + "strict": false, + "type": "" + }, + { + "edit": false, + "info": false, + "list": false, + "must": false, + "name": "index", + "strict": false, + "type": "index" + }, + { + "edit": true, + "info": false, + "list": false, + "must": false, + "name": "password", + "strict": false, + "type": "password" + }, + { + "edit": true, + "info": false, + "list": false, + "must": false, + "name": "pwd", + "strict": false, + "type": "password" + }, + { + "edit": false, + "info": false, + "list": false, + "must": false, + "name": "version", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "seq", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "sort", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "note", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "description", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "abstract", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "content", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "address", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "full_name", + "strict": false, + "type": "" + }, + { + "edit": false, + "info": true, + "list": false, + "must": false, + "name": "create_time", + "strict": true, + "type": "time" + }, + { + "edit": false, + "info": true, + "list": true, + "must": false, + "name": "modify_time", + "strict": true, + "type": "time" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "image", + "strict": false, + "type": "image" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "img", + "strict": false, + "type": "image" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "icon", + "strict": false, + "type": "image" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "avatar", + "strict": false, + "type": "image" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "file", + "strict": false, + "type": "file" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "age", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": false, + "must": false, + "name": "email", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "time", + "strict": false, + "type": "time" + }, + { + "edit": false, + "info": true, + "list": false, + "must": false, + "name": "level", + "strict": false, + "type": "" + }, + { + "edit": true, + "info": true, + "list": true, + "must": false, + "name": "rule", + "strict": false, + "type": "form" + }, + { + "edit": false, + "info": true, + "list": true, + "must": false, + "name": "table", + "strict": false, + "type": "table" + }, + { + "edit": false, + "info": true, + "list": true, + "must": false, + "name": "table_id", + "strict": false, + "type": "table_id" + } +] \ No newline at end of file diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..243dcc4 --- /dev/null +++ b/example/main.go @@ -0,0 +1,27 @@ +package main + +import ( + . "code.hoteas.com/golang/hotime" + . "code.hoteas.com/golang/hotime/common" + "fmt" +) + +func main() { + + appIns := Init("config/config.json") + appIns.Run(Router{"app": Proj{ + "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"}) + 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}) + }, + }, + }, + }) + +} diff --git a/log/logrus.go b/log/logrus.go index 01d09a3..7011118 100644 --- a/log/logrus.go +++ b/log/logrus.go @@ -82,10 +82,23 @@ func findCaller(skip int) string { for i := 0; i < 10; i++ { file, line = getCaller(skip + i) if !strings.HasPrefix(file, "logrus") { - - if file == "common/error.go" { - file, line = getCaller(skip + i + 1) + j := 0 + for true { + j++ + if file == "common/error.go" { + file, line = getCaller(skip + i + j) + } + if file == "db/hotimedb.go" { + file, line = getCaller(skip + i + j) + } + //if file == "code/makecode.go" { + // file, line = getCaller(skip + i + j) + //} + if j == 5 { + break + } } + break } }