Skip to content

A library to match URL patterns, like in gorilla/mux but without the router

License

Notifications You must be signed in to change notification settings

libertylocked/urlpattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

urlpattern

A library to match URL patterns, gorilla/mux style

  • Extract variables from a URL
  • Validate a URL against a pattern template
  • Match host, path, path prefix, or query parameters

Install

go get -u github.com/libertylocked/urlpattern

Examples

Matching paths

p := urlpattern.NewPattern().
    Path("/api/events/{id:[0-9]+}")
u, _ := url.Parse("https://super.example.com/api/events/12345")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "id": "12345" }

Matching hosts and paths

p := urlpattern.NewPattern().
    Host("{subdomain}.example.com").
    Path("/api/events/{id:[0-9]+}")
u, _ := url.Parse("https://super.example.com/api/events/12345")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "subdomain": "super", "id": "12345" }

Matching path prefixes

p := urlpattern.NewPattern().
    PathPrefix("/api")
u, _ := url.Parse("https://super.example.com/api/events/12345")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ }

Matching query params

p := urlpattern.NewPattern().
    Queries("key", "{key:[0-9]+}")
u, _ := url.Parse("http://super.example.com/api/events/12345?key=42")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "key": "42" }

All together

p := urlpattern.NewPattern().
    Host("{subdomain}.example.com").
    PathPrefix("/api").
    Path("/events/{id:[0-9]+}").
    Query("key", "{key}")
u, _ := url.Parse("http://super.example.com/api/events/12345?key=42")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "subdomain": "super", "id": "12345", "key": "42" }

About

A library to match URL patterns, like in gorilla/mux but without the router

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages