mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 11:48:09 +08:00
change logging level from error to debug in route regitsry for struct for ghttp package; handle possible overrange usage of r.URL.Path in ghttp.Server
This commit is contained in:
parent
afb1adee3d
commit
f258b5bf1c
@ -56,7 +56,7 @@ func (r *Response) CORS(options CORSOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
// 允许请求跨域访问(使用more配置).
|
||||
// 允许请求跨域访问(使用默认配置).
|
||||
func (r *Response) CORSDefault() {
|
||||
r.CORS(r.DefaultCORSOptions())
|
||||
}
|
||||
|
@ -108,6 +108,11 @@ const (
|
||||
HOOK_BEFORE_OUTPUT = "BeforeOutput"
|
||||
HOOK_AFTER_OUTPUT = "AfterOutput"
|
||||
|
||||
// deprecated.
|
||||
HOOK_BEFORE_CLOSE = "BeforeClose"
|
||||
// deprecated.
|
||||
HOOK_AFTER_CLOSE = "AfterClose"
|
||||
|
||||
HTTP_METHODS = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE"
|
||||
gDEFAULT_SERVER = "default"
|
||||
gDEFAULT_DOMAIN = "default"
|
||||
|
@ -74,8 +74,8 @@ type ServerConfig struct {
|
||||
Rewrites map[string]string // URI Rewrite重写配置
|
||||
|
||||
// 日志配置
|
||||
LogPath string // 存放日志的目录路径
|
||||
LogHandler LogHandler // 自定义日志处理回调方法
|
||||
LogPath string // 存放日志的目录路径(默认为空,表示不写文件)
|
||||
LogHandler LogHandler // 自定义日志处理回调方法(默认为空)
|
||||
LogStdPrint bool // 是否打印日志到终端(默认开启)
|
||||
ErrorLogEnabled bool // 是否开启error log(默认开启)
|
||||
AccessLogEnabled bool // 是否开启access log(默认关闭)
|
||||
|
@ -10,7 +10,10 @@ import (
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
// 设置日志目录
|
||||
// 设置日志目录,只有在设置了日志目录的情况下才会输出日志到日志文件中。
|
||||
// 日志文件路径格式为:
|
||||
// 1. 请求日志: access/YYYY-MM-DD.log
|
||||
// 2. 错误日志: error/YYYY-MM-DD.log
|
||||
func (s *Server)SetLogPath(path string) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
@ -23,7 +26,8 @@ func (s *Server)SetLogPath(path string) {
|
||||
s.logger.SetPath(path)
|
||||
}
|
||||
|
||||
// 设置日志内容是否输出到终端
|
||||
// 设置日志内容是否输出到终端,默认情况下只有错误日志才会自动输出到终端。
|
||||
// 如果需要输出请求日志到终端,默认情况下使用SetAccessLogEnabled方法开启请求日志特性即可。
|
||||
func (s *Server)SetLogStdPrint(enabled bool) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
@ -60,7 +64,7 @@ func (s *Server) SetLogHandler(handler LogHandler) {
|
||||
}
|
||||
|
||||
// 获取日志写入的回调函数
|
||||
func (s *Server) GetLogHandler() LogHandler {
|
||||
func (s *Server)GetLogHandler() LogHandler {
|
||||
return s.config.LogHandler
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,12 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Path = rewrite
|
||||
}
|
||||
}
|
||||
|
||||
// URI默认值
|
||||
if r.URL.Path == "" {
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
|
||||
// 去掉末尾的"/"号
|
||||
if r.URL.Path != "/" {
|
||||
for r.URL.Path[len(r.URL.Path) - 1] == '/' {
|
||||
|
@ -43,14 +43,22 @@ func (s *Server)BindController(pattern string, c Controller, methods...string) {
|
||||
if mname == "Init" || mname == "Shut" || mname == "Exit" {
|
||||
continue
|
||||
}
|
||||
if _, ok := v.Method(i).Interface().(func()); !ok {
|
||||
glog.Errorfln(`invalid method definition "%s", while "func()" is required`, v.Method(i).Type().String())
|
||||
continue
|
||||
}
|
||||
ctlName := gstr.Replace(t.String(), fmt.Sprintf(`%s.`, pkgName), "")
|
||||
if ctlName[0] == '*' {
|
||||
ctlName = fmt.Sprintf(`(%s)`, ctlName)
|
||||
}
|
||||
if _, ok := v.Method(i).Interface().(func()); !ok {
|
||||
if len(methodMap) > 0 {
|
||||
// 指定的方法名称注册,那么需要使用错误提示
|
||||
glog.Errorfln(`invalid route method: %s.%s.%s defined as "%s", but "func()" is required`,
|
||||
pkgPath, ctlName, mname, v.Method(i).Type().String())
|
||||
} else {
|
||||
// 否则只是Debug提示
|
||||
glog.Debugfln(`ignore route method: %s.%s.%s defined as "%s", no match "func()"`,
|
||||
pkgPath, ctlName, mname, v.Method(i).Type().String())
|
||||
}
|
||||
continue
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(pattern, sname, mname, true)
|
||||
m[key] = &handlerItem {
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, mname),
|
||||
@ -93,16 +101,17 @@ func (s *Server)BindControllerMethod(pattern string, c Controller, method string
|
||||
glog.Error("invalid method name:" + mname)
|
||||
return
|
||||
}
|
||||
if _, ok := fval.Interface().(func()); !ok {
|
||||
glog.Errorfln(`invalid method definition "%s", while "func()" is required`, fval.Type().String())
|
||||
return
|
||||
}
|
||||
pkgPath := t.Elem().PkgPath()
|
||||
pkgName := gfile.Basename(pkgPath)
|
||||
ctlName := gstr.Replace(t.String(), fmt.Sprintf(`%s.`, pkgName), "")
|
||||
if ctlName[0] == '*' {
|
||||
ctlName = fmt.Sprintf(`(%s)`, ctlName)
|
||||
}
|
||||
if _, ok := fval.Interface().(func()); !ok {
|
||||
glog.Errorfln(`invalid route method: %s.%s.%s defined as "%s", but "func()" is required`,
|
||||
pkgPath, ctlName, mname, fval.Type().String())
|
||||
return
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(pattern, sname, mname, false)
|
||||
m[key] = &handlerItem {
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, mname),
|
||||
@ -132,15 +141,16 @@ func (s *Server)BindControllerRest(pattern string, c Controller) {
|
||||
if _, ok := methodsMap[method]; !ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := v.Method(i).Interface().(func()); !ok {
|
||||
glog.Errorfln(`invalid method definition "%s", while "func()" is required`, v.Method(i).Type().String())
|
||||
return
|
||||
}
|
||||
pkgName := gfile.Basename(pkgPath)
|
||||
ctlName := gstr.Replace(t.String(), fmt.Sprintf(`%s.`, pkgName), "")
|
||||
if ctlName[0] == '*' {
|
||||
ctlName = fmt.Sprintf(`(%s)`, ctlName)
|
||||
}
|
||||
if _, ok := v.Method(i).Interface().(func()); !ok {
|
||||
glog.Errorfln(`invalid route method: %s.%s.%s defined as "%s", but "func()" is required`,
|
||||
pkgPath, ctlName, mname, v.Method(i).Type().String())
|
||||
return
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(mname + ":" + pattern, sname, mname, false)
|
||||
m[key] = &handlerItem {
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, mname),
|
||||
|
@ -49,15 +49,23 @@ func (s *Server)BindObject(pattern string, obj interface{}, methods...string) {
|
||||
if mname == "Init" || mname == "Shut" {
|
||||
continue
|
||||
}
|
||||
faddr, ok := v.Method(i).Interface().(func(*Request))
|
||||
if !ok {
|
||||
glog.Errorfln(`invalid method definition "%s", while "func(*Request))" is required`, v.Method(i).Type().String())
|
||||
continue
|
||||
}
|
||||
objName := gstr.Replace(t.String(), fmt.Sprintf(`%s.`, pkgName), "")
|
||||
if objName[0] == '*' {
|
||||
objName = fmt.Sprintf(`(%s)`, objName)
|
||||
}
|
||||
faddr, ok := v.Method(i).Interface().(func(*Request))
|
||||
if !ok {
|
||||
if len(methodMap) > 0 {
|
||||
// 指定的方法名称注册,那么需要使用错误提示
|
||||
glog.Errorfln(`invalid route method: %s.%s.%s defined as "%s", but "func(*ghttp.Request)" is required`,
|
||||
pkgPath, objName, mname, v.Method(i).Type().String())
|
||||
} else {
|
||||
// 否则只是Debug提示
|
||||
glog.Debugfln(`ignore route method: %s.%s.%s defined as "%s", no match "func(*ghttp.Request)"`,
|
||||
pkgPath, objName, mname, v.Method(i).Type().String())
|
||||
}
|
||||
continue
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(pattern, sname, mname, true)
|
||||
m[key] = &handlerItem {
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, mname),
|
||||
@ -103,11 +111,6 @@ func (s *Server)BindObjectMethod(pattern string, obj interface{}, method string)
|
||||
glog.Error("invalid method name:" + mname)
|
||||
return
|
||||
}
|
||||
faddr, ok := fval.Interface().(func(*Request))
|
||||
if !ok {
|
||||
glog.Errorfln(`invalid method definition "%s", while "func(*Request)" is required`, fval.Type().String())
|
||||
return
|
||||
}
|
||||
finit := (func(*Request))(nil)
|
||||
fshut := (func(*Request))(nil)
|
||||
if v.MethodByName("Init").IsValid() {
|
||||
@ -122,6 +125,12 @@ func (s *Server)BindObjectMethod(pattern string, obj interface{}, method string)
|
||||
if objName[0] == '*' {
|
||||
objName = fmt.Sprintf(`(%s)`, objName)
|
||||
}
|
||||
faddr, ok := fval.Interface().(func(*Request))
|
||||
if !ok {
|
||||
glog.Errorfln(`invalid route method: %s.%s.%s defined as "%s", but "func(*ghttp.Request)" is required`,
|
||||
pkgPath, objName, mname, fval.Type().String())
|
||||
return
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(pattern, sname, mname, false)
|
||||
m[key] = &handlerItem{
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, mname),
|
||||
@ -158,16 +167,17 @@ func (s *Server)BindObjectRest(pattern string, obj interface{}) {
|
||||
if _, ok := methodsMap[method]; !ok {
|
||||
continue
|
||||
}
|
||||
faddr, ok := v.Method(i).Interface().(func(*Request))
|
||||
if !ok {
|
||||
glog.Errorfln(`invalid method definition "%s", while "func(*Request)" is required`, v.Method(i).Type().String())
|
||||
continue
|
||||
}
|
||||
pkgName := gfile.Basename(pkgPath)
|
||||
objName := gstr.Replace(t.String(), fmt.Sprintf(`%s.`, pkgName), "")
|
||||
if objName[0] == '*' {
|
||||
objName = fmt.Sprintf(`(%s)`, objName)
|
||||
}
|
||||
faddr, ok := v.Method(i).Interface().(func(*Request))
|
||||
if !ok {
|
||||
glog.Errorfln(`invalid route method: %s.%s.%s defined as "%s", but "func(*ghttp.Request)" is required`,
|
||||
pkgPath, objName, mname, v.Method(i).Type().String())
|
||||
continue
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(mname + ":" + pattern, sname, mname, false)
|
||||
m[key] = &handlerItem {
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, mname),
|
||||
|
@ -57,7 +57,7 @@ func TestCache_LRU(t *testing.T) {
|
||||
|
||||
gtest.Assert(cache.Size(), 10)
|
||||
gtest.Assert(cache.Get(6), 6)
|
||||
time.Sleep(3*time.Second)
|
||||
time.Sleep(4*time.Second)
|
||||
gtest.Assert(cache.Size(), 2)
|
||||
gtest.Assert(cache.Get(6), 6)
|
||||
gtest.Assert(cache.Get(1), nil)
|
||||
|
@ -20,7 +20,7 @@ func (c *User) Test(r *ghttp.Request, value interface{}) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindObjectMethod("/user", new(User), "Test")
|
||||
s.BindObject("/user", new(User))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user