Efficient URL routing using a Trie data structure.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
trie
.travis.yml
LICENSE
README.md
router.go
router_benchmark_test.go
router_test.go

README.md

Go-UrlRouter

Efficient URL routing using a Trie data structure.

Build Status

Note: This package has been merged into Go-Json-Rest, where it has been improved and specialized for the REST API use case.

This Package implements a URL Router, but instead of using the usual "evaluate all the routes and return the first regexp that matches" strategy, it uses a Trie data structure to perform the routing. This is more efficient, and scales better for a large number of routes. It supports the :param and *splat placeholders in the route strings.

Install

This package is "go-gettable", just do:

go get github.com/ant0ine/go-urlrouter

Example

router := urlrouter.Router{
	Routes: []urlrouter.Route{
		urlrouter.Route{
			PathExp: "/resources/:id",
			Dest:    "one_resource",
		},
		urlrouter.Route{
			PathExp: "/resources",
			Dest:    "all_resources",
		},
	},
}
err := router.Start()
if err != nil {
	panic(err)
}
input := "http://example.org/resources/123"
route, params, err := router.FindRoute(input)
if err != nil {
	panic(err)
}
fmt.Print(route.Dest)  // one_resource
fmt.Print(params["id"])  // 123

More Examples

  • Web Server Demo how to use the router with net/http
  • Go-Json-Rest A quick and easy way to setup a RESTful JSON API

Documentation

Copyright (c) 2013-2014 Antoine Imbert

MIT License

Analytics