mirror of
https://gitee.com/johng/gf
synced 2026-06-24 08:48:09 +08:00
改进gfile在开发环境下的main包绝对路径获取方式;ghttp.Request增加idFileServe属性
This commit is contained in:
@ -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")
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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("<autogenerated>", 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("<autogenerated>", 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 ""
|
||||
}
|
||||
|
||||
|
||||
@ -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包文件开始检索
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
# 模板引擎目录
|
||||
viewpath = "/home/www/templates/"
|
||||
test = "v=1"
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
[[database.default]]
|
||||
@ -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() {
|
||||
|
||||
@ -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"))
|
||||
}
|
||||
Reference in New Issue
Block a user