-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from guregu/httptreemux
Switch router to httptreemux
- Loading branch information
Showing
10 changed files
with
850 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
Oops, something went wrong.