/enroute

A simple route-to-path matcher

Primary LanguageGoMIT LicenseMIT

enroute

Go Reference

A simple route-to-path matcher. The parser was extracted from livebud/mux.

Features

  • Uses a radix trie for better performance
  • Supports required, optional, regexp and wildcard slots
  • Smart slot delimiters (e.g. can match /{from}-{to})
  • Well-tested with 100s of tests

Install

go get github.com/matthewmueller/enroute

Example

package main

import (
	"net/http"

	"github.com/matthewmueller/enroute"
)

func main() {
  r := enroute.New()
  r.Insert("/", "index.html")
  r.Insert("/users/{id}", "users/show.html")
  r.Insert("/posts/{post_id}/comments/{id}", "posts/comments/show.html")
  r.Insert("/fly/{from}-{to}", "fly/routes.html")
  r.Insert("/v{major|[0-9]+}.{minor|[0-9]+}", "version.html")
  r.Insert("/{owner}/{repo}/{branch}/{path*}", "repo.html")
  match, _ := r.Match("/matthewmueller/enroute/main/enroute.go")
  fmt.Println(match.Route) // /{owner}/{repo}/{branch}/{path*}
  fmt.Println(match.Value) // repo.html
  fmt.Println(match.Slots[0].Key, match.Slots[0].Value) // owner matthewmueller
  fmt.Println(match.Slots[1].Key, match.Slots[1].Value) // repo enroute
  // ...
}

Contributors

License

MIT