From 2ffebf25004c2cb8aa35b5c64e6c92b30e8b7a24 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 29 Oct 2018 13:59:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgfile=E5=9C=A8=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E7=8E=AF=E5=A2=83=E4=B8=8B=E7=9A=84main=E5=8C=85?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84=E8=8E=B7=E5=8F=96=E6=96=B9?= =?UTF-8?q?=E5=BC=8F;ghttp.Request=E5=A2=9E=E5=8A=A0idFileServe=E5=B1=9E?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/ghttp_request.go | 6 +++ g/net/ghttp/ghttp_server.go | 6 ++- g/net/ghttp/ghttp_server_handler.go | 2 + g/os/gcfg/gcfg.go | 4 +- g/os/gfile/gfile.go | 61 ++++++++--------------- g/os/glog/glog_logger.go | 2 +- geg/frame/{ => mvc}/config.json | 0 geg/frame/{ => mvc}/config.toml | 1 + geg/frame/{ => mvc}/config.xml | 0 geg/frame/{ => mvc}/config.yml | 0 geg/frame/mvc/controller/demo/template.go | 2 +- geg/other/test2.go | 4 +- 12 files changed, 42 insertions(+), 46 deletions(-) rename geg/frame/{ => mvc}/config.json (100%) rename geg/frame/{ => mvc}/config.toml (98%) rename geg/frame/{ => mvc}/config.xml (100%) rename geg/frame/{ => mvc}/config.yml (100%) diff --git a/g/net/ghttp/ghttp_request.go b/g/net/ghttp/ghttp_request.go index de2143af3..77260c3f0 100644 --- a/g/net/ghttp/ghttp_request.go +++ b/g/net/ghttp/ghttp_request.go @@ -38,6 +38,7 @@ type Request struct { parsedHost *gtype.String // 解析过后不带端口号的服务器域名名称 clientIp *gtype.String // 解析过后的客户端IP地址 isFileRequest bool // 是否为静态文件请求(非服务请求,当静态文件存在时,优先级会被服务请求高,被识别为文件请求) + isFileServe bool // 是否为文件处理(调用Server.serveFile时设置为true), isFileRequest为true时isFileServe也为true } // 创建一个Request对象 @@ -180,6 +181,11 @@ func (r *Request) IsFileRequest() bool { return r.isFileRequest } +// 判断请求是否为文件处理 +func (r *Request) IsFileServe() bool { + return r.isFileServe +} + // 判断是否为AJAX请求 func (r *Request) IsAjaxRequest() bool { return strings.EqualFold(r.Header.Get("X-Requested-With"), "XMLHttpRequest") diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index 725474291..3ac8f322f 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -323,12 +323,16 @@ func (s *Server) GetRouteMap() string { } m[item.domain].Add(item) } + addr := s.config.Addr + if s.config.HTTPSAddr != "" { + addr += ",tls" + s.config.HTTPSAddr + } for _, a := range m { data := make([]string, 7) for _, v := range a.Slice() { item := v.(*tableItem) data[0] = s.name - data[1] = s.config.Addr + data[1] = addr data[2] = item.domain data[3] = item.method data[4] = item.route diff --git a/g/net/ghttp/ghttp_server_handler.go b/g/net/ghttp/ghttp_server_handler.go index c5a1b8b92..ccab1a4c0 100644 --- a/g/net/ghttp/ghttp_server_handler.go +++ b/g/net/ghttp/ghttp_server_handler.go @@ -142,6 +142,8 @@ func (s *Server)callServeHandler(h *handlerItem, r *Request) { // http server静态文件处理,path可以为相对路径也可以为绝对路径 func (s *Server)serveFile(r *Request, path string) { + r.isFileServe = true + // 首先判断是否给定的path已经是一个绝对路径 if !gfile.Exists(path) { path = s.paths.Search(path) diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 256e2b038..a7e5094d4 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -59,7 +59,7 @@ func (c *Config) filePath(file...string) string { // 设置配置管理器的配置文件存放目录绝对路径 func (c *Config) SetPath(path string) error { if err := c.paths.Set(path); err != nil { - glog.Debug("gcfg.SetPath failed:", path, err) + glog.Error("gcfg.SetPath failed:", path, err) return err } c.jsons.Clear() @@ -109,6 +109,8 @@ func (c *Config) getJson(file...string) *gjson.Json { c.addMonitor(fpath) c.jsons.Set(fpath, j) return j + } else { + glog.Errorfln(`gcfg.Load config file "%s" failed: %s`, fpath, err.Error()) } return nil } diff --git a/g/os/gfile/gfile.go b/g/os/gfile/gfile.go index 9a00eaed7..ad6513666 100644 --- a/g/os/gfile/gfile.go +++ b/g/os/gfile/gfile.go @@ -377,56 +377,37 @@ func MainPkgPath() string { if path != "" { return path } - f := "" + f := "" + goroot := runtime.GOROOT() for i := 1; i < 10000; i++ { if _, file, _, ok := runtime.Caller(i); ok { - if strings.EqualFold("", file) { - // 如果是通过init包方法进入,那么无法得到准确的文件路径 - f = "" - } else { - goroot := GoRootOfBuild() - if goroot != "" && !gregex.IsMatchString("^" + GoRootOfBuild(), file) { - // 不包含go源码路径 - f = file - } + // 不包含go源码路径 + if file != "" && goroot != "" && + !gregex.IsMatchString("^" + goroot, file) && + !strings.EqualFold("", file) { + f = file } } else { break } } if f != "" { - p := Dir(f) - mainPkgPath.Set(p) - return p - } - return "" -} - -// 编译时环境的GOROOT数值(对init初始化方法调用时无效,获取不了ROOT值) -// 注意:可能返回空 -func GoRootOfBuild() string { - if v := goRootOfBuild.Val(); v != "" { - return v - } - firstEntry := "" - for i := 0; i < 10000; i++ { - if _, file, _, ok := runtime.Caller(i); ok { - firstEntry = file - } else { - break + for { + p := Dir(f) + if p == f { + break + } + if paths, err := ScanDir(p, "*.go"); err == nil && len(paths) > 0 { + for _, path := range paths { + if gregex.IsMatchString(`package\s+main`, GetContents(path)) { + mainPkgPath.Set(p) + return p + } + } + } + f = p } } - if len(firstEntry) > 0 { - sep := "/" - array := strings.Split(firstEntry, sep) - if len(array) == 1 { - sep = "\\" - array = strings.Split(firstEntry, sep) - } - root := strings.Join(array[0 : len(array) - 3], sep) - goRootOfBuild.Set(root) - return root - } return "" } diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index 9c7a3902f..fce9a5a18 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -257,7 +257,7 @@ func (l *Logger) GetBacktrace(skip...int) string { } } // 从业务文件开始位置根据自定义的skip开始backtrace - goroot := gfile.GoRootOfBuild() + goroot := runtime.GOROOT() for i := from + customSkip + l.btSkip.Val(); i < 10000; i++ { if _, cfile, cline, ok := runtime.Caller(i); ok && cfile != "" { // 不打印出go源码路径及glog包文件路径,日志打印必须从业务源码文件开始,且从glog包文件开始检索 diff --git a/geg/frame/config.json b/geg/frame/mvc/config.json similarity index 100% rename from geg/frame/config.json rename to geg/frame/mvc/config.json diff --git a/geg/frame/config.toml b/geg/frame/mvc/config.toml similarity index 98% rename from geg/frame/config.toml rename to geg/frame/mvc/config.toml index 20d9dfd0c..7d6ad4b03 100644 --- a/geg/frame/config.toml +++ b/geg/frame/mvc/config.toml @@ -1,5 +1,6 @@ # 模板引擎目录 viewpath = "/home/www/templates/" +test = "v=1" # MySQL数据库配置 [database] [[database.default]] diff --git a/geg/frame/config.xml b/geg/frame/mvc/config.xml similarity index 100% rename from geg/frame/config.xml rename to geg/frame/mvc/config.xml diff --git a/geg/frame/config.yml b/geg/frame/mvc/config.yml similarity index 100% rename from geg/frame/config.yml rename to geg/frame/mvc/config.yml diff --git a/geg/frame/mvc/controller/demo/template.go b/geg/frame/mvc/controller/demo/template.go index c56a1c55e..e2ed91f39 100644 --- a/geg/frame/mvc/controller/demo/template.go +++ b/geg/frame/mvc/controller/demo/template.go @@ -15,7 +15,7 @@ func (c *ControllerTemplate) Info() { "age" : 18, "score" : 100, }) - c.View.Display("user/index.tpl") + c.View.Display("view/user/index.tpl") } func init() { diff --git a/geg/other/test2.go b/geg/other/test2.go index 933d8ed0b..d51d46561 100644 --- a/geg/other/test2.go +++ b/geg/other/test2.go @@ -1,10 +1,10 @@ package main import ( - "gitee.com/johng/gf/g" + "fmt" "gitee.com/johng/gf/g/os/gfile" ) func main() { - g.Dump(gfile.ScanDir("/var/log", "*.log, *.gz", true)) + fmt.Println(gfile.Dir("c:\111\222")) } \ No newline at end of file