diff --git a/TODO.MD b/TODO.MD index 22b8055ef..d6f0cf540 100644 --- a/TODO.MD +++ b/TODO.MD @@ -98,4 +98,4 @@ 1. `gfsnotify`增加添加监听文件时的监听ID返回,以便调用端删除监听时只删除自己添加的监听,而不影响其他对该同一文件的监听回调; 1. `gfsnotify`针对添加目录监听时无法使用多个`Watcher`,考虑改进,并考虑动态扩容全局`Watcher`方案; 1. 由于系统对inotify实例数量(`fs.inotify.max_user_instances`)以及队列大小(`fs.inotify.max_user_watches`)有限制,需要改进`gfsnotify`; - +1. WebServer事件回调允许对同一个路由规则绑定多个事件回调; diff --git a/g/container/gvar/gvar.go b/g/container/gvar/gvar.go index 8c92ebba7..119e69cf7 100644 --- a/g/container/gvar/gvar.go +++ b/g/container/gvar/gvar.go @@ -8,9 +8,9 @@ package gvar import ( - "time" - "gitee.com/johng/gf/g/util/gconv" "gitee.com/johng/gf/g/container/gtype" + "gitee.com/johng/gf/g/util/gconv" + "time" ) type Var struct { @@ -18,6 +18,35 @@ type Var struct { safe bool // 当为true时,value为 *gtype.Interface 类型 } +// 只读变量接口 +type VarRead interface { + Val() interface{} + IsNil() bool + Bytes() []byte + String() string + Bool() bool + Int() int + Int8() int8 + Int16() int16 + Int32() int32 + Int64() int64 + Uint() uint + Uint8() uint8 + Uint16() uint16 + Uint32() uint32 + Uint64() uint64 + Float32() float32 + Float64() float64 + Interface() interface{} + Ints() []int + Floats() []float64 + Strings() []string + Interfaces() []interface{} + Time(format ...string) time.Time + TimeDuration() time.Duration + Struct(objPointer interface{}, attrMapping ...map[string]string) error +} + // 创建一个动态变量,value参数可以为nil func New(value interface{}, safe...bool) *Var { v := &Var{} @@ -30,6 +59,16 @@ func New(value interface{}, safe...bool) *Var { return v } +// 创建一个只读动态变量,value参数可以为nil +func NewRead(value interface{}, safe...bool) VarRead { + return VarRead(New(value, safe...)) +} + +// 返回动态变量的只读接口 +func (v *Var) ReadOnly() VarRead { + return VarRead(v) +} + func (v *Var) Set(value interface{}) (old interface{}) { if v.safe { old = v.value.(*gtype.Interface).Set(value) @@ -48,6 +87,7 @@ func (v *Var) Val() interface{} { } } +// Val() 别名 func (v *Var) Interface() interface{} { return v.Val() } diff --git a/g/database/gdb/gdb.go b/g/database/gdb/gdb.go index ebe3fb211..362b3a046 100644 --- a/g/database/gdb/gdb.go +++ b/g/database/gdb/gdb.go @@ -108,7 +108,7 @@ type Sql struct { } // 返回数据表记录值 -type Value = *gvar.Var +type Value = gvar.VarRead // 返回数据表记录Map type Record map[string]Value diff --git a/g/net/ghttp/ghttp_request.go b/g/net/ghttp/ghttp_request.go index 3678881e1..94e6364cc 100644 --- a/g/net/ghttp/ghttp_request.go +++ b/g/net/ghttp/ghttp_request.go @@ -20,24 +20,24 @@ import ( // 请求对象 type Request struct { http.Request - parsedGet bool // GET参数是否已经解析 - parsedPost bool // POST参数是否已经解析 - queryVars map[string][]string // GET参数 - routerVars map[string][]string // 路由解析参数 - exit bool // 是否退出当前请求流程执行 - Id int // 请求id(唯一) - Server *Server // 请求关联的服务器对象 - Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全) - Session *Session // 与当前请求绑定的Session对象(并发安全) - Response *Response // 对应请求的返回数据操作对象 - Router *Router // 匹配到的路由对象 - EnterTime int64 // 请求进入时间(微秒) - LeaveTime int64 // 请求完成时间(微秒) - Param interface{} // 开发者自定义参数 - parsedHost string // 解析过后不带端口号的服务器域名名称 - clientIp string // 解析过后的客户端IP地址 - isFileRequest bool // 是否为静态文件请求(非服务请求,当静态文件存在时,优先级会被服务请求高,被识别为文件请求) - isFileServe bool // 是否为文件处理(调用Server.serveFile时设置为true), isFileRequest为true时isFileServe也为true + parsedGet bool // GET参数是否已经解析 + parsedPost bool // POST参数是否已经解析 + queryVars map[string][]string // GET参数 + routerVars map[string][]string // 路由解析参数 + exit bool // 是否退出当前请求流程执行 + Id int // 请求id(唯一) + Server *Server // 请求关联的服务器对象 + Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全) + Session *Session // 与当前请求绑定的Session对象(并发安全) + Response *Response // 对应请求的返回数据操作对象 + Router *Router // 匹配到的路由对象 + EnterTime int64 // 请求进入时间(微秒) + LeaveTime int64 // 请求完成时间(微秒) + params map[string]gvar.VarRead // 开发者自定义参数(请求流程中有效) + parsedHost string // 解析过后不带端口号的服务器域名名称 + clientIp string // 解析过后的客户端IP地址 + isFileRequest bool // 是否为静态文件请求(非服务请求,当静态文件存在时,优先级会被服务请求高,被识别为文件请求) + isFileServe bool // 是否为文件处理(调用Server.serveFile时设置为true), isFileRequest为true时isFileServe也为true } // 创建一个Request对象 @@ -74,7 +74,7 @@ func (r *Request) Get(key string, def ... string) string { return r.GetRequestString(key, def...) } -func (r *Request) GetVar(key string, def ... interface{}) *gvar.Var { +func (r *Request) GetVar(key string, def ... interface{}) gvar.VarRead { return r.GetRequestVar(key, def...) } diff --git a/g/net/ghttp/ghttp_request_params.go b/g/net/ghttp/ghttp_request_params.go new file mode 100644 index 000000000..583b0c7a0 --- /dev/null +++ b/g/net/ghttp/ghttp_request_params.go @@ -0,0 +1,28 @@ +// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. + +package ghttp + +import "gitee.com/johng/gf/g/container/gvar" + +// 设置请求流程共享变量 +func (r *Request) SetParam(key string, value interface{}) { + if r.params == nil { + r.params = make(map[string]gvar.VarRead) + } + r.params[key] = gvar.New(value, false) +} + +// 获取请求流程共享变量 +func (r *Request) GetParam(key string) gvar.VarRead { + if r.params != nil { + if v, ok := r.params[key]; ok { + return v + } + } + return nil +} + diff --git a/g/net/ghttp/ghttp_request_request.go b/g/net/ghttp/ghttp_request_request.go index a601b9afc..71730b00f 100644 --- a/g/net/ghttp/ghttp_request_request.go +++ b/g/net/ghttp/ghttp_request_request.go @@ -26,7 +26,7 @@ func (r *Request) GetRequest(key string, def ... []string) []string { return v } -func (r *Request) GetRequestVar(key string, def ... interface{}) *gvar.Var { +func (r *Request) GetRequestVar(key string, def ... interface{}) gvar.VarRead { value := r.GetRequest(key) if value != nil { return gvar.New(value) diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 9ddee34c6..585f10be8 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -131,7 +131,7 @@ func (c *Config) Get(pattern string, file...string) interface{} { } // 获得配置项,返回动态变量 -func (c *Config) GetVar(pattern string, file...string) *gvar.Var { +func (c *Config) GetVar(pattern string, file...string) gvar.VarRead { if j := c.getJson(file...); j != nil { return gvar.New(j.Get(pattern)) } diff --git a/geg/container/gvar/g.var.go b/geg/container/gvar/g.var.go index 95c3eb2ab..9eaa79dfa 100644 --- a/geg/container/gvar/g.var.go +++ b/geg/container/gvar/g.var.go @@ -29,4 +29,8 @@ func main() { s := new(Score) v.Struct(s) fmt.Println(s) + + // 只读接口 + r := v.ReadOnly() + fmt.Println(r.String()) } diff --git a/geg/database/orm/mysql/gdb_value.go b/geg/database/orm/mysql/gdb_value.go index d533806ec..d3e894fac 100644 --- a/geg/database/orm/mysql/gdb_value.go +++ b/geg/database/orm/mysql/gdb_value.go @@ -7,10 +7,10 @@ import ( func main() { gdb.AddDefaultConfigNode(gdb.ConfigNode { - Host : "127.0.0.1", + Host : "192.168.1.11", Port : "3306", User : "root", - Pass : "123456", + Pass : "8692651", Name : "test", Type : "mysql", Role : "master", diff --git a/geg/net/ghttp/server/hooks/hooks_param.go b/geg/net/ghttp/server/hooks/hooks_param.go new file mode 100644 index 000000000..2a8192deb --- /dev/null +++ b/geg/net/ghttp/server/hooks/hooks_param.go @@ -0,0 +1,20 @@ +package main + +import ( + "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/net/ghttp" +) + +func main() { + s := g.Server() + s.BindHandler("/", func(r *ghttp.Request) { + r.Response.Writeln(r.GetParam("name").String()) + }) + s.BindHookHandlerByMap("/", map[string]ghttp.HandlerFunc { + ghttp.HOOK_BEFORE_SERVE : func(r *ghttp.Request) { + r.SetParam("name", "john") + }, + }) + s.SetPort(8199) + s.Run() +} \ No newline at end of file