예제 #1
0
파일: mpath.go 프로젝트: 719Ben/muxy
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)
}
예제 #2
0
파일: mpath.go 프로젝트: 719Ben/muxy
// 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))
	})
}