| 1 | package goji |
| 2 | |
| 3 | import "net/http" |
| 4 | |
| 5 | /* |
| 6 | Handle adds a new route to the Mux. Requests that match the given Pattern will |
| 7 | be dispatched to the given http.Handler. |
| 8 | |
| 9 | Routing is performed in the order in which routes are added: the first route |
| 10 | with a matching Pattern will be used. In particular, Goji guarantees that |
| 11 | routing is performed in a manner that is indistinguishable from the following |
| 12 | algorithm: |
| 13 | |
| 14 | // Assume routes is a slice that every call to Handle appends to |
| 15 | for _, route := range routes { |
| 16 | // For performance, Patterns can opt out of this call to Match. |
| 17 | // See the documentation for Pattern for more. |
| 18 | if r2 := route.pattern.Match(r); r2 != nil { |
| 19 | route.handler.ServeHTTP(w, r2) |
| 20 | break |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | It is not safe to concurrently register routes from multiple goroutines, or to |
| 25 | register routes concurrently with requests. |
| 26 | */ |
| 27 | func (m *Mux) Handle(p Pattern, h http.Handler) { |
| 28 | m.router.add(p, h) |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | HandleFunc adds a new route to the Mux. It is equivalent to calling Handle on a |
| 33 | handler wrapped with http.HandlerFunc, and is provided only for convenience. |
| 34 | */ |
| 35 | func (m *Mux) HandleFunc(p Pattern, h func(http.ResponseWriter, *http.Request)) { |
| 36 | m.Handle(p, http.HandlerFunc(h)) |
| 37 | } |