From ff126dfbaaa2515933586baf3f408cdd09d4795c Mon Sep 17 00:00:00 2001 From: John Date: Tue, 30 Oct 2018 23:58:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=86=E6=9E=B6=E4=B8=80=E4=BA=9B=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9A=84=E7=BB=86=E8=8A=82=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 2 ++ g/frame/gins/gins.go | 11 ++++--- g/g_func.go | 10 ------ g/g_logger.go | 16 ++++++++++ g/net/ghttp/ghttp_server.go | 9 ++++-- g/net/ghttp/ghttp_server_config.go | 8 ++++- g/os/gcfg/gcfg.go | 37 ++++++++++++---------- g/os/glog/glog_logger.go | 13 +------- g/os/gspath/gspath.go | 51 +++++++++++++++++------------- g/os/gview/gview.go | 22 ++++++++++--- g/util/gregex/gregex.go | 6 ++-- geg/other/test2.go | 16 ++++++++-- 12 files changed, 121 insertions(+), 80 deletions(-) diff --git a/TODO b/TODO index 7ba360abe..740646c93 100644 --- a/TODO +++ b/TODO @@ -43,6 +43,8 @@ ghttp hook回调使用方式在注册路由比较多的时候,优先级可能 gform参考 https://gohouse.github.io/gorose/dist/index.html 进行改进 完善配置管理章节,说明默认的配置文件更改方式; 完善gform配置管理说明,g.DB/Database和gdb.New的区别; +改进gconv.Struct方法,支持不区分大小写的属性-键名匹配,便于开发者执行对象转换; + DONE: diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go index 76d409407..6404848e6 100644 --- a/g/frame/gins/gins.go +++ b/g/frame/gins/gins.go @@ -12,6 +12,7 @@ import ( "gitee.com/johng/gf/g/os/gcfg" "gitee.com/johng/gf/g/os/gcmd" "gitee.com/johng/gf/g/os/genv" + "gitee.com/johng/gf/g/os/glog" "gitee.com/johng/gf/g/os/gview" "gitee.com/johng/gf/g/os/gfile" "gitee.com/johng/gf/g/container/gmap" @@ -125,7 +126,7 @@ func Database(name...string) *gdb.Db { db := instances.GetOrSetFuncLock(key, func() interface{} { m := config.GetMap("database") if m == nil { - panic(fmt.Sprintf(`incomplete configuration for database: "database" node not found in config file "%s"`, config.GetFilePath())) + glog.Errorfln(`incomplete configuration for database: "database" node not found in config file "%s"`, config.GetFilePath()) } for group, v := range m { cg := gdb.ConfigGroup{} @@ -181,7 +182,7 @@ func Database(name...string) *gdb.Db { if db, err := gdb.New(name...); err == nil { return db } else { - panic(err) + glog.Error(err) } return nil }) @@ -213,13 +214,13 @@ func Redis(name...string) *gredis.Redis { Pass : array[4], }) } else { - panic(fmt.Sprintf(`invalid redis node configuration: "%s"`, line)) + glog.Errorfln(`invalid redis node configuration: "%s"`, line) } } else { - panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group)) + glog.Errorfln(`configuration for redis not found for group "%s"`, group) } } else { - panic(fmt.Sprintf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.GetFilePath())) + glog.Errorfln(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.GetFilePath()) } return nil }) diff --git a/g/g_func.go b/g/g_func.go index bef7e7fd5..78459c0e0 100644 --- a/g/g_func.go +++ b/g/g_func.go @@ -33,16 +33,6 @@ func Wait() { ghttp.Wait() } -// 是否显示调试信息 -func SetDebug(debug bool) { - glog.SetDebug(debug) -} - -// 设置日志的显示等级 -func SetLogLevel(level int) { - glog.SetLevel(level) -} - // 打印变量 func Dump(i...interface{}) { gutil.Dump(i...) diff --git a/g/g_logger.go b/g/g_logger.go index 6532bc601..ac9f1d6cf 100644 --- a/g/g_logger.go +++ b/g/g_logger.go @@ -6,3 +6,19 @@ package g +import "gitee.com/johng/gf/g/os/glog" + +// 是否显示调试信息 +func SetDebug(debug bool) { + glog.SetDebug(debug) +} + +// 设置日志的显示等级 +func SetLogLevel(level int) { + glog.SetLevel(level) +} + +// 获取设置的日志显示等级 +func GetLogLevel() int { + return glog.GetLevel() +} \ No newline at end of file diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index 3ac8f322f..2a4979588 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -213,9 +213,14 @@ func (s *Server) Start() error { // 如果设置了静态文件目录,那么优先按照静态文件目录进行检索,其次是当前可执行文件工作目录; // 并且如果是开发环境,默认也会添加main包的源码目录路径做为二级检索。 if s.config.ServerRoot != "" { - s.paths.Set(s.config.ServerRoot) + if rp, err := s.paths.Set(s.config.ServerRoot); err != nil { + glog.Error("ghttp.SetServerRoot failed:", err.Error()) + return err + } else { + glog.Debug("ghttp.SetServerRoot:", rp) + } } - s.paths.Add(gfile.SelfDir()) + s.AddSearchPath(gfile.SelfDir()) if p := gfile.MainPkgPath(); p != "" && gfile.Exists(p) { s.paths.Add(p) } diff --git a/g/net/ghttp/ghttp_server_config.go b/g/net/ghttp/ghttp_server_config.go index 3b84a7652..ae3bd90ed 100644 --- a/g/net/ghttp/ghttp_server_config.go +++ b/g/net/ghttp/ghttp_server_config.go @@ -303,7 +303,13 @@ func (s *Server) SetDumpRouteMap(enabled bool) { // 添加静态文件搜索目录,必须给定目录的绝对路径 func (s *Server) AddSearchPath(path string) error { - return s.paths.Add(path) + if rp, err := s.paths.Add(path); err != nil { + glog.Error("ghttp.AddSearchPath failed:", err.Error()) + return err + } else { + glog.Debug("ghttp.AddSearchPath:", rp) + } + return nil } // 获取 diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index a7e5094d4..08530608e 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -9,14 +9,14 @@ package gcfg import ( - "gitee.com/johng/gf/g/container/gvar" - "gitee.com/johng/gf/g/os/gspath" - "gitee.com/johng/gf/g/os/gfsnotify" - "gitee.com/johng/gf/g/container/gmap" - "gitee.com/johng/gf/g/encoding/gjson" - "gitee.com/johng/gf/g/container/gtype" "errors" + "gitee.com/johng/gf/g/container/gmap" + "gitee.com/johng/gf/g/container/gtype" + "gitee.com/johng/gf/g/container/gvar" + "gitee.com/johng/gf/g/encoding/gjson" + "gitee.com/johng/gf/g/os/gfsnotify" "gitee.com/johng/gf/g/os/glog" + "gitee.com/johng/gf/g/os/gspath" ) const ( @@ -37,14 +37,14 @@ func New(path string, file...string) *Config { if len(file) > 0 { name = file[0] } - s := gspath.New() - s.Set(path) - return &Config { + c := &Config { name : gtype.NewString(name), - paths : s, + paths : gspath.New(), jsons : gmap.NewStringInterfaceMap(), vc : gtype.NewBool(), } + c.SetPath(path) + return c } // 判断从哪个配置文件中获取内容,返回配置文件的绝对路径 @@ -58,12 +58,13 @@ func (c *Config) filePath(file...string) string { // 设置配置管理器的配置文件存放目录绝对路径 func (c *Config) SetPath(path string) error { - if err := c.paths.Set(path); err != nil { - glog.Error("gcfg.SetPath failed:", path, err) + if rp, err := c.paths.Set(path); err != nil { + glog.Error("gcfg.SetPath failed:", err.Error()) return err + } else { + c.jsons.Clear() + glog.Debug("gcfg.SetPath:", rp) } - c.jsons.Clear() - glog.Debug("gcfg.SetPath:", path) return nil } @@ -76,11 +77,12 @@ func (c *Config) SetViolenceCheck(check bool) { // 添加配置管理器的配置文件搜索路径 func (c *Config) AddPath(path string) error { - if err := c.paths.Add(path); err != nil { - glog.Debug("gcfg.AddPath failed:", path, err) + if rp, err := c.paths.Add(path); err != nil { + glog.Debug("gcfg.AddPath failed:", err.Error()) return err + } else { + glog.Debug("gcfg.AddPath:", rp) } - glog.Debug("gcfg.AddPath:", path) return nil } @@ -95,6 +97,7 @@ func (c *Config) GetFilePath(file...string) string { // 设置配置管理对象的默认文件名称 func (c *Config) SetFileName(name string) { + glog.Debug("gcfg.SetFileName:", name) c.name.Set(name) } diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index fce9a5a18..83bd7b051 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -246,19 +246,8 @@ func (l *Logger) GetBacktrace(skip...int) string { } backtrace := "" index := 1 - from := 0 - // 首先定位业务文件开始位置 - for i := 0; i < 10; i++ { - if _, cfile, _, ok := runtime.Caller(i); ok { - if !gregex.IsMatchString("/g/os/glog/glog.+$", cfile) { - from = i - break - } - } - } - // 从业务文件开始位置根据自定义的skip开始backtrace goroot := runtime.GOROOT() - for i := from + customSkip + l.btSkip.Val(); i < 10000; i++ { + for i := customSkip + l.btSkip.Val(); i < 10000; i++ { if _, cfile, cline, ok := runtime.Caller(i); ok && cfile != "" { // 不打印出go源码路径及glog包文件路径,日志打印必须从业务源码文件开始,且从glog包文件开始检索 if (goroot == "" || !gregex.IsMatchString("^" + goroot, cfile)) && !gregex.IsMatchString(``, cfile) { diff --git a/g/os/gspath/gspath.go b/g/os/gspath/gspath.go index c0d25edee..38d0bde35 100644 --- a/g/os/gspath/gspath.go +++ b/g/os/gspath/gspath.go @@ -10,6 +10,7 @@ package gspath import ( "errors" + "fmt" "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/os/gfile" "gitee.com/johng/gf/g/os/gfsnotify" @@ -32,46 +33,52 @@ func New () *SPath { } // 设置搜索路径,只保留当前设置项,其他搜索路径被清空 -func (sp *SPath) Set(path string) error { - r := gfile.RealPath(path) - if r == "" { - r = sp.Search(path) - if r == "" { - r = gfile.RealPath(gfile.Pwd() + gfile.Separator + path) +func (sp *SPath) Set(path string) (realpath string, err error) { + realpath = gfile.RealPath(path) + if realpath == "" { + realpath = sp.Search(path) + if realpath == "" { + realpath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path) } } - if r != "" && gfile.IsDir(r) { - r = strings.TrimRight(r, gfile.Separator) + if realpath == "" { + return realpath, errors.New(fmt.Sprintf(`path "%s" does not exist`, path)) + } + if realpath != "" && gfile.IsDir(realpath) { + realpath = strings.TrimRight(realpath, gfile.Separator) sp.mu.Lock() - sp.paths = []string{r} + sp.paths = []string{realpath} sp.mu.Unlock() sp.cache.Clear() //glog.Debug("gspath.SetPath:", r) - return nil + return realpath, nil } //glog.Warning("gspath.SetPath failed:", path) - return errors.New("invalid path:" + path) + return realpath, errors.New("invalid path:" + path) } // 添加搜索路径 -func (sp *SPath) Add(path string) error { - r := gfile.RealPath(path) - if r == "" { - r = sp.Search(path) - if r == "" { - r = gfile.RealPath(gfile.Pwd() + gfile.Separator + path) +func (sp *SPath) Add(path string) (realpath string, err error) { + realpath = gfile.RealPath(path) + if realpath == "" { + realpath = sp.Search(path) + if realpath == "" { + realpath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path) } } - if r != "" && gfile.IsDir(r) { - r = strings.TrimRight(r, gfile.Separator) + if realpath == "" { + return realpath, errors.New(fmt.Sprintf(`path "%s" does not exist`, path)) + } + if realpath != "" && gfile.IsDir(realpath) { + realpath = strings.TrimRight(realpath, gfile.Separator) sp.mu.Lock() - sp.paths = append(sp.paths, r) + sp.paths = append(sp.paths, realpath) sp.mu.Unlock() //glog.Debug("gspath.Add:", r) - return nil + return realpath, nil } //glog.Warning("gspath.Add failed:", path) - return errors.New("invalid path:" + path) + return realpath, errors.New("invalid path:" + path) } // 按照优先级搜索文件,返回搜索到的文件绝对路径,找不到该文件时,返回空字符串 diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 7a16c80d6..011024b2d 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -9,6 +9,7 @@ package gview import ( "gitee.com/johng/gf/g/encoding/gurl" + "gitee.com/johng/gf/g/os/glog" "gitee.com/johng/gf/g/os/gtime" "gitee.com/johng/gf/g/util/gstr" "strings" @@ -70,14 +71,13 @@ func Get(path string) *View { // 生成一个视图对象 func New(path string) *View { - s := gspath.New() - s.Set(path) view := &View { - paths : s, + paths : gspath.New(), data : make(map[string]interface{}), funcmap : make(map[string]interface{}), delimiters : make([]string, 2), } + view.SetPath(path) view.SetDelimiters("{{", "}}") // 内置方法 view.BindFunc("text", view.funcText) @@ -98,12 +98,24 @@ func New(path string) *View { // 设置模板目录绝对路径 func (view *View) SetPath(path string) error { - return view.paths.Set(path) + if rp, err := view.paths.Set(path); err != nil { + glog.Error("gview.SetPath failed:", err.Error()) + return err + } else { + glog.Debug("gview.SetPath:", rp) + } + return nil } // 添加模板目录搜索路径 func (view *View) AddPath(path string) error { - return view.paths.Add(path) + if rp, err := view.paths.Add(path); err != nil { + glog.Error("gview.AddPath failed:", err.Error()) + return err + } else { + glog.Debug("gview.SetPath:", rp) + } + return nil } // 批量绑定模板变量,即调用之后每个线程都会生效,因此有并发安全控制 diff --git a/g/util/gregex/gregex.go b/g/util/gregex/gregex.go index 73d92ae88..c6c74ebbc 100644 --- a/g/util/gregex/gregex.go +++ b/g/util/gregex/gregex.go @@ -8,12 +8,12 @@ package gregex import ( + "gitee.com/johng/gf/g/container/gmap" "regexp" - "gitee.com/johng/gf/g/os/gcache" ) // 缓存对象,主要用于缓存底层regx对象 -var regxCache = gcache.New() +var regxCache = gmap.NewStringInterfaceMap(true) // 根据pattern生成对应的regexp正则对象 func getRegexp(pattern string) (*regexp.Regexp, error) { @@ -21,7 +21,7 @@ func getRegexp(pattern string) (*regexp.Regexp, error) { return v.(*regexp.Regexp), nil } if r, err := regexp.Compile(pattern); err == nil { - regxCache.Set(pattern, r, 0) + regxCache.Set(pattern, r) return r, nil } else { return nil, err diff --git a/geg/other/test2.go b/geg/other/test2.go index d51d46561..e2811e825 100644 --- a/geg/other/test2.go +++ b/geg/other/test2.go @@ -1,10 +1,20 @@ package main import ( - "fmt" - "gitee.com/johng/gf/g/os/gfile" + "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/net/ghttp" ) + func main() { - fmt.Println(gfile.Dir("c:\111\222")) + s := g.Server() + s.Domain("www.a.com").BindHandler("/*", func(r *ghttp.Request) { + r.Response.ServeFile("/home/john/www1" + r.URL.Path) + }) + s.Domain("www.b.com").BindHandler("/*", func(r *ghttp.Request) { + r.Response.ServeFile("/home/john/www2" + r.URL.Path) + }) + s.SetIndexFolder(true) + s.SetPort(8080) + s.Run() } \ No newline at end of file