mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
change constant variable names
This commit is contained in:
@ -15,10 +15,10 @@ func main() {
|
||||
r.Response.Writeln("end")
|
||||
})
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
glog.To(r.Response.Writer).Println("BeforeServe")
|
||||
},
|
||||
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookAfterServe: func(r *ghttp.Request) {
|
||||
glog.To(r.Response.Writer).Println("AfterServe")
|
||||
},
|
||||
})
|
||||
|
||||
@ -12,7 +12,7 @@ func Order(r *ghttp.Request) {
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Hook("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/*any", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
})
|
||||
g.GET("/order", Order)
|
||||
|
||||
@ -11,10 +11,10 @@ func main() {
|
||||
p := "/:name/info/{uid}"
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_SERVE) },
|
||||
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_SERVE) },
|
||||
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_OUTPUT) },
|
||||
ghttp.HOOK_AFTER_OUTPUT: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_OUTPUT) },
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) { glog.Println(ghttp.HookBeforeServe) },
|
||||
ghttp.HookAfterServe: func(r *ghttp.Request) { glog.Println(ghttp.HookAfterServe) },
|
||||
ghttp.HookBeforeOutput: func(r *ghttp.Request) { glog.Println(ghttp.HookBeforeOutput) },
|
||||
ghttp.HookAfterOutput: func(r *ghttp.Request) { glog.Println(ghttp.HookAfterOutput) },
|
||||
})
|
||||
s.BindHandler(p, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.Get("uid"))
|
||||
|
||||
@ -11,7 +11,7 @@ func main() {
|
||||
// 多事件回调示例,事件1
|
||||
pattern1 := "/:name/info"
|
||||
s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.SetParam("uid", 1000)
|
||||
},
|
||||
})
|
||||
@ -22,7 +22,7 @@ func main() {
|
||||
// 多事件回调示例,事件2
|
||||
pattern2 := "/{object}/list/{page}.java"
|
||||
s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeOutput: func(r *ghttp.Request) {
|
||||
r.Response.SetBuffer([]byte(
|
||||
fmt.Sprintf("通过事件修改输出内容, object:%s, page:%s", r.Get("object"), r.GetRouterString("page"))),
|
||||
)
|
||||
|
||||
@ -7,10 +7,10 @@ import (
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHookHandler("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/*any", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/*any")
|
||||
})
|
||||
s.BindHookHandler("/v1/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/v1/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/v1/*")
|
||||
r.ExitHook()
|
||||
})
|
||||
|
||||
@ -11,7 +11,7 @@ func main() {
|
||||
r.Response.Writeln(r.Get("name"))
|
||||
})
|
||||
s.BindHookHandlerByMap("/", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.SetParam("name", "john")
|
||||
},
|
||||
})
|
||||
|
||||
@ -12,17 +12,17 @@ func main() {
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/priority/:name")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/priority/*any")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/priority/show")
|
||||
},
|
||||
})
|
||||
|
||||
@ -27,10 +27,10 @@ func main() {
|
||||
})
|
||||
})
|
||||
group.Group("/hook", func(group *ghttp.RouterGroup) {
|
||||
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook any")
|
||||
})
|
||||
group.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/:name", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook name")
|
||||
})
|
||||
})
|
||||
|
||||
@ -18,7 +18,7 @@ func main() {
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.SetServerRoot("root")
|
||||
s.BindHookHandler("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
fmt.Println(r.URL.Path, r.IsFileRequest())
|
||||
})
|
||||
s.BindHandler("/template", func(r *ghttp.Request) {
|
||||
|
||||
@ -27,7 +27,7 @@ func main() {
|
||||
s := g.Server()
|
||||
obj := new(Object)
|
||||
s.Group("/api").Bind([]ghttp.GroupItem{
|
||||
{"ALL", "*", HookHandler, ghttp.HOOK_BEFORE_SERVE},
|
||||
{"ALL", "*", HookHandler, ghttp.HookBeforeServe},
|
||||
{"ALL", "/handler", Handler},
|
||||
{"ALL", "/obj", obj},
|
||||
{"GET", "/obj/show", obj, "Show"},
|
||||
|
||||
@ -54,10 +54,10 @@ func main() {
|
||||
})
|
||||
})
|
||||
group.Group("/hook", func(group *ghttp.RouterGroup) {
|
||||
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook any")
|
||||
})
|
||||
group.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/:name", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook name")
|
||||
})
|
||||
})
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHookHandler("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/*any", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
fmt.Println(r.Router)
|
||||
fmt.Println(r.Get("customer_id"))
|
||||
})
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -97,7 +97,7 @@ func Database(name ...string) gdb.DB {
|
||||
if Config().Available() {
|
||||
// Initialize logger for ORM.
|
||||
var loggerConfigMap map[string]interface{}
|
||||
loggerConfigMap = Config().GetMap(fmt.Sprintf("%s.%s", configNodeKey, gLOGGER_NODE_NAME))
|
||||
loggerConfigMap = Config().GetMap(fmt.Sprintf("%s.%s", configNodeKey, configNodeNameLogger))
|
||||
if len(loggerConfigMap) == 0 {
|
||||
loggerConfigMap = Config().GetMap(configNodeKey)
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -13,8 +13,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gFRAME_CORE_COMPONENT_NAME_LOGGER = "gf.core.component.logger"
|
||||
gLOGGER_NODE_NAME = "logger"
|
||||
frameCoreComponentNameLogger = "gf.core.component.logger"
|
||||
configNodeNameLogger = "logger"
|
||||
)
|
||||
|
||||
// Log returns an instance of glog.Logger.
|
||||
@ -24,15 +24,15 @@ func Log(name ...string) *glog.Logger {
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
instanceName = name[0]
|
||||
}
|
||||
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName)
|
||||
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameLogger, instanceName)
|
||||
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
||||
logger := glog.Instance(instanceName)
|
||||
// To avoid file no found error while it's not necessary.
|
||||
if Config().Available() {
|
||||
var m map[string]interface{}
|
||||
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), gLOGGER_NODE_NAME)
|
||||
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameLogger)
|
||||
if nodeKey == "" {
|
||||
nodeKey = gLOGGER_NODE_NAME
|
||||
nodeKey = configNodeNameLogger
|
||||
}
|
||||
m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
|
||||
if len(m) == 0 {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -14,8 +14,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis"
|
||||
gREDIS_NODE_NAME = "redis"
|
||||
frameCoreComponentNameRedis = "gf.core.component.redis"
|
||||
configNodeNameRedis = "redis"
|
||||
)
|
||||
|
||||
// Redis returns an instance of redis client with specified configuration group name.
|
||||
@ -25,7 +25,7 @@ func Redis(name ...string) *gredis.Redis {
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
group = name[0]
|
||||
}
|
||||
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group)
|
||||
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameRedis, group)
|
||||
result := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
||||
// If already configured, it returns the redis instance.
|
||||
if _, ok := gredis.GetConfig(group); ok {
|
||||
@ -33,7 +33,7 @@ func Redis(name ...string) *gredis.Redis {
|
||||
}
|
||||
// Or else, it parses the default configuration file and returns a new redis instance.
|
||||
var m map[string]interface{}
|
||||
if _, v := gutil.MapPossibleItemByKey(Config().GetMap("."), gREDIS_NODE_NAME); v != nil {
|
||||
if _, v := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameRedis); v != nil {
|
||||
m = gconv.Map(v)
|
||||
}
|
||||
if len(m) > 0 {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -13,21 +13,21 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gFRAME_CORE_COMPONENT_NAME_SERVER = "gf.core.component.server"
|
||||
gSERVER_NODE_NAME = "server"
|
||||
frameCoreComponentNameServer = "gf.core.component.server"
|
||||
configNodeNameServer = "server"
|
||||
)
|
||||
|
||||
// Server returns an instance of http server with specified name.
|
||||
func Server(name ...interface{}) *ghttp.Server {
|
||||
instanceKey := fmt.Sprintf("%s.%v", gFRAME_CORE_COMPONENT_NAME_SERVER, name)
|
||||
instanceKey := fmt.Sprintf("%s.%v", frameCoreComponentNameServer, name)
|
||||
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
||||
s := ghttp.GetServer(name...)
|
||||
// To avoid file no found error while it's not necessary.
|
||||
if Config().Available() {
|
||||
var m map[string]interface{}
|
||||
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), gSERVER_NODE_NAME)
|
||||
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameServer)
|
||||
if nodeKey == "" {
|
||||
nodeKey = gSERVER_NODE_NAME
|
||||
nodeKey = configNodeNameServer
|
||||
}
|
||||
m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, s.GetName()))
|
||||
if len(m) == 0 {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -13,8 +13,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gFRAME_CORE_COMPONENT_NAME_VIEWER = "gf.core.component.viewer"
|
||||
gVIEWER_NODE_NAME = "viewer"
|
||||
frameCoreComponentNameViewer = "gf.core.component.viewer"
|
||||
configNodeNameViewer = "viewer"
|
||||
)
|
||||
|
||||
// View returns an instance of View with default settings.
|
||||
@ -24,7 +24,7 @@ func View(name ...string) *gview.View {
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
instanceName = name[0]
|
||||
}
|
||||
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEWER, instanceName)
|
||||
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameViewer, instanceName)
|
||||
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
||||
return getViewInstance(instanceName)
|
||||
}).(*gview.View)
|
||||
@ -39,9 +39,9 @@ func getViewInstance(name ...string) *gview.View {
|
||||
// To avoid file no found error while it's not necessary.
|
||||
if Config().Available() {
|
||||
var m map[string]interface{}
|
||||
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), gVIEWER_NODE_NAME)
|
||||
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameViewer)
|
||||
if nodeKey == "" {
|
||||
nodeKey = gVIEWER_NODE_NAME
|
||||
nodeKey = configNodeNameViewer
|
||||
}
|
||||
m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
|
||||
if len(m) == 0 {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -107,25 +107,29 @@ type (
|
||||
)
|
||||
|
||||
const (
|
||||
SERVER_STATUS_STOPPED = 0
|
||||
SERVER_STATUS_RUNNING = 1
|
||||
HOOK_BEFORE_SERVE = "HOOK_BEFORE_SERVE"
|
||||
HOOK_AFTER_SERVE = "HOOK_AFTER_SERVE"
|
||||
HOOK_BEFORE_OUTPUT = "HOOK_BEFORE_OUTPUT"
|
||||
HOOK_AFTER_OUTPUT = "HOOK_AFTER_OUTPUT"
|
||||
HTTP_METHODS = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE"
|
||||
gDEFAULT_SERVER = "default"
|
||||
gDEFAULT_DOMAIN = "default"
|
||||
gDEFAULT_METHOD = "ALL"
|
||||
gHANDLER_TYPE_HANDLER = 1
|
||||
gHANDLER_TYPE_OBJECT = 2
|
||||
gHANDLER_TYPE_CONTROLLER = 3
|
||||
gHANDLER_TYPE_MIDDLEWARE = 4
|
||||
gHANDLER_TYPE_HOOK = 5
|
||||
gEXCEPTION_EXIT = "exit"
|
||||
gEXCEPTION_EXIT_ALL = "exit_all"
|
||||
gEXCEPTION_EXIT_HOOK = "exit_hook"
|
||||
gROUTE_CACHE_DURATION = time.Hour
|
||||
HOOK_BEFORE_SERVE = "HOOK_BEFORE_SERVE" // Deprecated, use HookBeforeServe instead.
|
||||
HOOK_AFTER_SERVE = "HOOK_AFTER_SERVE" // Deprecated, use HookAfterServe instead.
|
||||
HOOK_BEFORE_OUTPUT = "HOOK_BEFORE_OUTPUT" // Deprecated, use HookBeforeOutput instead.
|
||||
HOOK_AFTER_OUTPUT = "HOOK_AFTER_OUTPUT" // Deprecated, use HookAfterOutput instead.
|
||||
HookBeforeServe = "HOOK_BEFORE_SERVE"
|
||||
HookAfterServe = "HOOK_AFTER_SERVE"
|
||||
HookBeforeOutput = "HOOK_BEFORE_OUTPUT"
|
||||
HookAfterOutput = "HOOK_AFTER_OUTPUT"
|
||||
ServerStatusStopped = 0
|
||||
ServerStatusRunning = 1
|
||||
SupportedHttpMethods = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE"
|
||||
defaultServerName = "default"
|
||||
defaultDomainName = "default"
|
||||
defaultMethod = "ALL"
|
||||
handlerTypeHandler = 1
|
||||
handlerTypeObject = 2
|
||||
handlerTypeController = 3
|
||||
handlerTypeMiddleware = 4
|
||||
handlerTypeHook = 5
|
||||
exceptionExit = "exit"
|
||||
exceptionExitAll = "exit_all"
|
||||
exceptionExitHook = "exit_hook"
|
||||
routeCacheDuration = time.Hour
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -71,7 +71,7 @@ func niceCallFunc(f func()) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
switch e {
|
||||
case gEXCEPTION_EXIT, gEXCEPTION_EXIT_ALL:
|
||||
case exceptionExit, exceptionExitAll:
|
||||
return
|
||||
default:
|
||||
if _, ok := e.(errorStack); ok {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -108,18 +108,18 @@ func (r *Request) WebSocket() (*WebSocket, error) {
|
||||
|
||||
// Exit exits executing of current HTTP handler.
|
||||
func (r *Request) Exit() {
|
||||
panic(gEXCEPTION_EXIT)
|
||||
panic(exceptionExit)
|
||||
}
|
||||
|
||||
// ExitAll exits executing of current and following HTTP handlers.
|
||||
func (r *Request) ExitAll() {
|
||||
r.exit = true
|
||||
panic(gEXCEPTION_EXIT_ALL)
|
||||
panic(exceptionExitAll)
|
||||
}
|
||||
|
||||
// ExitHook exits executing of current and following HTTP HOOK handlers.
|
||||
func (r *Request) ExitHook() {
|
||||
panic(gEXCEPTION_EXIT_HOOK)
|
||||
panic(exceptionExitHook)
|
||||
}
|
||||
|
||||
// IsExited checks and returns whether current request is exited.
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -34,7 +34,7 @@ func (m *Middleware) Next() {
|
||||
}
|
||||
item = m.request.handlers[m.handlerIndex]
|
||||
// Filter the HOOK handlers, which are designed to be called in another standalone procedure.
|
||||
if item.handler.itemType == gHANDLER_TYPE_HOOK {
|
||||
if item.handler.itemType == handlerTypeHook {
|
||||
m.handlerIndex++
|
||||
continue
|
||||
}
|
||||
@ -59,7 +59,7 @@ func (m *Middleware) Next() {
|
||||
|
||||
switch item.handler.itemType {
|
||||
// Service controller.
|
||||
case gHANDLER_TYPE_CONTROLLER:
|
||||
case handlerTypeController:
|
||||
m.served = true
|
||||
if m.request.IsExited() {
|
||||
break
|
||||
@ -80,7 +80,7 @@ func (m *Middleware) Next() {
|
||||
}
|
||||
|
||||
// Service object.
|
||||
case gHANDLER_TYPE_OBJECT:
|
||||
case handlerTypeObject:
|
||||
m.served = true
|
||||
if m.request.IsExited() {
|
||||
break
|
||||
@ -102,7 +102,7 @@ func (m *Middleware) Next() {
|
||||
}
|
||||
|
||||
// Service handler.
|
||||
case gHANDLER_TYPE_HANDLER:
|
||||
case handlerTypeHandler:
|
||||
m.served = true
|
||||
if m.request.IsExited() {
|
||||
break
|
||||
@ -112,7 +112,7 @@ func (m *Middleware) Next() {
|
||||
})
|
||||
|
||||
// Global middleware array.
|
||||
case gHANDLER_TYPE_MIDDLEWARE:
|
||||
case handlerTypeMiddleware:
|
||||
niceCallFunc(func() {
|
||||
item.handler.itemFunc(m.request)
|
||||
})
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -45,7 +45,7 @@ func init() {
|
||||
func (r *Response) DefaultCORSOptions() CORSOptions {
|
||||
options := CORSOptions{
|
||||
AllowOrigin: "*",
|
||||
AllowMethods: HTTP_METHODS,
|
||||
AllowMethods: SupportedHttpMethods,
|
||||
AllowCredentials: "true",
|
||||
AllowHeaders: defaultAllowHeaders,
|
||||
MaxAge: 3628800,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -35,7 +35,7 @@ import (
|
||||
|
||||
func init() {
|
||||
// Initialize the methods map.
|
||||
for _, v := range strings.Split(HTTP_METHODS, ",") {
|
||||
for _, v := range strings.Split(SupportedHttpMethods, ",") {
|
||||
methodsMap[v] = struct{}{}
|
||||
}
|
||||
}
|
||||
@ -87,7 +87,7 @@ func serverProcessInit() {
|
||||
// Note that the parameter <name> should be unique for different servers. It returns an existing
|
||||
// server instance if given <name> is already existing in the server mapping.
|
||||
func GetServer(name ...interface{}) *Server {
|
||||
serverName := gDEFAULT_SERVER
|
||||
serverName := defaultServerName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
serverName = gconv.String(name[0])
|
||||
}
|
||||
@ -124,7 +124,7 @@ func (s *Server) Start() error {
|
||||
serverProcessInit()
|
||||
|
||||
// Server can only be run once.
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
if s.Status() == ServerStatusRunning {
|
||||
return errors.New("[ghttp] server is already running")
|
||||
}
|
||||
|
||||
@ -256,9 +256,9 @@ func (s *Server) GetRouterArray() []RouterItem {
|
||||
handler: registeredItem.handler,
|
||||
}
|
||||
switch item.handler.itemType {
|
||||
case gHANDLER_TYPE_CONTROLLER, gHANDLER_TYPE_OBJECT, gHANDLER_TYPE_HANDLER:
|
||||
case handlerTypeController, handlerTypeObject, handlerTypeHandler:
|
||||
item.IsServiceHandler = true
|
||||
case gHANDLER_TYPE_MIDDLEWARE:
|
||||
case handlerTypeMiddleware:
|
||||
item.Middleware = "GLOBAL MIDDLEWARE"
|
||||
}
|
||||
if len(item.handler.middleware) > 0 {
|
||||
@ -280,9 +280,9 @@ func (s *Server) GetRouterArray() []RouterItem {
|
||||
if r = strings.Compare(item1.Domain, item2.Domain); r == 0 {
|
||||
if r = strings.Compare(item1.Route, item2.Route); r == 0 {
|
||||
if r = strings.Compare(item1.Method, item2.Method); r == 0 {
|
||||
if item1.handler.itemType == gHANDLER_TYPE_MIDDLEWARE && item2.handler.itemType != gHANDLER_TYPE_MIDDLEWARE {
|
||||
if item1.handler.itemType == handlerTypeMiddleware && item2.handler.itemType != handlerTypeMiddleware {
|
||||
return -1
|
||||
} else if item1.handler.itemType == gHANDLER_TYPE_MIDDLEWARE && item2.handler.itemType == gHANDLER_TYPE_MIDDLEWARE {
|
||||
} else if item1.handler.itemType == handlerTypeMiddleware && item2.handler.itemType == handlerTypeMiddleware {
|
||||
return 1
|
||||
} else if r = strings.Compare(item1.Middleware, item2.Middleware); r == 0 {
|
||||
r = item2.Priority - item1.Priority
|
||||
@ -445,15 +445,15 @@ func (s *Server) startServer(fdMap listenerFdMap) {
|
||||
// Status retrieves and returns the server status.
|
||||
func (s *Server) Status() int {
|
||||
if serverRunning.Val() == 0 {
|
||||
return SERVER_STATUS_STOPPED
|
||||
return ServerStatusStopped
|
||||
}
|
||||
// If any underlying server is running, the server status is running.
|
||||
for _, v := range s.servers {
|
||||
if v.status == SERVER_STATUS_RUNNING {
|
||||
return SERVER_STATUS_RUNNING
|
||||
if v.status == ServerStatusRunning {
|
||||
return ServerStatusRunning
|
||||
}
|
||||
}
|
||||
return SERVER_STATUS_STOPPED
|
||||
return ServerStatusStopped
|
||||
}
|
||||
|
||||
// getListenerFdMap retrieves and returns the socket file descriptors.
|
||||
@ -485,9 +485,9 @@ func (s *Server) getListenerFdMap() map[string]string {
|
||||
// Deprecated.
|
||||
func IsExitError(err interface{}) bool {
|
||||
errStr := gconv.String(err)
|
||||
if strings.EqualFold(errStr, gEXCEPTION_EXIT) ||
|
||||
strings.EqualFold(errStr, gEXCEPTION_EXIT_ALL) ||
|
||||
strings.EqualFold(errStr, gEXCEPTION_EXIT_HOOK) {
|
||||
if strings.EqualFold(errStr, exceptionExit) ||
|
||||
strings.EqualFold(errStr, exceptionExitAll) ||
|
||||
strings.EqualFold(errStr, exceptionExitHook) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -153,9 +153,9 @@ func (s *gracefulServer) doServe() error {
|
||||
"%d: %s server %s listening on [%s]",
|
||||
gproc.Pid(), s.getProto(), action, s.address,
|
||||
)
|
||||
s.status = SERVER_STATUS_RUNNING
|
||||
s.status = ServerStatusRunning
|
||||
err := s.httpServer.Serve(s.listener)
|
||||
s.status = SERVER_STATUS_STOPPED
|
||||
s.status = ServerStatusStopped
|
||||
return err
|
||||
}
|
||||
|
||||
@ -181,7 +181,7 @@ func (s *gracefulServer) getNetListener() (net.Listener, error) {
|
||||
|
||||
// shutdown shuts down the server gracefully.
|
||||
func (s *gracefulServer) shutdown() {
|
||||
if s.status == SERVER_STATUS_STOPPED {
|
||||
if s.status == ServerStatusStopped {
|
||||
return
|
||||
}
|
||||
if err := s.httpServer.Shutdown(context.Background()); err != nil {
|
||||
@ -194,7 +194,7 @@ func (s *gracefulServer) shutdown() {
|
||||
|
||||
// close shuts down the server forcibly.
|
||||
func (s *gracefulServer) close() {
|
||||
if s.status == SERVER_STATUS_STOPPED {
|
||||
if s.status == ServerStatusStopped {
|
||||
return
|
||||
}
|
||||
if err := s.httpServer.Close(); err != nil {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -105,7 +105,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// HOOK - BeforeServe
|
||||
s.callHookHandler(HOOK_BEFORE_SERVE, request)
|
||||
s.callHookHandler(HookBeforeServe, request)
|
||||
|
||||
// Core serving handling.
|
||||
if !request.IsExited() {
|
||||
@ -133,12 +133,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// HOOK - AfterServe
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_AFTER_SERVE, request)
|
||||
s.callHookHandler(HookAfterServe, request)
|
||||
}
|
||||
|
||||
// HOOK - BeforeOutput
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_BEFORE_OUTPUT, request)
|
||||
s.callHookHandler(HookBeforeOutput, request)
|
||||
}
|
||||
|
||||
// HTTP status checking.
|
||||
@ -176,7 +176,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
request.Response.Flush()
|
||||
// HOOK - AfterOutput
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_AFTER_OUTPUT, request)
|
||||
s.callHookHandler(HookAfterOutput, request)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
// pprof封装.
|
||||
|
||||
package ghttp
|
||||
|
||||
@ -24,7 +23,7 @@ const (
|
||||
|
||||
// EnablePProf enables PProf feature for server.
|
||||
func (s *Server) EnablePProf(pattern ...string) {
|
||||
s.Domain(gDEFAULT_DOMAIN).EnablePProf(pattern...)
|
||||
s.Domain(defaultDomainName).EnablePProf(pattern...)
|
||||
}
|
||||
|
||||
// EnablePProf enables PProf feature for server of specified domain.
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -38,8 +38,8 @@ func (s *Server) routerMapKey(hook, method, path, domain string) string {
|
||||
// parsePattern parses the given pattern to domain, method and path variable.
|
||||
func (s *Server) parsePattern(pattern string) (domain, method, path string, err error) {
|
||||
path = strings.TrimSpace(pattern)
|
||||
domain = gDEFAULT_DOMAIN
|
||||
method = gDEFAULT_METHOD
|
||||
domain = defaultDomainName
|
||||
method = defaultMethod
|
||||
if array, err := gregex.MatchString(`([a-zA-Z]+):(.+)`, pattern); len(array) > 1 && err == nil {
|
||||
path = strings.TrimSpace(array[2])
|
||||
if v := strings.TrimSpace(array[1]); v != "" {
|
||||
@ -85,7 +85,7 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
routerKey := s.routerMapKey(handler.hookName, method, uri, domain)
|
||||
if !s.config.RouteOverWrite {
|
||||
switch handler.itemType {
|
||||
case gHANDLER_TYPE_HANDLER, gHANDLER_TYPE_OBJECT, gHANDLER_TYPE_CONTROLLER:
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
if item, ok := s.routesMap[routerKey]; ok {
|
||||
s.Logger().Fatalf(
|
||||
`duplicated route registry "%s" at %s , already registered at %s`,
|
||||
@ -197,7 +197,7 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
handler: handler,
|
||||
}
|
||||
switch handler.itemType {
|
||||
case gHANDLER_TYPE_HANDLER, gHANDLER_TYPE_OBJECT, gHANDLER_TYPE_CONTROLLER:
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
// Overwrite the route.
|
||||
s.routesMap[routerKey] = []registeredRouteItem{routeItem}
|
||||
default:
|
||||
@ -216,11 +216,11 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
// 3. Route type: {xxx} > :xxx > *xxx.
|
||||
func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerItem) bool {
|
||||
// If they're all type of middleware, the priority is according their registered sequence.
|
||||
if newItem.itemType == gHANDLER_TYPE_MIDDLEWARE && oldItem.itemType == gHANDLER_TYPE_MIDDLEWARE {
|
||||
if newItem.itemType == handlerTypeMiddleware && oldItem.itemType == handlerTypeMiddleware {
|
||||
return false
|
||||
}
|
||||
// The middleware has the most high priority.
|
||||
if newItem.itemType == gHANDLER_TYPE_MIDDLEWARE && oldItem.itemType != gHANDLER_TYPE_MIDDLEWARE {
|
||||
if newItem.itemType == handlerTypeMiddleware && oldItem.itemType != handlerTypeMiddleware {
|
||||
return true
|
||||
}
|
||||
// URI: The deeper the higher (simply check the count of char '/' in the URI).
|
||||
@ -312,18 +312,18 @@ func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerIte
|
||||
|
||||
// It then compares the accuracy of their http method,
|
||||
// the more accurate the more priority.
|
||||
if newItem.router.Method != gDEFAULT_METHOD {
|
||||
if newItem.router.Method != defaultMethod {
|
||||
return true
|
||||
}
|
||||
if oldItem.router.Method != gDEFAULT_METHOD {
|
||||
if oldItem.router.Method != defaultMethod {
|
||||
return true
|
||||
}
|
||||
|
||||
// If they have different router type,
|
||||
// the new router item has more priority than the other one.
|
||||
if newItem.itemType == gHANDLER_TYPE_HANDLER ||
|
||||
newItem.itemType == gHANDLER_TYPE_OBJECT ||
|
||||
newItem.itemType == gHANDLER_TYPE_CONTROLLER {
|
||||
if newItem.itemType == handlerTypeHandler ||
|
||||
newItem.itemType == handlerTypeObject ||
|
||||
newItem.itemType == handlerTypeController {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -175,7 +175,7 @@ func (g *RouterGroup) Bind(items []GroupItem) *RouterGroup {
|
||||
|
||||
// ALL registers a http handler to given route pattern and all http methods.
|
||||
func (g *RouterGroup) ALL(pattern string, object interface{}, params ...interface{}) *RouterGroup {
|
||||
return g.Clone().preBindToLocalArray("HANDLER", gDEFAULT_METHOD+":"+pattern, object, params...)
|
||||
return g.Clone().preBindToLocalArray("HANDLER", defaultMethod+":"+pattern, object, params...)
|
||||
}
|
||||
|
||||
// ALLMap registers http handlers for http methods using map.
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -18,7 +18,7 @@ func (s *Server) BindHookHandler(pattern string, hook string, handler HandlerFun
|
||||
|
||||
func (s *Server) doBindHookHandler(pattern string, hook string, handler HandlerFunc, source string) {
|
||||
s.setHandler(pattern, &handlerItem{
|
||||
itemType: gHANDLER_TYPE_HOOK,
|
||||
itemType: handlerTypeHook,
|
||||
itemName: gdebug.FuncPath(handler),
|
||||
itemFunc: handler,
|
||||
hookName: hook,
|
||||
@ -45,11 +45,11 @@ func (s *Server) callHookHandler(hook string, r *Request) {
|
||||
// r.Router = item.handler.router
|
||||
if err := s.niceCallHookHandler(item.handler.itemFunc, r); err != nil {
|
||||
switch err {
|
||||
case gEXCEPTION_EXIT:
|
||||
case exceptionExit:
|
||||
break
|
||||
case gEXCEPTION_EXIT_ALL:
|
||||
case exceptionExitAll:
|
||||
fallthrough
|
||||
case gEXCEPTION_EXIT_HOOK:
|
||||
case exceptionExitHook:
|
||||
return
|
||||
default:
|
||||
r.Response.WriteStatus(http.StatusInternalServerError, err)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -22,7 +22,7 @@ const (
|
||||
func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
||||
for _, handler := range handlers {
|
||||
s.setHandler(pattern, &handlerItem{
|
||||
itemType: gHANDLER_TYPE_MIDDLEWARE,
|
||||
itemType: handlerTypeMiddleware,
|
||||
itemName: gdebug.FuncPath(handler),
|
||||
itemFunc: handler,
|
||||
})
|
||||
@ -35,7 +35,7 @@ func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
||||
func (s *Server) BindMiddlewareDefault(handlers ...HandlerFunc) {
|
||||
for _, handler := range handlers {
|
||||
s.setHandler(gDEFAULT_MIDDLEWARE_PATTERN, &handlerItem{
|
||||
itemType: gHANDLER_TYPE_MIDDLEWARE,
|
||||
itemType: handlerTypeMiddleware,
|
||||
itemName: gdebug.FuncPath(handler),
|
||||
itemFunc: handler,
|
||||
})
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -52,7 +52,7 @@ func (s *Server) getHandlersWithCache(r *Request) (parsedItems []*handlerParsedI
|
||||
return &handlerCacheItem{parsedItems, hasHook, hasServe}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}, gROUTE_CACHE_DURATION)
|
||||
}, routeCacheDuration)
|
||||
if value != nil {
|
||||
item := value.(*handlerCacheItem)
|
||||
return item.parsedItems, item.hasHook, item.hasServe
|
||||
@ -96,7 +96,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
)
|
||||
|
||||
// Default domain has the most priority when iteration.
|
||||
for _, domain := range []string{gDEFAULT_DOMAIN, domain} {
|
||||
for _, domain := range []string{defaultDomainName, domain} {
|
||||
p, ok := s.serveTree[domain]
|
||||
if !ok {
|
||||
continue
|
||||
@ -149,11 +149,11 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
// Serving handler can only be added to the handler array just once.
|
||||
if hasServe {
|
||||
switch item.itemType {
|
||||
case gHANDLER_TYPE_HANDLER, gHANDLER_TYPE_OBJECT, gHANDLER_TYPE_CONTROLLER:
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
continue
|
||||
}
|
||||
}
|
||||
if item.router.Method == gDEFAULT_METHOD || item.router.Method == method {
|
||||
if item.router.Method == defaultMethod || item.router.Method == method {
|
||||
// Note the rule having no fuzzy rules: len(match) == 1
|
||||
if match, err := gregex.MatchString(item.router.RegRule, path); err == nil && len(match) > 0 {
|
||||
parsedItem := &handlerParsedItem{item, nil}
|
||||
@ -170,14 +170,14 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
}
|
||||
switch item.itemType {
|
||||
// The serving handler can be only added just once.
|
||||
case gHANDLER_TYPE_HANDLER, gHANDLER_TYPE_OBJECT, gHANDLER_TYPE_CONTROLLER:
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
hasServe = true
|
||||
parsedItemList.PushBack(parsedItem)
|
||||
|
||||
// The middleware is inserted before the serving handler.
|
||||
// If there're multiple middleware, they're inserted into the result list by their registering order.
|
||||
// The middleware are also executed by their registered order.
|
||||
case gHANDLER_TYPE_MIDDLEWARE:
|
||||
case handlerTypeMiddleware:
|
||||
if lastMiddlewareElem == nil {
|
||||
lastMiddlewareElem = parsedItemList.PushFront(parsedItem)
|
||||
} else {
|
||||
@ -185,7 +185,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
}
|
||||
|
||||
// HOOK handler, just push it back to the list.
|
||||
case gHANDLER_TYPE_HOOK:
|
||||
case handlerTypeHook:
|
||||
hasHook = true
|
||||
parsedItemList.PushBack(parsedItem)
|
||||
|
||||
@ -211,7 +211,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (item *handlerItem) MarshalJSON() ([]byte, error) {
|
||||
switch item.itemType {
|
||||
case gHANDLER_TYPE_HOOK:
|
||||
case handlerTypeHook:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s (%s)`,
|
||||
@ -221,7 +221,7 @@ func (item *handlerItem) MarshalJSON() ([]byte, error) {
|
||||
item.hookName,
|
||||
),
|
||||
)
|
||||
case gHANDLER_TYPE_MIDDLEWARE:
|
||||
case handlerTypeMiddleware:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s (MIDDLEWARE)`,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -69,7 +69,7 @@ func (s *Server) doBindController(
|
||||
s.Logger().Fatal(err)
|
||||
return
|
||||
}
|
||||
if strings.EqualFold(method, gDEFAULT_METHOD) {
|
||||
if strings.EqualFold(method, defaultMethod) {
|
||||
pattern = s.serveHandlerKey("", path, domain)
|
||||
}
|
||||
// Retrieve a list of methods, create construct corresponding URI.
|
||||
@ -110,7 +110,7 @@ func (s *Server) doBindController(
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, true)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: gHANDLER_TYPE_CONTROLLER,
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
@ -133,7 +133,7 @@ func (s *Server) doBindController(
|
||||
}
|
||||
m[k] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: gHANDLER_TYPE_CONTROLLER,
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
@ -179,7 +179,7 @@ func (s *Server) doBindControllerMethod(
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: gHANDLER_TYPE_CONTROLLER,
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
@ -219,7 +219,7 @@ func (s *Server) doBindControllerRest(
|
||||
key := s.mergeBuildInNameToPattern(methodName+":"+pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: gHANDLER_TYPE_CONTROLLER,
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -28,7 +28,7 @@ func (s *Server) doBindHandler(
|
||||
) {
|
||||
s.setHandler(pattern, &handlerItem{
|
||||
itemName: gdebug.FuncPath(handler),
|
||||
itemType: gHANDLER_TYPE_HANDLER,
|
||||
itemType: handlerTypeHandler,
|
||||
itemFunc: handler,
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -64,7 +64,7 @@ func (s *Server) doBindObject(
|
||||
s.Logger().Fatal(err)
|
||||
return
|
||||
}
|
||||
if strings.EqualFold(method, gDEFAULT_METHOD) {
|
||||
if strings.EqualFold(method, defaultMethod) {
|
||||
pattern = s.serveHandlerKey("", path, domain)
|
||||
}
|
||||
m := make(map[string]*handlerItem)
|
||||
@ -111,7 +111,7 @@ func (s *Server) doBindObject(
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, true)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: gHANDLER_TYPE_OBJECT,
|
||||
itemType: handlerTypeObject,
|
||||
itemFunc: itemFunc,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
@ -133,7 +133,7 @@ func (s *Server) doBindObject(
|
||||
}
|
||||
m[k] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: gHANDLER_TYPE_OBJECT,
|
||||
itemType: handlerTypeObject,
|
||||
itemFunc: itemFunc,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
@ -184,7 +184,7 @@ func (s *Server) doBindObjectMethod(
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: gHANDLER_TYPE_OBJECT,
|
||||
itemType: handlerTypeObject,
|
||||
itemFunc: itemFunc,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
@ -233,7 +233,7 @@ func (s *Server) doBindObjectRest(
|
||||
key := s.mergeBuildInNameToPattern(methodName+":"+pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: gHANDLER_TYPE_OBJECT,
|
||||
itemType: handlerTypeObject,
|
||||
itemFunc: itemFunc,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -12,7 +12,7 @@ import (
|
||||
|
||||
// getStatusHandler retrieves and returns the handler for given status code.
|
||||
func (s *Server) getStatusHandler(status int, r *Request) []HandlerFunc {
|
||||
domains := []string{r.GetHost(), gDEFAULT_DOMAIN}
|
||||
domains := []string{r.GetHost(), defaultDomainName}
|
||||
for _, domain := range domains {
|
||||
if f, ok := s.statusHandlerMap[s.statusHandlerKey(status, domain)]; ok {
|
||||
return f
|
||||
@ -37,7 +37,7 @@ func (s *Server) statusHandlerKey(status int, domain string) string {
|
||||
|
||||
// BindStatusHandler registers handler for given status code.
|
||||
func (s *Server) BindStatusHandler(status int, handler HandlerFunc) {
|
||||
s.addStatusHandler(s.statusHandlerKey(status, gDEFAULT_DOMAIN), handler)
|
||||
s.addStatusHandler(s.statusHandlerKey(status, defaultDomainName), handler)
|
||||
}
|
||||
|
||||
// BindStatusHandlerByMap registers handler for given status code using map.
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -228,14 +228,14 @@ func Test_Middleware_Hook_With_Static(t *testing.T) {
|
||||
s := g.Server(p)
|
||||
a := garray.New(true)
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
a.Append(1)
|
||||
fmt.Println("HOOK_BEFORE_SERVE")
|
||||
fmt.Println("HookBeforeServe")
|
||||
r.Response.Write("a")
|
||||
})
|
||||
group.Hook("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/*", ghttp.HookAfterServe, func(r *ghttp.Request) {
|
||||
a.Append(1)
|
||||
fmt.Println("HOOK_AFTER_SERVE")
|
||||
fmt.Println("HookAfterServe")
|
||||
r.Response.Write("b")
|
||||
})
|
||||
group.Middleware(func(r *ghttp.Request) {
|
||||
@ -539,16 +539,16 @@ func Test_Hook_Middleware_Basic1(t *testing.T) {
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.BindHookHandler("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("a")
|
||||
})
|
||||
s.BindHookHandler("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/*", ghttp.HookAfterServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("b")
|
||||
})
|
||||
s.BindHookHandler("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("c")
|
||||
})
|
||||
s.BindHookHandler("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/*", ghttp.HookAfterServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("d")
|
||||
})
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -20,10 +20,10 @@ func Test_Router_Exit(t *testing.T) {
|
||||
p, _ := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) { r.Response.Write("1") },
|
||||
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) { r.Response.Write("2") },
|
||||
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) { r.Response.Write("3") },
|
||||
ghttp.HOOK_AFTER_OUTPUT: func(r *ghttp.Request) { r.Response.Write("4") },
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) { r.Response.Write("1") },
|
||||
ghttp.HookAfterServe: func(r *ghttp.Request) { r.Response.Write("2") },
|
||||
ghttp.HookBeforeOutput: func(r *ghttp.Request) { r.Response.Write("3") },
|
||||
ghttp.HookAfterOutput: func(r *ghttp.Request) { r.Response.Write("4") },
|
||||
})
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test-start")
|
||||
@ -53,17 +53,17 @@ func Test_Router_ExitHook(t *testing.T) {
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.ExitHook()
|
||||
},
|
||||
@ -91,17 +91,17 @@ func Test_Router_ExitAll(t *testing.T) {
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.ExitAll()
|
||||
},
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -48,10 +48,10 @@ func Test_Router_Group_Group(t *testing.T) {
|
||||
})
|
||||
})
|
||||
group.Group("/hook", func(group *ghttp.RouterGroup) {
|
||||
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook any")
|
||||
})
|
||||
group.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Hook("/:name", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook name")
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -25,10 +25,10 @@ func Test_Router_Group_Hook1(t *testing.T) {
|
||||
})
|
||||
group.ALL("/handler", func(r *ghttp.Request) {
|
||||
r.Response.Write("0")
|
||||
}, ghttp.HOOK_BEFORE_SERVE)
|
||||
}, ghttp.HookBeforeServe)
|
||||
group.ALL("/handler", func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
}, ghttp.HOOK_AFTER_SERVE)
|
||||
}, ghttp.HookAfterServe)
|
||||
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouterMap(false)
|
||||
@ -54,10 +54,10 @@ func Test_Router_Group_Hook2(t *testing.T) {
|
||||
})
|
||||
group.GET("/*", func(r *ghttp.Request) {
|
||||
r.Response.Write("0")
|
||||
}, ghttp.HOOK_BEFORE_SERVE)
|
||||
}, ghttp.HookBeforeServe)
|
||||
group.GET("/*", func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
}, ghttp.HOOK_AFTER_SERVE)
|
||||
}, ghttp.HookAfterServe)
|
||||
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouterMap(false)
|
||||
@ -84,10 +84,10 @@ func Test_Router_Group_Hook3(t *testing.T) {
|
||||
}},
|
||||
{"ALL", "/*", func(r *ghttp.Request) {
|
||||
r.Response.Write("0")
|
||||
}, ghttp.HOOK_BEFORE_SERVE},
|
||||
}, ghttp.HookBeforeServe},
|
||||
{"ALL", "/*", func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
}, ghttp.HOOK_AFTER_SERVE},
|
||||
}, ghttp.HookAfterServe},
|
||||
})
|
||||
|
||||
s.SetPort(p)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -20,10 +20,10 @@ func Test_Router_Hook_Basic(t *testing.T) {
|
||||
p, _ := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) { r.Response.Write("1") },
|
||||
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) { r.Response.Write("2") },
|
||||
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) { r.Response.Write("3") },
|
||||
ghttp.HOOK_AFTER_OUTPUT: func(r *ghttp.Request) { r.Response.Write("4") },
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) { r.Response.Write("1") },
|
||||
ghttp.HookAfterServe: func(r *ghttp.Request) { r.Response.Write("2") },
|
||||
ghttp.HookBeforeOutput: func(r *ghttp.Request) { r.Response.Write("3") },
|
||||
ghttp.HookAfterOutput: func(r *ghttp.Request) { r.Response.Write("4") },
|
||||
})
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
@ -49,7 +49,7 @@ func Test_Router_Hook_Fuzzy_Router(t *testing.T) {
|
||||
i := 1000
|
||||
pattern1 := "/:name/info"
|
||||
s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.SetParam("uid", i)
|
||||
i++
|
||||
},
|
||||
@ -60,7 +60,7 @@ func Test_Router_Hook_Fuzzy_Router(t *testing.T) {
|
||||
|
||||
pattern2 := "/{object}/list/{page}.java"
|
||||
s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeOutput: func(r *ghttp.Request) {
|
||||
r.Response.SetBuffer([]byte(
|
||||
fmt.Sprint(r.Get("object"), "&", r.Get("page"), "&", i),
|
||||
))
|
||||
@ -95,17 +95,17 @@ func Test_Router_Hook_Priority(t *testing.T) {
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
},
|
||||
})
|
||||
@ -134,12 +134,12 @@ func Test_Router_Hook_Multi(t *testing.T) {
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/multi-hook", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/multi-hook", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
ghttp.HookBeforeServe: func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
},
|
||||
})
|
||||
@ -174,7 +174,7 @@ func Test_Router_Hook_ExitAll(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
s.BindHookHandler("/hook/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.BindHookHandler("/hook/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook")
|
||||
r.ExitAll()
|
||||
})
|
||||
|
||||
@ -15,10 +15,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
STATUS_READY = gtimer.STATUS_READY
|
||||
STATUS_RUNNING = gtimer.STATUS_RUNNING
|
||||
STATUS_STOPPED = gtimer.STATUS_STOPPED
|
||||
STATUS_CLOSED = gtimer.STATUS_CLOSED
|
||||
StatusReady = gtimer.StatusReady
|
||||
StatusRunning = gtimer.StatusRunning
|
||||
StatusStopped = gtimer.StatusStopped
|
||||
StatusClosed = gtimer.StatusClosed
|
||||
gDEFAULT_TIMES = math.MaxInt32
|
||||
)
|
||||
|
||||
|
||||
@ -30,7 +30,7 @@ type Cron struct {
|
||||
func New() *Cron {
|
||||
return &Cron{
|
||||
idGen: gtype.NewInt64(),
|
||||
status: gtype.NewInt(STATUS_RUNNING),
|
||||
status: gtype.NewInt(StatusRunning),
|
||||
entries: gmap.NewStrAnyMap(true),
|
||||
logPath: gtype.NewString(),
|
||||
logLevel: gtype.NewInt(glog.LEVEL_PROD),
|
||||
@ -162,7 +162,7 @@ func (c *Cron) Start(name ...string) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
c.status.Set(STATUS_READY)
|
||||
c.status.Set(StatusReady)
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,7 +175,7 @@ func (c *Cron) Stop(name ...string) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
c.status.Set(STATUS_STOPPED)
|
||||
c.status.Set(StatusStopped)
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,7 +188,7 @@ func (c *Cron) Remove(name string) {
|
||||
|
||||
// Close stops and closes current cron.
|
||||
func (c *Cron) Close() {
|
||||
c.status.Set(STATUS_CLOSED)
|
||||
c.status.Set(StatusClosed)
|
||||
}
|
||||
|
||||
// Size returns the size of the timed tasks.
|
||||
|
||||
@ -57,7 +57,7 @@ func (c *Cron) addEntry(pattern string, job func(), singleton bool, name ...stri
|
||||
// It should start running after the entry is added to the entries map,
|
||||
// to avoid the task from running during adding where the entries
|
||||
// does not have the entry information, which might cause panic.
|
||||
entry.entry = gtimer.AddEntry(time.Second, entry.check, singleton, -1, gtimer.STATUS_STOPPED)
|
||||
entry.entry = gtimer.AddEntry(time.Second, entry.check, singleton, -1, gtimer.StatusStopped)
|
||||
c.entries.Set(entry.Name, entry)
|
||||
entry.entry.Start()
|
||||
return entry, nil
|
||||
@ -112,20 +112,20 @@ func (entry *Entry) check() {
|
||||
path := entry.cron.GetLogPath()
|
||||
level := entry.cron.GetLogLevel()
|
||||
switch entry.cron.status.Val() {
|
||||
case STATUS_STOPPED:
|
||||
case StatusStopped:
|
||||
return
|
||||
|
||||
case STATUS_CLOSED:
|
||||
case StatusClosed:
|
||||
glog.Path(path).Level(level).Debugf("[gcron] %s(%s) %s removed", entry.Name, entry.schedule.pattern, entry.jobName)
|
||||
entry.Close()
|
||||
|
||||
case STATUS_READY:
|
||||
case StatusReady:
|
||||
fallthrough
|
||||
case STATUS_RUNNING:
|
||||
case StatusRunning:
|
||||
// Running times check.
|
||||
times := entry.times.Add(-1)
|
||||
if times <= 0 {
|
||||
if entry.entry.SetStatus(STATUS_CLOSED) == STATUS_CLOSED || times < 0 {
|
||||
if entry.entry.SetStatus(StatusClosed) == StatusClosed || times < 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -139,7 +139,7 @@ func (entry *Entry) check() {
|
||||
} else {
|
||||
glog.Path(path).Level(level).Debugf("[gcron] %s(%s) %s end", entry.Name, entry.schedule.pattern, entry.jobName)
|
||||
}
|
||||
if entry.entry.Status() == STATUS_CLOSED {
|
||||
if entry.entry.Status() == StatusClosed {
|
||||
entry.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
@ -28,23 +28,23 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
STATUS_READY = 0 // Job is ready for running.
|
||||
STATUS_RUNNING = 1 // Job is already running.
|
||||
STATUS_STOPPED = 2 // Job is stopped.
|
||||
STATUS_RESET = 3 // Job is reset.
|
||||
STATUS_CLOSED = -1 // Job is closed and waiting to be deleted.
|
||||
gPANIC_EXIT = "exit" // Internal usage for custom job exit function with panic.
|
||||
gDEFAULT_TIMES = math.MaxInt32 // Default limit running times, a big number.
|
||||
gDEFAULT_SLOT_NUMBER = 10 // Default slot number.
|
||||
gDEFAULT_WHEEL_INTERVAL = 100 // Default wheel interval.
|
||||
gDEFAULT_WHEEL_LEVEL = 5 // Default wheel level.
|
||||
cmdEnvKey = "gf.gtimer" // Configuration key for command argument or environment.
|
||||
StatusReady = 0 // Job is ready for running.
|
||||
StatusRunning = 1 // Job is already running.
|
||||
StatusStopped = 2 // Job is stopped.
|
||||
StatusReset = 3 // Job is reset.
|
||||
StatusClosed = -1 // Job is closed and waiting to be deleted.
|
||||
panicExit = "exit" // Internal usage for custom job exit function with panic.
|
||||
defaultTimes = math.MaxInt32 // Default limit running times, a big number.
|
||||
defaultSlotNumber = 10 // Default slot number.
|
||||
defaultWheelInterval = 100 // Default wheel interval.
|
||||
defaultWheelLevel = 5 // Default wheel level.
|
||||
cmdEnvKey = "gf.gtimer" // Configuration key for command argument or environment.
|
||||
)
|
||||
|
||||
var (
|
||||
defaultSlots = gcmd.GetWithEnv(fmt.Sprintf("%s.slots", cmdEnvKey), gDEFAULT_SLOT_NUMBER).Int()
|
||||
defaultLevel = gcmd.GetWithEnv(fmt.Sprintf("%s.level", cmdEnvKey), gDEFAULT_WHEEL_LEVEL).Int()
|
||||
defaultInterval = gcmd.GetWithEnv(fmt.Sprintf("%s.interval", cmdEnvKey), gDEFAULT_WHEEL_INTERVAL).Duration() * time.Millisecond
|
||||
defaultSlots = gcmd.GetWithEnv(fmt.Sprintf("%s.slots", cmdEnvKey), defaultSlotNumber).Int()
|
||||
defaultLevel = gcmd.GetWithEnv(fmt.Sprintf("%s.level", cmdEnvKey), defaultWheelLevel).Int()
|
||||
defaultInterval = gcmd.GetWithEnv(fmt.Sprintf("%s.interval", cmdEnvKey), defaultWheelInterval).Duration() * time.Millisecond
|
||||
defaultTimer = New(defaultSlots, defaultInterval, defaultLevel)
|
||||
)
|
||||
|
||||
@ -130,5 +130,5 @@ func DelayAddTimes(delay time.Duration, interval time.Duration, times int, job J
|
||||
// mechanism internally implementing this feature, which is designed for simplification
|
||||
// and convenience.
|
||||
func Exit() {
|
||||
panic(gPANIC_EXIT)
|
||||
panic(panicExit)
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -32,7 +32,7 @@ type JobFunc = func()
|
||||
// addEntry adds a timing job to the wheel.
|
||||
func (w *wheel) addEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry {
|
||||
if times <= 0 {
|
||||
times = gDEFAULT_TIMES
|
||||
times = defaultTimes
|
||||
}
|
||||
var (
|
||||
ms = interval.Nanoseconds() / 1e6
|
||||
@ -98,22 +98,22 @@ func (entry *Entry) SetStatus(status int) int {
|
||||
|
||||
// Start starts the job.
|
||||
func (entry *Entry) Start() {
|
||||
entry.status.Set(STATUS_READY)
|
||||
entry.status.Set(StatusReady)
|
||||
}
|
||||
|
||||
// Stop stops the job.
|
||||
func (entry *Entry) Stop() {
|
||||
entry.status.Set(STATUS_STOPPED)
|
||||
entry.status.Set(StatusStopped)
|
||||
}
|
||||
|
||||
//Reset reset the job.
|
||||
func (entry *Entry) Reset() {
|
||||
entry.status.Set(STATUS_RESET)
|
||||
entry.status.Set(StatusReset)
|
||||
}
|
||||
|
||||
// Close closes the job, and then it will be removed from the timer.
|
||||
func (entry *Entry) Close() {
|
||||
entry.status.Set(STATUS_CLOSED)
|
||||
entry.status.Set(StatusClosed)
|
||||
}
|
||||
|
||||
// IsSingleton checks and returns whether the job in singleton mode.
|
||||
@ -139,11 +139,11 @@ func (entry *Entry) Run() {
|
||||
// check checks if the job should be run in given ticks and timestamp milliseconds.
|
||||
func (entry *Entry) check(nowTicks int64, nowMs int64) (runnable, addable bool) {
|
||||
switch entry.status.Val() {
|
||||
case STATUS_STOPPED:
|
||||
case StatusStopped:
|
||||
return false, true
|
||||
case STATUS_CLOSED:
|
||||
case StatusClosed:
|
||||
return false, false
|
||||
case STATUS_RESET:
|
||||
case StatusReset:
|
||||
return false, true
|
||||
}
|
||||
// Firstly checks using the ticks, this may be low precision as one tick is a little bit long.
|
||||
@ -171,7 +171,7 @@ func (entry *Entry) check(nowTicks int64, nowMs int64) (runnable, addable bool)
|
||||
// Singleton mode check.
|
||||
if entry.IsSingleton() {
|
||||
// Note that it is atomic operation to ensure concurrent safety.
|
||||
if entry.status.Set(STATUS_RUNNING) == STATUS_RUNNING {
|
||||
if entry.status.Set(StatusRunning) == StatusRunning {
|
||||
return false, true
|
||||
}
|
||||
}
|
||||
@ -179,14 +179,14 @@ func (entry *Entry) check(nowTicks int64, nowMs int64) (runnable, addable bool)
|
||||
times := entry.times.Add(-1)
|
||||
if times <= 0 {
|
||||
// Note that it is atomic operation to ensure concurrent safety.
|
||||
if entry.status.Set(STATUS_CLOSED) == STATUS_CLOSED || times < 0 {
|
||||
if entry.status.Set(StatusClosed) == StatusClosed || times < 0 {
|
||||
return false, false
|
||||
}
|
||||
}
|
||||
// This means it does not limit the running times.
|
||||
// I know it's ugly, but it is surely high performance for running times limit.
|
||||
if times < 2000000000 && times > 1000000000 {
|
||||
entry.times.Set(gDEFAULT_TIMES)
|
||||
entry.times.Set(defaultTimes)
|
||||
}
|
||||
return true, true
|
||||
}
|
||||
|
||||
@ -20,13 +20,13 @@ func (w *wheel) start() {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
switch w.timer.status.Val() {
|
||||
case STATUS_RUNNING:
|
||||
case StatusRunning:
|
||||
w.proceed()
|
||||
|
||||
case STATUS_STOPPED:
|
||||
case StatusStopped:
|
||||
// Do nothing.
|
||||
|
||||
case STATUS_CLOSED:
|
||||
case StatusClosed:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
@ -61,14 +61,14 @@ func (w *wheel) proceed() {
|
||||
go func(entry *Entry) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
if err != gPANIC_EXIT {
|
||||
if err != panicExit {
|
||||
panic(err)
|
||||
} else {
|
||||
entry.Close()
|
||||
}
|
||||
}
|
||||
if entry.Status() == STATUS_RUNNING {
|
||||
entry.SetStatus(STATUS_READY)
|
||||
if entry.Status() == StatusRunning {
|
||||
entry.SetStatus(StatusReady)
|
||||
}
|
||||
}()
|
||||
entry.job()
|
||||
@ -76,9 +76,9 @@ func (w *wheel) proceed() {
|
||||
}
|
||||
// If rolls on the job.
|
||||
if addable {
|
||||
//If STATUS_RESET , reset to runnable state.
|
||||
if entry.Status() == STATUS_RESET {
|
||||
entry.SetStatus(STATUS_READY)
|
||||
//If StatusReset , reset to runnable state.
|
||||
if entry.Status() == StatusReset {
|
||||
entry.SetStatus(StatusReady)
|
||||
}
|
||||
entry.wheel.timer.doAddEntryByParent(entry.rawIntervalMs, entry)
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -38,17 +38,17 @@ type wheel struct {
|
||||
// New creates and returns a Hierarchical Timing Wheel designed timer.
|
||||
// The parameter <interval> specifies the interval of the timer.
|
||||
// The optional parameter <level> specifies the wheels count of the timer,
|
||||
// which is gDEFAULT_WHEEL_LEVEL in default.
|
||||
// which is defaultWheelLevel in default.
|
||||
func New(slot int, interval time.Duration, level ...int) *Timer {
|
||||
if slot <= 0 {
|
||||
panic(fmt.Sprintf("invalid slot number: %d", slot))
|
||||
}
|
||||
length := gDEFAULT_WHEEL_LEVEL
|
||||
length := defaultWheelLevel
|
||||
if len(level) > 0 {
|
||||
length = level[0]
|
||||
}
|
||||
t := &Timer{
|
||||
status: gtype.NewInt(STATUS_RUNNING),
|
||||
status: gtype.NewInt(StatusRunning),
|
||||
wheels: make([]*wheel, length),
|
||||
length: length,
|
||||
number: slot,
|
||||
@ -62,7 +62,7 @@ func New(slot int, interval time.Duration, level ...int) *Timer {
|
||||
}
|
||||
w := t.newWheel(i, slot, n)
|
||||
t.wheels[i] = w
|
||||
t.wheels[i-1].addEntry(n, w.proceed, false, gDEFAULT_TIMES, STATUS_READY)
|
||||
t.wheels[i-1].addEntry(n, w.proceed, false, defaultTimes, StatusReady)
|
||||
} else {
|
||||
t.wheels[i] = t.newWheel(i, slot, interval)
|
||||
}
|
||||
@ -91,7 +91,7 @@ func (t *Timer) newWheel(level int, slot int, interval time.Duration) *wheel {
|
||||
|
||||
// Add adds a timing job to the timer, which runs in interval of <interval>.
|
||||
func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, false, gDEFAULT_TIMES, STATUS_READY)
|
||||
return t.doAddEntry(interval, job, false, defaultTimes, StatusReady)
|
||||
}
|
||||
|
||||
// AddEntry adds a timing job to the timer with detailed parameters.
|
||||
@ -111,17 +111,17 @@ func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, ti
|
||||
|
||||
// AddSingleton is a convenience function for add singleton mode job.
|
||||
func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, true, gDEFAULT_TIMES, STATUS_READY)
|
||||
return t.doAddEntry(interval, job, true, defaultTimes, StatusReady)
|
||||
}
|
||||
|
||||
// AddOnce is a convenience function for adding a job which only runs once and then exits.
|
||||
func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, true, 1, STATUS_READY)
|
||||
return t.doAddEntry(interval, job, true, 1, StatusReady)
|
||||
}
|
||||
|
||||
// AddTimes is a convenience function for adding a job which is limited running times.
|
||||
func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, true, times, STATUS_READY)
|
||||
return t.doAddEntry(interval, job, true, times, StatusReady)
|
||||
}
|
||||
|
||||
// DelayAdd adds a timing job after delay of <interval> duration.
|
||||
@ -166,17 +166,17 @@ func (t *Timer) DelayAddTimes(delay time.Duration, interval time.Duration, times
|
||||
|
||||
// Start starts the timer.
|
||||
func (t *Timer) Start() {
|
||||
t.status.Set(STATUS_RUNNING)
|
||||
t.status.Set(StatusRunning)
|
||||
}
|
||||
|
||||
// Stop stops the timer.
|
||||
func (t *Timer) Stop() {
|
||||
t.status.Set(STATUS_STOPPED)
|
||||
t.status.Set(StatusStopped)
|
||||
}
|
||||
|
||||
// Close closes the timer.
|
||||
func (t *Timer) Close() {
|
||||
t.status.Set(STATUS_CLOSED)
|
||||
t.status.Set(StatusClosed)
|
||||
}
|
||||
|
||||
// doAddEntry adds a timing job to timer for internal usage.
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -44,7 +44,7 @@ func TestAddEntry(t *testing.T) {
|
||||
array := garray.New(true)
|
||||
gtimer.AddEntry(200*time.Millisecond, func() {
|
||||
array.Append(1)
|
||||
}, false, 2, gtimer.STATUS_READY)
|
||||
}, false, 2, gtimer.StatusReady)
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
t.Assert(array.Len(), 2)
|
||||
})
|
||||
@ -91,7 +91,7 @@ func TestDelayAddEntry(t *testing.T) {
|
||||
array := garray.New(true)
|
||||
gtimer.DelayAddEntry(200*time.Millisecond, 200*time.Millisecond, func() {
|
||||
array.Append(1)
|
||||
}, false, 2, gtimer.STATUS_READY)
|
||||
}, false, 2, gtimer.StatusReady)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
t.Assert(array.Len(), 0)
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -162,7 +162,7 @@ func TestTimer_DelayAddEntry(t *testing.T) {
|
||||
array := garray.New(true)
|
||||
timer.DelayAddEntry(200*time.Millisecond, 200*time.Millisecond, func() {
|
||||
array.Append(1)
|
||||
}, false, 100, gtimer.STATUS_READY)
|
||||
}, false, 100, gtimer.StatusReady)
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
t.Assert(array.Len(), 0)
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -36,7 +36,7 @@ func TestEntry_Start_Stop_Close(t *testing.T) {
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
t.Assert(array.Len(), 2)
|
||||
|
||||
t.Assert(entry.Status(), gtimer.STATUS_CLOSED)
|
||||
t.Assert(entry.Status(), gtimer.StatusClosed)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user