urlrouter

package module
v0.0.0-...-ad3a6f0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2014 License: MIT Imports: 1 Imported by: 40

README

Kocha-urlrouter Build Status

Better URL router collection for Go

Note: Kocha-urlrouter will be used as a sandbox for some implementations of a URL router. If you want a fast URL router, Please use Denco instead.

Installation

Interface:

go get -u github.com/naoina/kocha-urlrouter

Implementation:

go get -u github.com/naoina/kocha-urlrouter/doublearray

Kocha-urlrouter has multiple URL router implementations. See Implementations.

Usage

package main

import (
    "github.com/naoina/kocha-urlrouter"
    _ "github.com/naoina/kocha-urlrouter/doublearray"
)

type route struct {
    name string
}

func main() {
    router := urlrouter.NewURLRouter("doublearray")
    router.Build([]urlrouter.Record{
        urlrouter.NewRecord("/", &route{"root"}),
        urlrouter.NewRecord("/user/:id", &route{"user"}),
        urlrouter.NewRecord("/user/:name/:id", &route{"username"}),
        urlrouter.NewRecord("/static/*filepath", &route{"static"}),
    })

    router.Lookup("/")                    // returns *route{"root"}, nil slice.
    router.Lookup("/user/hoge")           // returns *route{"user"}, []urlrouter.Param{{"id", "hoge"}}
    router.Lookup("/user/hoge/7")           // returns *route{"username"}, []urlrouter.Param{{"name", "hoge"}, {"id", "7"}}
    router.Lookup("/static/path/to/file") // returns *route{"static"}, []urlrouter.Param{{"filepath", "path/to/file"}}
}

See Godoc for more docs.

Implementations

  • Double-Array github.com/naoina/kocha-urlrouter/doublearray
  • Regular-Expression github.com/naoina/kocha-urlrouter/regexp
  • Ternary Search Tree github.com/naoina/kocha-urlrouter/tst

Benchmark

cd $GOPATH/github.com/naoina/kocha-urlrouter
go test -bench . -benchmem ./...

License

Kocha-urlrouter is licensed under the MIT

Documentation

Index

Constants

View Source
const (
	ParamCharacter    = ':'
	WildcardCharacter = '*'
)

Variables

This section is empty.

Functions

func IsMetaChar

func IsMetaChar(c byte) bool

isMetaChar returns whether the meta character.

func NextSeparator

func NextSeparator(path string, start int) int

NextSeparator returns an index of next separator in path.

func ParamNames

func ParamNames(path string) (names []string)

ParamNames returns parameter names in given path. It returns names which meta character is prefixed.

func Register

func Register(name string, router Router)

Register registers a Router with name.

Types

type Param

type Param struct {
	Name  string
	Value string
}

param represents a name and value of path parameter.

type Record

type Record struct {
	// Key for a router construction.
	Key string

	// Result value for Key.
	Value interface{}
}

Record represents a record data for a router construction.

func NewRecord

func NewRecord(key string, value interface{}) Record

NewRecord returns a new Record.

type Router

type Router interface {
	// New returns a new URLRouter.
	New() URLRouter
}

Router is an interface of factory of URLRouter.

type URLRouter

type URLRouter interface {
	// Lookup returns data and path parameters that associated with path.
	// params is a slice of the Param that arranged in the order in which parameters appeared.
	// e.g. when built routing path is "/path/:id/:name" and given path is "/path/to/1/alice". params order is [{"id": "1"}, {"name": "alice"}], not [{"name": "alice"}, {"id": "1"}].
	// If failed to lookup, data will be nil.
	Lookup(path string) (data interface{}, params []Param)

	// Build builds URL router from records.
	Build(records []Record) error
}

URLRouter is an interface that must be implemented by a URL router.

func NewURLRouter

func NewURLRouter(name string) URLRouter

NewURLRouter returns the URLRouter with the specified name.

Directories

Path Synopsis
A URL router implemented by Double-Array Trie.
A URL router implemented by Double-Array Trie.
A URL router implemented by Regular-Expression.
A URL router implemented by Regular-Expression.
A URL router implemented by Ternary Search Tree.
A URL router implemented by Ternary Search Tree.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL