Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "log" "net/http" "github.com/infogulch/pathmatcher" ) func Index(w http.ResponseWriter, r *http.Request, _ pathmatcher.Params) { fmt.Fprint(w, "Welcome!\n") } func Hello(w http.ResponseWriter, r *http.Request, ps pathmatcher.Params) { fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) } type Handler func(w http.ResponseWriter, r *http.Request, ps pathmatcher.Params) func main() { matcher := pathmatcher.NewHttpMatcher[Handler]() matcher.GET("/", Index) matcher.GET("/hello/:name", Hello) log.Fatal(http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, handler, params, _ := matcher.Find(r.Method, r.URL.Path) if handler != nil { handler(w, r, params) } else { http.NotFound(w, r) } }))) }
Index ¶
- func CleanPath(p string) string
- type HttpMatcher
- func (m *HttpMatcher[V]) Add(method, path string, value V)
- func (m *HttpMatcher[V]) Allowed(path string) string
- func (m *HttpMatcher[V]) CONNECT(path string, value V)
- func (m *HttpMatcher[V]) DELETE(path string, value V)
- func (m *HttpMatcher[V]) Find(method, path string) (match string, value V, params Params, redir bool)
- func (m *HttpMatcher[V]) GET(path string, value V)
- func (m *HttpMatcher[V]) HEAD(path string, value V)
- func (m *HttpMatcher[V]) OPTIONS(path string, value V)
- func (m *HttpMatcher[V]) PATCH(path string, value V)
- func (m *HttpMatcher[V]) POST(path string, value V)
- func (m *HttpMatcher[V]) PUT(path string, value V)
- func (m *HttpMatcher[V]) TRACE(path string, value V)
- type Matcher
- type Param
- type Params
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanPath ¶
CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.
The following rules are applied iteratively until no further processing can be done:
- Replace multiple slashes with a single slash.
- Eliminate each . path name element (the current directory).
- Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
- Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.
If the result of this process is an empty string, "/" is returned
Types ¶
type HttpMatcher ¶
type HttpMatcher[V any] struct { // contains filtered or unexported fields }
HttpMatcher associates endpoints (methods + parameterized paths) with values.
Implemented as a map of method names to Matchers with a shared param pool.
func NewHttpMatcher ¶
func NewHttpMatcher[V any]() (m *HttpMatcher[V])
func (*HttpMatcher[V]) Add ¶ added in v0.2.0
func (m *HttpMatcher[V]) Add(method, path string, value V)
func (*HttpMatcher[V]) Allowed ¶
func (m *HttpMatcher[V]) Allowed(path string) string
Allowed returns an Allow list 1 based on the methods and endpoints set in the matcher.
func (*HttpMatcher[V]) CONNECT ¶ added in v0.2.0
func (m *HttpMatcher[V]) CONNECT(path string, value V)
func (*HttpMatcher[V]) DELETE ¶ added in v0.2.0
func (m *HttpMatcher[V]) DELETE(path string, value V)
func (*HttpMatcher[V]) Find ¶ added in v0.2.0
func (m *HttpMatcher[V]) Find(method, path string) (match string, value V, params Params, redir bool)
func (*HttpMatcher[V]) GET ¶ added in v0.2.0
func (m *HttpMatcher[V]) GET(path string, value V)
func (*HttpMatcher[V]) HEAD ¶ added in v0.2.0
func (m *HttpMatcher[V]) HEAD(path string, value V)
func (*HttpMatcher[V]) OPTIONS ¶ added in v0.2.0
func (m *HttpMatcher[V]) OPTIONS(path string, value V)
func (*HttpMatcher[V]) PATCH ¶ added in v0.2.0
func (m *HttpMatcher[V]) PATCH(path string, value V)
func (*HttpMatcher[V]) POST ¶ added in v0.2.0
func (m *HttpMatcher[V]) POST(path string, value V)
func (*HttpMatcher[V]) PUT ¶ added in v0.2.0
func (m *HttpMatcher[V]) PUT(path string, value V)
func (*HttpMatcher[V]) TRACE ¶ added in v0.2.0
func (m *HttpMatcher[V]) TRACE(path string, value V)
type Matcher ¶
type Matcher[V any] struct { // contains filtered or unexported fields }
Matcher associates parametrized paths with values.
It's implementation is a light wrapper around tree.go/node struct and manages a pool of parameters.