mirror of
https://gitee.com/johng/gf
synced 2026-06-23 16:34:17 +08:00
框架一些模块的细节改进
This commit is contained in:
2
TODO
2
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:
|
||||
|
||||
@ -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
|
||||
})
|
||||
|
||||
10
g/g_func.go
10
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...)
|
||||
|
||||
@ -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()
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
// 获取
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
||||
@ -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(`<autogenerated>`, cfile) {
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
// 按照优先级搜索文件,返回搜索到的文件绝对路径,找不到该文件时,返回空字符串
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
// 批量绑定模板变量,即调用之后每个线程都会生效,因此有并发安全控制
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user