routing

package module
v0.0.0-...-46d9fe4 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2016 License: MIT Imports: 4 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

routing

Build Status Coverage Status

URL routing based on trie.

routing is only used to define and match URLs with some custom meta infomation. And basing on it, you can build your own http router with any additional feature.

Installation

go get -u github.com/DavidCai1993/routing

Support signatures

  • string: /hello
  • separated string: /a|b|c
  • regex: /([0-9a-f]{24})
  • named parameter: /:id
  • named separated string: /:id(a|b|c)
  • named regex: /:id([0-9a-f]{24})

Documentation

API documentation can be found here: https://godoc.org/github.com/DavidCai1993/routing

Usage

router := routing.New()

router.Define("/:type(a|b)/:id(0-9a-f]{24})", yourHandler)

callback, params, ok := router.Match("/a/8")

fmt.Println(ok)
// -> true

fmt.Println(callback.(http.Handler))

fmt.Println(params)
// -> map[string]string{"type": "a", "id": "8"}

Documentation

Overview

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/DavidCai1993/routing"
)

var httpHandler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello World"))
})

func main() {
	router := routing.New()

	router.Define("/:type(a|b)/:id(0-9a-f]{24})", httpHandler)

	callback, params, ok := router.Match("/a/8")

	fmt.Println(ok)
	// -> true

	fmt.Println(callback.(http.Handler))

	fmt.Println(params)
	// -> map[string]string{"type": "a", "id": "8"}
}

Index

Examples

Constants

View Source
const Version = "1.1.0"

Version is this package's version number.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents a node in the trie tree. In this package, you can only get the root node by the New() func. And then define and match URL with it.

func New

func New() *Node

New returns a new root Node. you can define and match URL with it.

func (*Node) Define

func (n *Node) Define(u string, callback interface{})

Define defines a URL and some meta infomation (interface{}) on it.

support signatures:

- string: `/hello`

- separated string: `/a|b|c`

- regex: `/([0-9a-f]{24})`

- named parameter: `/:id`

- named separated string: `/:id(a|b|c)`

- named regex: `/:id([0-9a-f]{24})`

func (*Node) Match

func (n *Node) Match(u string) (interface{}, map[string]string, bool)

Match matchs a URL and get the meta infomation defined on it. You should always check the third return value at first to know whether the URL is matched. The first return value is the URL's defined meta infomation and the second return value is the map of named parameters.

Jump to

Keyboard shortcuts

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