Exemple #1
0
func New(options ...func(*matcher)) *muxy.Router {
	m := &matcher{
		root:            &node{edges: map[string]*node{}},
		patterns:        map[*muxy.Route]*pattern{},
		notFoundHandler: muxy.HandlerFunc(notFound),
	}
	for _, o := range options {
		o(m)
	}
	return muxy.New(m)
}
Exemple #2
0
// allowHandler returns a handler that sets a header with the given
// status code and allowed methods.
func allowHandler(handlers map[string]muxy.Handler, code int) muxy.Handler {
	allowed := make([]string, len(handlers)+1)
	allowed[0] = "OPTIONS"
	i := 1
	for m, _ := range handlers {
		if m != "" && m != "OPTIONS" {
			allowed[i] = m
			i++
		}
	}
	return muxy.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.Header().Set("Allow", strings.Join(allowed[:i], ", "))
		w.WriteHeader(code)
		fmt.Fprintln(w, code, http.StatusText(code))
	})
}