Fetch the repository succeeded.
package goa
import (
"bytes"
"net/http"
"reflect"
"runtime"
"gitee.com/go-better/dev/type/tree/regexptree"
)
type Router struct {
beforeLookup func(ctx *ContextBeforeLookup)
RouterGroup
notFound []func(*Context)
}
func New() *Router {
return &Router{
RouterGroup: RouterGroup{routes: make(map[string]*regexptree.Node)},
notFound: []func(*Context){defaultNotFound},
}
}
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ctxBeforeLookup := ContextBeforeLookup{Request: req, ResponseWriter: rw}
if r.beforeLookup != nil {
r.beforeLookup(&ctxBeforeLookup)
}
handlers, params := r.Lookup(req.Method, req.URL.Path)
c := &Context{ContextBeforeLookup: ctxBeforeLookup, handlers: handlers, params: params, index: -1}
if len(handlers) == 0 {
c.handlers = r.notFound
}
c.Next()
}
// BeforeLookup regiter a function to be run before every route Lookup.
func (r *Router) BeforeLookup(fun func(ctx *ContextBeforeLookup)) {
r.beforeLookup = fun
}
func (r *Router) Use(handlers ...func(*Context)) {
r.RouterGroup.Use(handlers...)
last := len(r.notFound) - 1
notFound := r.notFound[last]
r.notFound = append(r.notFound[:last], handlers...)
r.notFound = append(r.notFound, notFound)
}
func (r *Router) NotFound(handler func(*Context)) {
last := len(r.notFound) - 1
r.notFound[last] = handler
}
func defaultNotFound(c *Context) {
if c.ResponseWriter != nil {
c.WriteHeader(404)
c.Write([]byte(`{"code":"404","message":"Not Found."}`))
}
}
func (r *Router) String() string {
var buf bytes.Buffer
buf.WriteString("{\n")
buf.WriteString(r.RoutesString())
if len(r.notFound) > 0 {
buf.WriteString(" notFound: " + funcName(r.notFound[len(r.notFound)-1]) + "\n")
}
buf.WriteString("}\n")
return buf.String()
}
func funcName(fun interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(fun).Pointer()).Name()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。