代码拉取完成,页面将自动刷新
package grouter
import (
"fmt"
"strings"
"gitee.com/micro-tools/wf/text/gstr"
"gitee.com/micro-tools/wf/debug/gdebug"
"github.com/golang/glog"
)
const stackFilterKey = "extend/utils/grouter"
type (
// Request handler function.
HandlerFunc = func(r *Request)
// RouterGroup is a group wrapping multiple routes and middleware.
RouterGroup struct {
server *GRouter // Server.
prefix string // Prefix for sub-route.
}
preBindItem struct {
group *RouterGroup
pattern string
object interface{} // Can be handler, controller or object.
source string // Handler is register at certain source file path:line.
bound bool // Is this item bound to server.
}
// Router object.
Router struct {
Uri string // URI.
RegRule string // Parsed regular expression for route matching.
RegNames []string // Parsed router parameter names.
Priority int // Just for reference.
}
// handlerItem is the registered handler for route handling,
// including middleware and hook functions.
handlerItem struct {
itemId int // Unique handler item id mark.
itemName string // Handler name, which is automatically retrieved from runtime stack when registered.
itemFunc HandlerFunc // Handler address.
router *Router // Router object.
source string // Source file path:line when registering.
}
// handlerParsedItem is the item parsed from URL.Path.
handlerParsedItem struct {
handler *handlerItem // Handler information.
values map[string]string // Router values parsed from URL.Path.
}
// handlerCacheItem is an item just for internal router searching cache.
handlerCacheItem struct {
parsedItems []*handlerParsedItem
}
Request struct {
URL string
Payload interface{}
RouterMap map[string]string // Router parameters map, which might be nil if there're no router parameters.
}
)
// GET registers a http handler to given route pattern and http method: GET.
func (g *RouterGroup) GET(pattern string, object HandlerFunc) *RouterGroup {
return g.Clone().preBindToLocalArray(pattern, object)
}
// Clone returns a new router group which is a clone of current group.
func (g *RouterGroup) Clone() *RouterGroup {
newGroup := &RouterGroup{
server: g.server,
prefix: g.prefix,
}
return newGroup
}
// preBindToLocalArray adds the route registering parameters to internal variable array for lazily registering feature.
func (g *RouterGroup) preBindToLocalArray(pattern string, object interface{}) *RouterGroup {
_, file, line := gdebug.CallerWithFilter(stackFilterKey)
g.server.preBindItems = append(g.server.preBindItems, &preBindItem{
group: g,
pattern: pattern,
object: object,
source: fmt.Sprintf(`%s:%d`, file, line),
})
return g
}
// doBindRoutersToServer does really registering for the group.
func (g *RouterGroup) doBindRoutersToServer(item *preBindItem) *RouterGroup {
var (
pattern = item.pattern
object = item.object
source = item.source
)
prefix := g.getPrefix()
if len(prefix) > 0 {
path, err := g.server.parsePattern(pattern)
if err != nil {
glog.Infof("invalid pattern: %s", pattern)
}
pattern = prefix + "/" + strings.TrimLeft(path, "/")
}
// Filter repeated char '/'.
pattern = gstr.Replace(pattern, "//", "/")
if h, ok := object.(HandlerFunc); ok {
if g.server != nil {
g.server.doBindHandler(pattern, h, source)
}
}
return g
}
// getPrefix returns the route prefix of the group
func (g *RouterGroup) getPrefix() string {
prefix := g.prefix
return prefix
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。