urlrouter

package module
v0.0.0-...-8cda547 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2016 License: MIT Imports: 3 Imported by: 19

README

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

Documentation

Overview

Efficient URL routing using a Trie data structure.

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.

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

(Blog Post: https://www.ant0ine.com/post/better-url-routing-golang.html)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Route

type Route struct {
	// A string defining the route, like "/resource/:id.json".
	// Placeholders supported are:
	// :param that matches any char to the first '/' or '.'
	// *splat that matches everything to the end of the string
	PathExp string
	// Can be anything useful to point to the code to run for this route.
	Dest interface{}
}

type Router

type Router struct {
	// list of Routes, the order matters, if multiple Routes match, the first defined will be used.
	Routes []Route
	// contains filtered or unexported fields
}

func (*Router) FindRoute

func (self *Router) FindRoute(urlStr string) (*Route, map[string]string, error)

Parse the url string (complete or just the path) and return the first matching Route and the corresponding parameters.

func (*Router) FindRouteFromURL

func (self *Router) FindRouteFromURL(urlObj *url.URL) (*Route, map[string]string)

Return the first matching Route and the corresponding parameters for a given URL object.

func (*Router) Start

func (self *Router) Start() error

This validates the Routes and prepares the Trie data structure. It must be called once the Routes are defined and before trying to find Routes.

Directories

Path Synopsis
examples
simple command
webserver command
Special Trie implementation for the URLRouter.
Special Trie implementation for the URLRouter.

Jump to

Keyboard shortcuts

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