diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go index 554077164..963d36dd5 100644 --- a/g/frame/gins/gins.go +++ b/g/frame/gins/gins.go @@ -76,10 +76,12 @@ func View(name...string) *gview.View { if path == "" { path = genv.Get("GF_VIEWPATH") if path == "" { - path = gfile.SelfDir() + if gfile.SelfDir() != gfile.TempDir() { + path = gfile.SelfDir() + } } } - view := gview.Get(path) + view := gview.New(path) // 添加基于源码的搜索目录检索地址,常用于开发环境调试,只添加入口文件目录 if p := gfile.MainPkgPath(); p != "" && gfile.Exists(p) { view.AddPath(p) @@ -103,7 +105,9 @@ func Config(file...string) *gcfg.Config { if path == "" { path = genv.Get("GF_CFGPATH") if path == "" { - path = gfile.SelfDir() + if gfile.SelfDir() != gfile.TempDir() { + path = gfile.SelfDir() + } } } config := gcfg.New(path, configFile) diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index 4ea764267..223ee70fd 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -224,7 +224,9 @@ func (s *Server) Start() error { } } // 添加当前可执行文件运行目录到搜索目录 - s.paths.Add(gfile.SelfDir()) + if gfile.SelfDir() != gfile.TempDir() { + s.paths.Add(gfile.SelfDir()) + } // (开发环境)添加main源码包到搜索目录 if p := gfile.MainPkgPath(); p != "" && gfile.Exists(p) { s.paths.Add(p) diff --git a/g/os/gfile/gfile.go b/g/os/gfile/gfile.go index 188e01c13..420c8f10a 100644 --- a/g/os/gfile/gfile.go +++ b/g/os/gfile/gfile.go @@ -108,10 +108,9 @@ func IsDir(path string) bool { return s.IsDir() } -// 获取当前工作目录 +// 获取当前工作目录(SelfDir()方法的别名) func Pwd() string { - pwd, _ := filepath.Abs(filepath.Dir(os.Args[0])) - return pwd + return SelfDir() } // 判断所给路径是否为文件 diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 2b23ac058..e306dbe53 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -8,22 +8,22 @@ package gview import ( + "bytes" + "errors" "fmt" + "gitee.com/johng/gf/g/encoding/ghash" + "gitee.com/johng/gf/g/encoding/ghtml" "gitee.com/johng/gf/g/encoding/gurl" + "gitee.com/johng/gf/g/os/gfcache" + "gitee.com/johng/gf/g/os/gfile" "gitee.com/johng/gf/g/os/glog" + "gitee.com/johng/gf/g/os/gspath" "gitee.com/johng/gf/g/os/gtime" + "gitee.com/johng/gf/g/util/gconv" "gitee.com/johng/gf/g/util/gstr" "strings" "sync" - "bytes" - "errors" "text/template" - "gitee.com/johng/gf/g/container/gmap" - "gitee.com/johng/gf/g/encoding/ghash" - "gitee.com/johng/gf/g/util/gconv" - "gitee.com/johng/gf/g/os/gspath" - "gitee.com/johng/gf/g/os/gfcache" - "gitee.com/johng/gf/g/encoding/ghtml" ) // 视图对象 @@ -41,16 +41,17 @@ type Params = map[string]interface{} // 函数映射表 type FuncMap = map[string]interface{} -// 视图表 -var viewMap = gmap.NewStringInterfaceMap() - // 默认的视图对象 var viewObj *View // 初始化默认的视图对象 func checkAndInitDefaultView() { if viewObj == nil { - viewObj = Get(".") + if gfile.SelfDir() != gfile.TempDir() { + viewObj = New(gfile.SelfDir()) + } else { + viewObj = New() + } } } @@ -60,25 +61,17 @@ func ParseContent(content string, params Params) ([]byte, error) { return viewObj.ParseContent(content, params) } -// 获取或者创建一个视图对象 -func Get(path string) *View { - if r := viewMap.Get(path); r != nil { - return r.(*View) - } - v := New(path) - viewMap.Set(path, v) - return v -} - // 生成一个视图对象 -func New(path string) *View { +func New(path...string) *View { view := &View { paths : gspath.New(), data : make(map[string]interface{}), funcmap : make(map[string]interface{}), delimiters : make([]string, 2), } - view.SetPath(path) + if len(path) > 0 && len(path[0]) > 0 { + view.SetPath(path[0]) + } view.SetDelimiters("{{", "}}") // 内置方法 view.BindFunc("text", view.funcText) @@ -103,6 +96,11 @@ func New(path string) *View { // 设置模板目录绝对路径 func (view *View) SetPath(path string) error { + if p := gfile.RealPath(path); p == "" { + return errors.New(path + " does not exist") + } else { + path = p + } if rp, err := view.paths.Set(path); err != nil { glog.Error("gview.SetPath failed:", err.Error()) return err @@ -114,6 +112,11 @@ func (view *View) SetPath(path string) error { // 添加模板目录搜索路径 func (view *View) AddPath(path string) error { + if p := gfile.RealPath(path); p == "" { + return errors.New(path + " does not exist") + } else { + path = p + } if rp, err := view.paths.Add(path); err != nil { glog.Error("gview.AddPath failed:", err.Error()) return err diff --git a/geg/other/test.go b/geg/other/test.go index 9df38fca3..e68980d3d 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,9 +2,11 @@ package main import ( "fmt" + "gitee.com/johng/gf/g/os/gfile" ) func main() { - s := "" - fmt.Println(s[0]) + fmt.Println(gfile.TempDir()) + fmt.Println(gfile.SelfDir()) + fmt.Println(gfile.Pwd()) }