2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
import "net/http"
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
type (
|
|
|
|
router struct {
|
2015-04-01 17:05:54 +02:00
|
|
|
trees map[string]*node
|
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
node struct {
|
2015-04-01 17:05:54 +02:00
|
|
|
label byte
|
|
|
|
prefix string
|
|
|
|
handler HandlerFunc
|
|
|
|
edges edges
|
2015-04-04 19:44:48 +02:00
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
edges []*node
|
|
|
|
param struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
Params []param
|
|
|
|
)
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
func NewRouter(e *Echo) (r *router) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r = &router{
|
2015-04-01 17:05:54 +02:00
|
|
|
trees: make(map[string]*node),
|
|
|
|
echo: e,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-01 17:05:54 +02:00
|
|
|
for _, m := range methods {
|
|
|
|
r.trees[m] = &node{
|
|
|
|
prefix: "",
|
|
|
|
edges: edges{},
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 17:05:54 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-02 23:41:36 +02:00
|
|
|
func (r *router) Add(method, path string, h HandlerFunc, echo *Echo) {
|
2015-04-05 23:21:03 +02:00
|
|
|
for i, l := 0, len(path); i < l; i++ {
|
2015-03-01 19:45:13 +02:00
|
|
|
if path[i] == ':' {
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], nil, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
for ; i < l && path[i] != '/'; i++ {
|
|
|
|
}
|
|
|
|
if i == l {
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], h, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], nil, echo)
|
2015-03-07 07:55:51 +02:00
|
|
|
} else if path[i] == '*' {
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], nil, echo)
|
|
|
|
r.insert(method, path[:l], h, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path, h, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 17:05:29 +02:00
|
|
|
func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
|
2015-04-01 17:05:54 +02:00
|
|
|
cn := r.trees[method] // Current node as root
|
2015-03-01 19:45:13 +02:00
|
|
|
search := path
|
|
|
|
|
|
|
|
for {
|
|
|
|
sl := len(search)
|
|
|
|
pl := len(cn.prefix)
|
|
|
|
l := lcp(search, cn.prefix)
|
|
|
|
|
|
|
|
if l == 0 {
|
|
|
|
// At root node
|
|
|
|
cn.label = search[0]
|
|
|
|
cn.prefix = search
|
|
|
|
if h != nil {
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = h
|
2015-04-02 23:41:36 +02:00
|
|
|
cn.echo = echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
} else if l < pl {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Split node
|
2015-04-12 17:05:29 +02:00
|
|
|
n := newNode(cn.prefix[l:], cn.handler, cn.edges, cn.echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
cn.edges = edges{n} // Add to parent
|
|
|
|
|
|
|
|
// Reset parent node
|
|
|
|
cn.label = cn.prefix[0]
|
|
|
|
cn.prefix = cn.prefix[:l]
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = nil
|
2015-04-02 23:41:36 +02:00
|
|
|
cn.echo = nil
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
if l == sl {
|
|
|
|
// At parent node
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = h
|
2015-04-02 23:41:36 +02:00
|
|
|
cn.echo = echo
|
2015-03-01 19:45:13 +02:00
|
|
|
} else {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Create child node
|
2015-04-12 17:05:29 +02:00
|
|
|
n = newNode(search[l:], h, edges{}, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
cn.edges = append(cn.edges, n)
|
|
|
|
}
|
|
|
|
} else if l < sl {
|
|
|
|
search = search[l:]
|
|
|
|
e := cn.findEdge(search[0])
|
2015-04-08 23:40:49 +02:00
|
|
|
if e != nil {
|
|
|
|
// Go deeper
|
2015-03-01 19:45:13 +02:00
|
|
|
cn = e
|
2015-04-08 23:40:49 +02:00
|
|
|
continue
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-08 23:40:49 +02:00
|
|
|
// Create child node
|
2015-04-12 17:05:29 +02:00
|
|
|
n := newNode(search, h, edges{}, echo)
|
2015-04-08 23:40:49 +02:00
|
|
|
cn.edges = append(cn.edges, n)
|
2015-03-01 19:45:13 +02:00
|
|
|
} else {
|
|
|
|
// Node already exists
|
|
|
|
if h != nil {
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = h
|
2015-04-02 23:41:36 +02:00
|
|
|
cn.echo = echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 23:40:49 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 17:05:29 +02:00
|
|
|
func newNode(pfx string, h HandlerFunc, e edges, echo *Echo) (n *node) {
|
2015-03-01 19:45:13 +02:00
|
|
|
n = &node{
|
2015-04-01 17:05:54 +02:00
|
|
|
label: pfx[0],
|
|
|
|
prefix: pfx,
|
|
|
|
handler: h,
|
|
|
|
edges: e,
|
2015-04-04 19:44:48 +02:00
|
|
|
echo: echo,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
func (n *node) findEdge(l byte) *node {
|
|
|
|
for _, e := range n.edges {
|
|
|
|
if e.label == l {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Length of longest common prefix
|
|
|
|
func lcp(a, b string) (i int) {
|
|
|
|
max := len(a)
|
|
|
|
l := len(b)
|
|
|
|
if l < max {
|
|
|
|
max = l
|
|
|
|
}
|
|
|
|
for ; i < max && a[i] == b[i]; i++ {
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *Echo) {
|
2015-04-01 17:05:54 +02:00
|
|
|
cn := r.trees[method] // Current node as root
|
2015-03-01 19:45:13 +02:00
|
|
|
search := path
|
|
|
|
n := 0 // Param count
|
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Search order static > param > catch-all
|
2015-03-01 19:45:13 +02:00
|
|
|
for {
|
2015-04-11 22:49:45 +02:00
|
|
|
if search == "" || search == cn.prefix { // Fix me
|
2015-04-04 00:40:36 +02:00
|
|
|
// Found
|
2015-04-01 17:05:54 +02:00
|
|
|
h = cn.handler
|
2015-04-02 23:41:36 +02:00
|
|
|
echo = cn.echo
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-11 20:10:19 +02:00
|
|
|
pl := len(cn.prefix)
|
2015-03-01 19:45:13 +02:00
|
|
|
l := lcp(search, cn.prefix)
|
2015-04-11 22:49:45 +02:00
|
|
|
if l == pl {
|
|
|
|
search = search[l:]
|
2015-04-11 20:10:19 +02:00
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Static node
|
|
|
|
e := cn.findEdge(search[0])
|
|
|
|
if e != nil {
|
|
|
|
cn = e
|
|
|
|
continue
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Param node
|
|
|
|
e = cn.findEdge(':')
|
|
|
|
if e != nil {
|
|
|
|
cn = e
|
|
|
|
i, l := 0, len(search)
|
|
|
|
for ; i < l && search[i] != '/'; i++ {
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-11 19:09:41 +02:00
|
|
|
p := params[:n+1]
|
|
|
|
p[n].Name = cn.prefix[1:]
|
|
|
|
p[n].Value = search[:i]
|
|
|
|
n++
|
|
|
|
search = search[i:]
|
|
|
|
continue
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Catch-all node
|
|
|
|
e = cn.findEdge('*')
|
|
|
|
if e != nil {
|
2015-03-09 08:58:10 +02:00
|
|
|
cn = e
|
2015-04-11 19:09:41 +02:00
|
|
|
p := params[:n+1]
|
|
|
|
p[n].Name = "_name"
|
|
|
|
p[n].Value = search
|
|
|
|
search = "" // End search
|
2015-03-09 08:58:10 +02:00
|
|
|
continue
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-11 19:09:41 +02:00
|
|
|
|
2015-04-12 17:05:29 +02:00
|
|
|
// Not found
|
2015-04-04 00:40:36 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:02:23 +02:00
|
|
|
func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2015-04-09 23:59:31 +02:00
|
|
|
c := r.echo.pool.Get().(*Context)
|
|
|
|
h, _ := r.Find(req.Method, req.URL.Path, c.params)
|
|
|
|
c.Response.Writer = w
|
2015-04-01 17:05:54 +02:00
|
|
|
if h != nil {
|
|
|
|
h(c)
|
|
|
|
} else {
|
|
|
|
r.echo.notFoundHandler(c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-09 07:23:47 +02:00
|
|
|
r.echo.pool.Put(c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|