Skip to content

Commit

Permalink
Merge pull request #16 from guregu/httptreemux
Browse files Browse the repository at this point in the history
Switch router to httptreemux
  • Loading branch information
guregu committed Feb 12, 2016
2 parents 1eac903 + 6b3227d commit 3cfea74
Show file tree
Hide file tree
Showing 10 changed files with 850 additions and 85 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## kami [![GoDoc](https://godoc.org/github.com/guregu/kami?status.svg)](https://godoc.org/github.com/guregu/kami) [![Coverage](http://gocover.io/_badge/github.com/guregu/kami?0)](http://gocover.io/github.com/guregu/kami)
`import "github.com/guregu/kami"` [or](http://gopkg.in) `import "gopkg.in/guregu/kami.v1"`

kami (神) is a tiny web framework using [x/net/context](https://blog.golang.org/context) for request context and [HttpRouter](https://github.com/julienschmidt/httprouter) for routing. It includes a simple system for running hierarchical middleware before and after requests, in addition to log and panic hooks. Graceful restart via einhorn is also supported.
kami (神) is a tiny web framework using [x/net/context](https://blog.golang.org/context) for request context and [httptreemux](https://github.com/dimfeld/httptreemux) for routing. It includes a simple system for running hierarchical middleware before and after requests, in addition to log and panic hooks. Graceful restart via einhorn is also supported.

kami is designed to be used as central registration point for your routes, middleware, and context "god object". You are encouraged to use the global functions, but kami supports multiple muxes with `kami.New()`.

Expand Down Expand Up @@ -97,7 +97,7 @@ func FromContext(ctx context.Context) string {

### Usage

* Set up routes using `kami.Get("/path", handler)`, `kami.Post(...)`, etc. You can use [named parameters](https://github.com/julienschmidt/httprouter#named-parameters) or [wildcards](https://github.com/julienschmidt/httprouter#catch-all-parameters) in URLs like `/hello/:name/edit` or `/files/*path`, and access them using the context kami gives you: `kami.Param(ctx, "name")`. The following kinds of handlers are accepted:
* Set up routes using `kami.Get("/path", handler)`, `kami.Post(...)`, etc. You can use named parameters or wildcards in URLs like `/hello/:name/edit` or `/files/*path`, and access them using the context kami gives you: `kami.Param(ctx, "name")`. See the [routing rules](https://github.com/dimfeld/httptreemux#routing-rules) and [routing priority](https://github.com/dimfeld/httptreemux#routing-priority). The following kinds of handlers are accepted:
* types that implement `kami.ContextHandler`
* `func(context.Context, http.ResponseWriter, *http.Request)`
* types that implement `http.Handler`
Expand Down Expand Up @@ -239,5 +239,5 @@ MIT

### Acknowledgements

* [HttpRouter](https://github.com/julienschmidt/httprouter): router
* [httptreemux](https://github.com/dimfeld/httptreemux): router
* [Goji](https://github.com/zenazn/goji): graceful, WriterProxy
21 changes: 21 additions & 0 deletions internal/treemux/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Daniel Imfeld

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions internal/treemux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
treemux [![GoDoc](http://godoc.org/github.com/dimfeld/httptreemux?status.png)](http://godoc.org/github.com/guregu/kami/internal/treemux)
===========

Generic router ripped from [httptreemux](https://github.com/dimfeld/httptreemux).
54 changes: 54 additions & 0 deletions internal/treemux/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Package treemux is a generic treemux ripped from httptreemux.
package treemux

import (
"fmt"
)

type TreeMux struct {
root *node
}

func (t *TreeMux) Dump() string {
return t.root.dumpTree("", "")
}

func (t *TreeMux) Set(path string, v interface{}) {
if path[0] != '/' {
panic(fmt.Sprintf("Path %s must start with slash", path))
}

node := t.root.addPath(path[1:], nil)
node.setValue(v)
}

func (t *TreeMux) Get(path string) (interface{}, map[string]string) {
n, params := t.root.search(path[1:])
if n == nil {
return nil, nil
}

var paramMap map[string]string
if len(params) != 0 {
if len(params) != len(n.leafWildcardNames) {
// Need better behavior here. Should this be a panic?
panic(fmt.Sprintf("treemux parameter list length mismatch: %v, %v",
params, n.leafWildcardNames))
}

paramMap = make(map[string]string)
numParams := len(params)
for index := 0; index < numParams; index++ {
paramMap[n.leafWildcardNames[numParams-index-1]] = params[index]
}
}

return n.leafValue, paramMap
}

func New() *TreeMux {
root := &node{path: "/"}
return &TreeMux{
root: root,
}
}
Loading

0 comments on commit 3cfea74

Please sign in to comment.