mirror of
https://gitlab.com/ric_harvey/MailHog.git
synced 2024-11-24 06:44:04 +00:00
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package handler;
|
|
|
|
import (
|
|
"regexp"
|
|
"net/http"
|
|
)
|
|
|
|
// http://stackoverflow.com/questions/6564558/wildcards-in-the-pattern-for-http-handlefunc
|
|
|
|
type Route struct {
|
|
Pattern *regexp.Regexp
|
|
Handler HandlerFunc
|
|
}
|
|
|
|
type HandlerFunc func(http.ResponseWriter, *http.Request, *Route)
|
|
|
|
//type Handler http.Handler
|
|
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, route *Route) {
|
|
f(w, r, route)
|
|
}
|
|
|
|
type RegexpHandler struct {
|
|
routes []*Route
|
|
}
|
|
|
|
func (h *RegexpHandler) Handler(pattern *regexp.Regexp, handler HandlerFunc) {
|
|
h.routes = append(h.routes, &Route{pattern, handler})
|
|
}
|
|
|
|
func (h *RegexpHandler) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request, *Route)) {
|
|
h.routes = append(h.routes, &Route{pattern, HandlerFunc(handler)})
|
|
}
|
|
|
|
func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
for _, route := range h.routes {
|
|
if route.Pattern.MatchString(r.URL.Path) {
|
|
route.Handler.ServeHTTP(w, r, route)
|
|
return
|
|
}
|
|
}
|
|
// no pattern matched; send 404 response
|
|
http.NotFound(w, r)
|
|
}
|